Guest User

Untitled

a guest
Jun 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. var chai = require('chai');
  2. var R = require('ramda');
  3.  
  4. const colorize = (str, num) => '\033[' + num + 'm ' + str + ' \033[39m';
  5.  
  6. const hydrateField = donor => (fieldValue, fieldName) => {
  7. const donorValueOnMaybeMatchingField = R.prop(fieldName, donor);
  8.  
  9. return donorValueOnMaybeMatchingField
  10. ? { ...fieldValue, content: donorValueOnMaybeMatchingField }
  11. : { ...fieldValue };
  12. }
  13.  
  14. const hydrate = (form, donor) => R.mapObjIndexed(hydrateField(donor), form);
  15.  
  16. const test = {
  17. testcaseA1: {
  18. title: 'Given match of field name, should push key-value-pair of name: {content: donorsValue} for all matches',
  19. input1: { a: { content: 1 }, b: { content: 1 }},
  20. input2: { a: 'foo', b: 'bar' },
  21. expectedOutcome: { a: { content: 'foo' }, b: {content: 'bar'} },
  22. assertion: chai.assert.deepEqual
  23. },
  24. testcaseA2: {
  25. title: 'Given extra fields on form, should preserve all of these extra form fields',
  26. input1: { a: { content: 1 }, b: { content: 2}, c: { content: 3}},
  27. input2: { a: 'foo'},
  28. expectedOutcome: { a: { content: 'foo' }, b: { content: 2 }, c: {content: 3}},
  29. assertion: chai.assert.deepEqual
  30. },
  31. testcaseA3: {
  32. title: 'Given extra field on donor, should omit extra donor fields',
  33. input1: { a: { content: 1 }, b: { content: 2 }, c: { content: 3 } },
  34. input2: { a: 'foo', x: 'bar' },
  35. expectedOutcome: { a: { content: 'foo' }, b: { content: 2 }, c: { content: 3 } },
  36. assertion: chai.assert.deepEqual
  37. },
  38. testcaseB1: {
  39. title: 'Given nested content, should preserve the nesting',
  40. input1: { a: { content: 1 } } ,
  41. input2: { a: { levelOne: { levelTwo: 'foo' } } },
  42. expectedOutcome: { a: { content: { levelOne: { levelTwo: 'foo' } } } },
  43. assertion: chai.assert.deepEqual
  44. },
  45. testcaseB2: {
  46. title: 'Given additional key value pairs, should preseve them',
  47. input1: { a: { otherField: 'abc', content: 1 } },
  48. input2: { a: 'foo' },
  49. expectedOutcome: { a: { otherField: 'abc', content: 'foo!' } },
  50. assertion: chai.assert.deepEqual
  51. },
  52. }
  53.  
  54. const runTestcaseWith = fn => testcase => {
  55. console.log(`${testcase.title}`);
  56.  
  57. const actualOutcame = fn(testcase.input1, testcase.input2);
  58.  
  59. try {
  60. testcase.assertion(actualOutcame, testcase.expectedOutcome);
  61. } catch {
  62. console.log('\n\n');
  63. console.log(testcase.input1, testcase.input2);
  64. console.log('')
  65. console.log(testcase.input2);
  66. console.log('')
  67. console.log('=>')
  68. console.log('')
  69. console.log(actualOutcame);
  70. console.log('')
  71. console.log('=/=')
  72. console.log('')
  73. console.log(testcase.expectedOutcome);
  74. console.log('\n\n')
  75.  
  76. throw Error(colorize('Tests failed'), 31)
  77. }
  78. };
  79.  
  80. const performTesting = fn => {
  81. console.log(`Testing ${fn.name}`);
  82. console.log('Test runner commenced');
  83.  
  84. Object.values(test).forEach(runTestcaseWith(fn));
  85.  
  86. console.log(colorize('\nAll tests passed !', 32));
  87. };
  88.  
  89. performTesting(hydrate);
Add Comment
Please, Sign In to add comment