helpers.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var assert = require('assert');
  2. var denodeify = require('../');
  3. function myNodeStyleFunction(argument1, argument2, callback) {
  4. if (argument1 && argument2) {
  5. callback(null, argument1+argument2);
  6. } else {
  7. callback('Need both arguments');
  8. }
  9. }
  10. exports.basicDenodeify = function() {
  11. it('should resolve when there are no errors', function(done) {
  12. var myDenodeifiedNodeStyleFunction = denodeify(myNodeStyleFunction);
  13. myDenodeifiedNodeStyleFunction(1, 2)
  14. .then(function(result) {
  15. assert.equal(3, result);
  16. done();
  17. }, function() {
  18. throw new Error('Error callback called wrongly');
  19. });
  20. });
  21. it('should reject when there are errors', function(done) {
  22. var myDenodeifiedNodeStyleFunction = denodeify(myNodeStyleFunction);
  23. var promise = myDenodeifiedNodeStyleFunction(1, undefined);
  24. assert(promise instanceof Promise);
  25. promise
  26. .then(function(result) {
  27. throw new Error('A Promised myNodeStyleFunction with one argument should never resolve');
  28. }, function(error) {
  29. assert.equal(error, 'Need both arguments');
  30. done();
  31. });
  32. });
  33. };
  34. function multipleArgumentsNodeStyleFunction(callback) {
  35. callback(null, 'a', 'b');
  36. }
  37. function myFilter(err, a, b) {
  38. return [err, [a, b]];
  39. }
  40. exports.multipleArguments = function() {
  41. it('should pass multiple arguments to the next then', function(done) {
  42. var myDenodeifiedNodeStyleFunction = denodeify(multipleArgumentsNodeStyleFunction, myFilter);
  43. myDenodeifiedNodeStyleFunction()
  44. .then(function(results) {
  45. assert.equal(results[0], 'a');
  46. assert.equal(results[1], 'b');
  47. done();
  48. });
  49. });
  50. };