Skip to content

Commit

Permalink
Tests and spreadsheet mock
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusmr13 committed Jun 30, 2016
1 parent 13b95f8 commit 37b8249
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 51 deletions.
86 changes: 56 additions & 30 deletions src/SheetsTemplater.gs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var QuickDrive = function (DriveApp, newConfig) {
var QuickDrive = function (DriveApp, SpreadsheetApp, newConfig) {
var QuickDrive = {};

QuickDrive.annotationFunctions = {
Expand All @@ -12,7 +12,7 @@ var QuickDrive = function (DriveApp, newConfig) {
'=': QuickDrive.annotationFunctions.REPLACE_TEXT,
'~': QuickDrive.annotationFunctions.FOR_EACH
};
this.config = {
this._config = {
folderId: '0ByQE0cDEoa0qLUlPU21xVzNqZVk',
templateId: '1stc2xmCa3QB61bTR52tomteWUOwlVZ4s8OSKWG5dP_8',
newDocumentName: 'My new sheet',
Expand All @@ -23,16 +23,40 @@ var QuickDrive = function (DriveApp, newConfig) {
permission: DriveApp.Permission.VIEW
}]
};
for (var propertie in newConfig) {
config[propertie] = newConfig[propertie];
var validateConfig = function (config) {
if (config.folderId && (typeof config.folderId != 'string' || config.folderId.length != 18)) {
throw new Error('invalid-folder-id');
}

if (config.templateId && (typeof config.templateId != 'string' || config.templateId.length != 45)) {
throw new Error('invalid-file-id');
}

if (config.newDocumentName && (typeof config.newDocumentName != 'string')) {
throw new Error('invalid-file-name');
}

if (config.stripeColor && (typeof config.stripeColor != 'string' ||
(config.stripeColor[0] == '#' && (config.stripeColor.length != 4 && config.stripeColor.length != 7) ||
(config.stripeColor[0] != '#' && !(config.stripeColor[0] == 'r' && config.stripeColor[1] == 'g' && config.stripeColor[2] == 'b'))
))) {
throw new Error('invalid-stripe-color');
}
};

if (newConfig) {
validateConfig(newConfig);
for (var propertie in newConfig) {
this._config[propertie] = newConfig[propertie];
}
}

QuickDrive.getSheetNewDocument = function () {
var templateFile = DriveApp.getFileById(config.templateId);
var newFile = templateFile.makeCopy(config.newDocumentName, DriveApp.getFolderById(config.folderId));
var templateFile = DriveApp.getFileById(_config.templateId);
var newFile = templateFile.makeCopy(_config.newDocumentName, DriveApp.getFolderById(_config.folderId));

for (var i = 0; i < config.permissions.length; i++) {
newFile.setSharing(config.permissions[i].access, config.permissions[i].permission);
for (var i = 0; i < _config.permissions.length; i++) {
newFile.setSharing(_config.permissions[i].access, _config.permissions[i].permission);
}

var ss = SpreadsheetApp.open(newFile);
Expand Down Expand Up @@ -157,35 +181,37 @@ var QuickDrive = function (DriveApp, newConfig) {
var annotationFunction = QuickDrive.getAnnotationType(cellValue)(properties);
};

QuickDrive.processSheet = function(json) {
var newSpreadSheet = QuickDrive.getSheetNewDocument();
var sheet = newSpreadSheet.sheet;
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var values = range.getValues();
var myThis = {};
myThis['this'] = json;
var properties = {
sheet: sheet,
json: myThis,
values: values,
i: 0,
j: 0
};
for (properties.i = 0; properties.i < properties.values.length; properties.i++) {
for (properties.j = 0; properties.j < properties.values[properties.i].length; properties.j++) {
QuickDrive.processCell(properties);
}
}
return newSpreadSheet;
};

return QuickDrive;
};

function doPost(e) {
var json = e ? JSON.parse(e.parameters.data[0]) : {};
var config = e ? (e.parameters.config ? JSON.parse(e.parameters.config[0]) : {}) : {};
var QuickDrive = new QuickDrive(DriveApp, SpreadsheetApp, config);
var newSpreadSheet = QuickDrive.getSheetNewDocument(json);
var sheet = newSpreadSheet.sheet;

var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var values = range.getValues();
var myThis = {};
myThis['this'] = json;
var properties = {
sheet: sheet,
json: myThis,
values: values,
i: 0,
j: 0
};
QuickDrive.setConfigs(config);
for (properties.i = 0; properties.i < properties.values.length; properties.i++) {
for (properties.j = 0; properties.j < properties.values[properties.i].length; properties.j++) {
QuickDrive.processCell(properties);
}
}

return ContentService.createTextOutput(newSpreadSheet.fileId);
var newFile = QuickDrive.processSheet(json);
return ContentService.createTextOutput(newFile.fileId);
};

if (typeof module !== 'undefined' && module.exports != null) {
Expand Down
35 changes: 21 additions & 14 deletions test/mock/DriveAppMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var DriveApp = function () {
this.setName = function (newName) {
properties.name = newName;
};
this._setProperties = function(newProperties) {
this._setProperties = function (newProperties) {
for (var propertie in newProperties) {
properties[propertie] = newProperties[propertie];
}
Expand Down Expand Up @@ -67,17 +67,10 @@ var DriveApp = function () {
};
};

var SpreadsheetApp = function () {
var SpreadSheetMock = function (mySheets) {
var sheets = mySheets || [];

this.getSheets = function () {
return sheets;
};
var SpreadsheetApp = function (mock) {
var SpreadSheetMock = function (mock) {
var SheetMock = function (myMatrix) {
var matrix = myMatrix || [
[]
];
var matrix = myMatrix;

var CellMock = function () {
var value = "";
Expand All @@ -95,7 +88,7 @@ var SpreadsheetApp = function () {
[]
];

this.getValues = function () {
this.getValues = function (row, col, lines, cols) {
var values = [];
for (var i = row; i < lines; i++) {
values[i] = [];
Expand All @@ -115,7 +108,7 @@ var SpreadsheetApp = function () {
};
};

SheetMock.getRange = function (row, col, lines, cols) {
this.getRange = function (row, col, lines, cols) {
var newMatrix = [
[]
];
Expand All @@ -127,12 +120,26 @@ var SpreadsheetApp = function () {
}
return new CellGroupMock(newMatrix);
};

this.getMaxRows = function () {
return matrix.length;
};

this.getMaxColumns = function () {
return matrix[0].length;
};
};

var sheets = [new SheetMock(mock)];

this.getSheets = function () {
return sheets;
};
};

return {
open: function (file) {
return new SpreadSheetMock();
return new SpreadSheetMock(mock);
}
}
};
Expand Down
72 changes: 65 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
var DriveApp = require('./mock/DriveAppMock.js').DriveAppMock.DriveApp;
var QuickDrive = require('../src/SheetsTemplater.gs').QuickDrive(DriveApp());
var assert = require('chai').assert;
evar DriveAppMock = require('./mock/DriveAppMock.js').DriveAppMock;
var DriveApp = DriveAppMock.DriveApp;
var SpreadsheetApp = DriveAppMock.SpreadsheetApp;
var QuickDriveConstructor = require('../src/SheetsTemplater.gs').QuickDrive;
var QuickDrive = QuickDriveConstructor(DriveApp(), SpreadsheetApp());
var chai = require('chai');
var assert = chai.assert;
var expect = chai.expect;

describe('QuickDrive', function () {
describe('QuickDrive functions', function () {
describe('getAnnotationType', function () {
it('it should return none annotation, just simple text', function () {
assert.equal(QuickDrive.annotationFunctions.NONE, QuickDrive.getAnnotationType('==simple text with no annotation=='));
Expand Down Expand Up @@ -30,9 +35,62 @@ describe('QuickDrive', function () {
assert.equal(QuickDrive.annotationFunctions.FOR_EACH, QuickDrive.getAnnotationType('{~foo.bar.with.many.properties.myList}'));
});
});
describe('merge configs', function () {
it('it should keep default config', function () {
var config = QuickDrive.confi
describe('validateConfig', function () {
var createQuickDriveWithConfig = function (propertie, value) {
var obj = {};
obj[propertie] = value;
return function () {
QuickDriveConstructor(DriveApp(), SpreadsheetApp(), obj);
};
};
it('it should have default config with no error', function () {
QuickDriveConstructor(DriveApp(), QuickDrive._config);
});
it('it should validate fileId', function () {
assert.throws(createQuickDriveWithConfig('templateId', 'myshortid'), Error, 'invalid-file-id');
assert.throws(createQuickDriveWithConfig('templateId', 2313123), Error, 'invalid-file-id');
assert.throws(createQuickDriveWithConfig('templateId', true), Error, 'invalid-file-id');
createQuickDriveWithConfig('fileId', '123456789012345678901234567890123456789012345')();
});
it('it should validate folderId', function () {
assert.throws(createQuickDriveWithConfig('folderId', 'myshortid'), Error, 'invalid-folder-id');
assert.throws(createQuickDriveWithConfig('folderId', 2313123), Error, 'invalid-folder-id');
assert.throws(createQuickDriveWithConfig('folderId', true), Error, 'invalid-folder-id');
createQuickDriveWithConfig('folderId', '123456789012345678')();
});
it('it should validate file name', function () {
assert.throws(createQuickDriveWithConfig('newDocumentName', 2313123), Error, 'invalid-file-name');
assert.throws(createQuickDriveWithConfig('newDocumentName', true), Error, 'invalid-file-name');
createQuickDriveWithConfig('newDocumentName', 'my new name')();
});
it('it should validate stripe color', function () {
assert.throws(createQuickDriveWithConfig('stripeColor', 2313123), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', true), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', '#3333'), Error, 'invalid-stripe-color');
assert.throws(createQuickDriveWithConfig('stripeColor', '3333'), Error, 'invalid-stripe-color');
createQuickDriveWithConfig('stripeColor', '#333333')();
createQuickDriveWithConfig('stripeColor', '#333')();
createQuickDriveWithConfig('stripeColor', 'rgb(123,123,123)')();
});
});
});

var matrixMock = [
['asd', 'koko', '{=this.my_header}', 'my left header'],
['asd', 'koko', '{=this.my_header}', 'my left header'],
['asd', 'koko', '{=this.my_header}', 'my left header']
];

var jsonMock = {
this: {
my_header: 'header title'
}
};
describe('QuickDrive functions', function () {
describe('get new sheet', function () {
it('it should return sheet with text replaced', function () {
var QuickDriveMock = QuickDriveConstructor(DriveApp(), SpreadsheetApp(matrixMock));
var file = QuickDriveMock.processSheet(jsonMock);
});
});
});

0 comments on commit 37b8249

Please sign in to comment.