promisesTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const { expect } = require('chai')
  2. const {
  3. promisifyAll,
  4. callbackifyMultiResult
  5. } = require('../../../../app/src/util/promises')
  6. describe('promisifyAll', function() {
  7. describe('basic functionality', function() {
  8. before(function() {
  9. this.module = {
  10. SOME_CONSTANT: 1,
  11. asyncAdd(a, b, callback) {
  12. callback(null, a + b)
  13. },
  14. asyncDouble(x, callback) {
  15. this.asyncAdd(x, x, callback)
  16. }
  17. }
  18. this.promisified = promisifyAll(this.module)
  19. })
  20. it('promisifies functions in the module', async function() {
  21. const sum = await this.promisified.asyncAdd(29, 33)
  22. expect(sum).to.equal(62)
  23. })
  24. it('binds this to the original module', async function() {
  25. const sum = await this.promisified.asyncDouble(38)
  26. expect(sum).to.equal(76)
  27. })
  28. it('does not copy over non-functions', async function() {
  29. expect(this.promisified).not.to.have.property('SOME_CONSTANT')
  30. })
  31. it('does not modify the prototype of the module', async function() {
  32. expect(this.promisified.toString()).to.equal('[object Object]')
  33. })
  34. })
  35. describe('without option', function() {
  36. before(function() {
  37. this.module = {
  38. asyncAdd(a, b, callback) {
  39. callback(null, a + b)
  40. },
  41. syncAdd(a, b) {
  42. return a + b
  43. }
  44. }
  45. this.promisified = promisifyAll(this.module, { without: 'syncAdd' })
  46. })
  47. it('does not promisify excluded functions', function() {
  48. expect(this.promisified.syncAdd).not.to.exist
  49. })
  50. it('promisifies other functions', async function() {
  51. const sum = await this.promisified.asyncAdd(12, 89)
  52. expect(sum).to.equal(101)
  53. })
  54. })
  55. describe('multiResult option', function() {
  56. before(function() {
  57. this.module = {
  58. asyncAdd(a, b, callback) {
  59. callback(null, a + b)
  60. },
  61. asyncArithmetic(a, b, callback) {
  62. callback(null, a + b, a * b)
  63. }
  64. }
  65. this.promisified = promisifyAll(this.module, {
  66. multiResult: { asyncArithmetic: ['sum', 'product'] }
  67. })
  68. })
  69. it('promisifies multi-result functions', async function() {
  70. const result = await this.promisified.asyncArithmetic(3, 6)
  71. expect(result).to.deep.equal({ sum: 9, product: 18 })
  72. })
  73. it('promisifies other functions normally', async function() {
  74. const sum = await this.promisified.asyncAdd(6, 1)
  75. expect(sum).to.equal(7)
  76. })
  77. })
  78. })
  79. describe('callbackifyMultiResult', function() {
  80. it('callbackifies a multi-result function', function(done) {
  81. async function asyncArithmetic(a, b) {
  82. return { sum: a + b, product: a * b }
  83. }
  84. const callbackified = callbackifyMultiResult(asyncArithmetic, [
  85. 'sum',
  86. 'product'
  87. ])
  88. callbackified(3, 11, (err, sum, product) => {
  89. if (err != null) {
  90. return done(err)
  91. }
  92. expect(sum).to.equal(14)
  93. expect(product).to.equal(33)
  94. done()
  95. })
  96. })
  97. it('propagates errors', function(done) {
  98. async function asyncBomb() {
  99. throw new Error('BOOM!')
  100. }
  101. const callbackified = callbackifyMultiResult(asyncBomb, [
  102. 'explosives',
  103. 'dynamite'
  104. ])
  105. callbackified(err => {
  106. expect(err).to.exist
  107. done()
  108. })
  109. })
  110. })