Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var chai = require('chai');
- var R = require('ramda');
- const colorize = (str, num) => '\033[' + num + 'm ' + str + ' \033[39m';
- const hydrateField = donor => (fieldValue, fieldName) => {
- const donorValueOnMaybeMatchingField = R.prop(fieldName, donor);
- return donorValueOnMaybeMatchingField
- ? { ...fieldValue, content: donorValueOnMaybeMatchingField }
- : { ...fieldValue };
- }
- const hydrate = (form, donor) => R.mapObjIndexed(hydrateField(donor), form);
- const test = {
- testcaseA1: {
- title: 'Given match of field name, should push key-value-pair of name: {content: donorsValue} for all matches',
- input1: { a: { content: 1 }, b: { content: 1 }},
- input2: { a: 'foo', b: 'bar' },
- expectedOutcome: { a: { content: 'foo' }, b: {content: 'bar'} },
- assertion: chai.assert.deepEqual
- },
- testcaseA2: {
- title: 'Given extra fields on form, should preserve all of these extra form fields',
- input1: { a: { content: 1 }, b: { content: 2}, c: { content: 3}},
- input2: { a: 'foo'},
- expectedOutcome: { a: { content: 'foo' }, b: { content: 2 }, c: {content: 3}},
- assertion: chai.assert.deepEqual
- },
- testcaseA3: {
- title: 'Given extra field on donor, should omit extra donor fields',
- input1: { a: { content: 1 }, b: { content: 2 }, c: { content: 3 } },
- input2: { a: 'foo', x: 'bar' },
- expectedOutcome: { a: { content: 'foo' }, b: { content: 2 }, c: { content: 3 } },
- assertion: chai.assert.deepEqual
- },
- testcaseB1: {
- title: 'Given nested content, should preserve the nesting',
- input1: { a: { content: 1 } } ,
- input2: { a: { levelOne: { levelTwo: 'foo' } } },
- expectedOutcome: { a: { content: { levelOne: { levelTwo: 'foo' } } } },
- assertion: chai.assert.deepEqual
- },
- testcaseB2: {
- title: 'Given additional key value pairs, should preseve them',
- input1: { a: { otherField: 'abc', content: 1 } },
- input2: { a: 'foo' },
- expectedOutcome: { a: { otherField: 'abc', content: 'foo!' } },
- assertion: chai.assert.deepEqual
- },
- }
- const runTestcaseWith = fn => testcase => {
- console.log(`${testcase.title}`);
- const actualOutcame = fn(testcase.input1, testcase.input2);
- try {
- testcase.assertion(actualOutcame, testcase.expectedOutcome);
- } catch {
- console.log('\n\n');
- console.log(testcase.input1, testcase.input2);
- console.log('')
- console.log(testcase.input2);
- console.log('')
- console.log('=>')
- console.log('')
- console.log(actualOutcame);
- console.log('')
- console.log('=/=')
- console.log('')
- console.log(testcase.expectedOutcome);
- console.log('\n\n')
- throw Error(colorize('Tests failed'), 31)
- }
- };
- const performTesting = fn => {
- console.log(`Testing ${fn.name}`);
- console.log('Test runner commenced');
- Object.values(test).forEach(runTestcaseWith(fn));
- console.log(colorize('\nAll tests passed !', 32));
- };
- performTesting(hydrate);
Add Comment
Please, Sign In to add comment