Skip to content

Commit

Permalink
Add simple tests for contact-summary & nools lib code
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jul 9, 2018
1 parent 96d2e00 commit 88ecab1
Show file tree
Hide file tree
Showing 8 changed files with 534 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.17.7
* Add simple tests for contact-summary & nools lib code
* Fix comment ref NO_LABEL

## v1.17.6
* [compile-contact-summary] separate templated source into proper .js file
* Fix unit tests for compile-contact-summary
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-conf",
"version": "1.17.6",
"version": "1.17.7",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/js-to-string.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function toS(o) {
if(Array.isArray(o)) {
let s = '[';
let s = '';
o.forEach(i => s += toS(i) + ',');
return withoutTrailingComma(s) + ']';
return '[' + withoutTrailingComma(s) + ']';
}
switch(typeof o) {
case 'function':
Expand All @@ -15,7 +15,7 @@ function toS(o) {
}

function oToS(o) {
let s = '{';
let s = '';
Object.keys(o).forEach(k => {
let val = o[k];
if(typeof val === 'function') {
Expand All @@ -29,7 +29,7 @@ function oToS(o) {
}
s += `${k}:${val},`;
});
return withoutTrailingComma(s) + '}';
return '{' + withoutTrailingComma(s) + '}';
}

const withoutTrailingComma = s => s.slice(0, s.length-1);
Expand Down
10 changes: 9 additions & 1 deletion src/lib/simple-js-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ module.exports = (optsOrPath, ...exportNames) => {
if(!header) header = '';

const rawCode = paths.map(fs.read).join('');
const indentedCode = withIndent(' ', `'use strict';
let indentedCode = withIndent(' ', `'use strict';
${header}
${rawCode}`);

// Allow removal of `return` statements etc.
if(optsOrPath.trimLinesFromEnd) {
indentedCode = indentedCode
.split('\n')
.slice(0, -optsOrPath.trimLinesFromEnd)
.join('\n');
}

const returnList = exportNames.map(r => `${r}:${r}`).join(',');

const code =
Expand Down
49 changes: 49 additions & 0 deletions test/contact-summary/lib.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const assert = require('chai').assert;
const parseJs = require('../../src/lib/simple-js-parser');

describe('contact-summary lib', function() {
const loadLibWithConfig = ({ cards, fields }) => parseJs({
jsFiles: [ `${__dirname}/../../src/contact-summary/lib.js` ],
header: `var fields = ${JSON.stringify(fields)};` +
`var cards = ${JSON.stringify(cards)};`,
trimLinesFromEnd: 3,
},
'isReportValid',
'result');

describe('test-setup', function() {
it('should provide the lib', function() {
// given
const lib = loadLibWithConfig({ cards:[], fields:[] });

// expect
assert.isNotNull(lib);
});
});

describe('isReportValid', function() {
it('should return true if report has no errors property (as for xforms)', function() {
// given
const lib = loadLibWithConfig({ cards:[], fields:[] });

// expect
assert.isTrue(lib.isReportValid({}));
});

it('should return true if report has empty errors array (as for JSON forms)', function() {
// given
const lib = loadLibWithConfig({ cards:[], fields:[] });

// expect
assert.isTrue(lib.isReportValid({ errors:[] }));
});

it('should return false if report has errors', function() {
// given
const lib = loadLibWithConfig({ cards:[], fields:[] });

// expect
assert.isFalse(lib.isReportValid({ errors:['???'] }));
});
});
});
106 changes: 106 additions & 0 deletions test/lib/js-to-string.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const assert = require('chai').assert;

const jsToString = require('../../src/lib/js-to-string');

describe('jsToString', function() {
describe('String handling', function() {
it('should handle a simple string', function() {
// expect
assert.equal(jsToString('a simple string'), '"a simple string"');
});
it('a string with quotes', function() {
// given
const input = 'a "string" with \'quotes\'';
const expected = '"a \\"string\\" with \'quotes\'"';

// expect
assert.equal(jsToString(input), expected);
});
it('should handle a string with linebreaks', function() {
// given
const input = 'a\nstring\nwith\nlinebreaks\n';
const expected = '"a\\nstring\\nwith\\nlinebreaks\\n"';

// expect
assert.equal(jsToString(input), expected);
});
});

describe('Number handling', function() {
it('should handle zero', function() {
// expect
assert.equal(jsToString(0), '0');
});
it('should handle a positive number', function() {
// expect
assert.equal(jsToString(1), '1');
});
it('should handle a negative number', function() {
// expect
assert.equal(jsToString(-1), '-1');
});
});

describe('Array handling', function() {
it('should handle empty array', function() {
// expect
assert.equal(jsToString([]), '[]');
});
it('should handle array with one entry', function() {
// expect
assert.equal(jsToString([1]), '[1]');
});
it('should handle array with multiple entries', function() {
// expect
assert.equal(jsToString([1, 2, 3]), '[1,2,3]');
});
});

describe('Object handling', function() {
it('should handle empty object', function() {
// expect
assert.equal(jsToString({}), '{}');
});
it('should handle object with one property', function() {
// expect
assert.equal(jsToString({ a:1 }), '{a:1}');
});
it('should handle object with multiple properties', function() {
// expect
assert.equal(jsToString({ a:1, b:2, c:3 }), '{a:1,b:2,c:3}');
});
});

describe('Function handling', function() {
it('should not handle a simple function', function() {
try {
// when
jsToString(function() {});
assert.isNotOk('should have thrown :(');
} catch(e) {
// then
assert.equal(e.message, 'This function does not support functions!');
}
});
it('should not handle a named function', function() {
try {
// when
jsToString(function withName() {});
assert.isNotOk('should have thrown :(');
} catch(e) {
// then
assert.equal(e.message, 'This function does not support functions!');
}
});
it('should not handle a function which does something', function() {
try {
// when
jsToString(function(a, b) { return a + b; });
assert.isNotOk('should have thrown :(');
} catch(e) {
// then
assert.equal(e.message, 'This function does not support functions!');
}
});
});
});
Loading

0 comments on commit 88ecab1

Please sign in to comment.