test-exit-coercion.helper.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Module dependencies
  3. */
  4. var util = require('util');
  5. var _ = require('@sailshq/lodash');
  6. var rttc = require('rttc');
  7. var Machine = require('../../');
  8. var isEquivalent = require('../../node_modules/rttc/spec/helpers/is-equivalent');
  9. module.exports = function testExitCoercion(expectations, cb){
  10. // Determine type schema of the value.
  11. // (using inference to pull it from the `example`, if provided)
  12. var typeSchema = rttc.infer(expectations.example);
  13. var machine = Machine.build({
  14. inputs: {},
  15. exits: {
  16. error: {},
  17. success: (function _determineExitDefinition(){
  18. var def = {};
  19. if (!_.isUndefined(expectations.example)) {
  20. def.example = expectations.example;
  21. }
  22. if (!_.isUndefined(expectations.getExample)) {
  23. def.getExample = expectations.getExample;
  24. }
  25. if (!_.isUndefined(expectations.void)) {
  26. def.void = expectations.void;
  27. }
  28. return def;
  29. })()
  30. },
  31. fn: function (inputs, exits) {
  32. exits(null, expectations.actual);
  33. }
  34. })
  35. .exec(function (err, actualResult){
  36. if (err) return cb(new Error('Unexpected error:'+require('util').inspect(err, false, null)));
  37. // If `example` is undefined, automatically pass the test (because `undefined` exit example
  38. // is a special case that is slightly different than ref: ie. it skips base value coercion)
  39. if (_.isUndefined(expectations.example)) {
  40. // TODO: consider doing a strictEq check here instead
  41. return cb();
  42. }
  43. // If an expected `result` is provided, compare the actual result against that.
  44. // Otherwise compare it against the original value (`actual`)
  45. var compareTo = expectations.hasOwnProperty('result') ? expectations.result : expectations.actual;
  46. // if `result` is set, use a lodash equality check
  47. if (!isEquivalent(actualResult, compareTo, typeSchema)) {
  48. return cb(new Error('returned incorrect value: '+util.inspect(actualResult, false, null)));
  49. }
  50. // Test using strict equality (===) if explicitly requested
  51. if (expectations.strictEq) {
  52. if (actualResult !== compareTo) {
  53. return cb(new Error('returned value is equivalent (but not ===)'));
  54. }
  55. }
  56. // Test AGAINST strict equality using `isNew` if requested
  57. // (i.e. guarantees this is a new value and is !== what was passed in)
  58. if (expectations.isNew) {
  59. // Check both the expected result and the actual value, just to be safe.
  60. // (should never even be possible for it to be a direct reference to the expected result)
  61. if (actualResult === compareTo || actualResult === expectations.actual) {
  62. return cb(new Error('returned value === value that was passed in -- but should have been a new value!'));
  63. }
  64. }
  65. // If we made it here, everything's good!
  66. return cb();
  67. });
  68. };