promisesTests.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const { expect } = require('chai')
  2. const {
  3. promisifyAll,
  4. promisifyClass,
  5. callbackifyMultiResult,
  6. } = require('../../../../app/src/util/promises')
  7. describe('promisifyAll', function () {
  8. describe('basic functionality', function () {
  9. before(function () {
  10. this.module = {
  11. SOME_CONSTANT: 1,
  12. asyncAdd(a, b, callback) {
  13. callback(null, a + b)
  14. },
  15. asyncDouble(x, callback) {
  16. this.asyncAdd(x, x, callback)
  17. },
  18. }
  19. this.promisified = promisifyAll(this.module)
  20. })
  21. it('promisifies functions in the module', async function () {
  22. const sum = await this.promisified.asyncAdd(29, 33)
  23. expect(sum).to.equal(62)
  24. })
  25. it('binds this to the original module', async function () {
  26. const sum = await this.promisified.asyncDouble(38)
  27. expect(sum).to.equal(76)
  28. })
  29. it('does not copy over non-functions', async function () {
  30. expect(this.promisified).not.to.have.property('SOME_CONSTANT')
  31. })
  32. it('does not modify the prototype of the module', async function () {
  33. expect(this.promisified.toString()).to.equal('[object Object]')
  34. })
  35. })
  36. describe('without option', function () {
  37. before(function () {
  38. this.module = {
  39. asyncAdd(a, b, callback) {
  40. callback(null, a + b)
  41. },
  42. syncAdd(a, b) {
  43. return a + b
  44. },
  45. }
  46. this.promisified = promisifyAll(this.module, { without: ['syncAdd'] })
  47. })
  48. it('does not promisify excluded functions', function () {
  49. expect(this.promisified.syncAdd).not.to.exist
  50. })
  51. it('promisifies other functions', async function () {
  52. const sum = await this.promisified.asyncAdd(12, 89)
  53. expect(sum).to.equal(101)
  54. })
  55. })
  56. describe('multiResult option', function () {
  57. before(function () {
  58. this.module = {
  59. asyncAdd(a, b, callback) {
  60. callback(null, a + b)
  61. },
  62. asyncArithmetic(a, b, callback) {
  63. callback(null, a + b, a * b)
  64. },
  65. }
  66. this.promisified = promisifyAll(this.module, {
  67. multiResult: { asyncArithmetic: ['sum', 'product'] },
  68. })
  69. })
  70. it('promisifies multi-result functions', async function () {
  71. const result = await this.promisified.asyncArithmetic(3, 6)
  72. expect(result).to.deep.equal({ sum: 9, product: 18 })
  73. })
  74. it('promisifies other functions normally', async function () {
  75. const sum = await this.promisified.asyncAdd(6, 1)
  76. expect(sum).to.equal(7)
  77. })
  78. })
  79. })
  80. describe('promisifyClass', function () {
  81. describe('basic functionality', function () {
  82. before(function () {
  83. this.Class = class {
  84. constructor(a) {
  85. this.a = a
  86. }
  87. asyncAdd(b, callback) {
  88. callback(null, this.a + b)
  89. }
  90. }
  91. this.Promisified = promisifyClass(this.Class)
  92. })
  93. it('promisifies the class methods', async function () {
  94. const adder = new this.Promisified(1)
  95. const sum = await adder.asyncAdd(2)
  96. expect(sum).to.equal(3)
  97. })
  98. })
  99. describe('without option', function () {
  100. before(function () {
  101. this.Class = class {
  102. constructor(a) {
  103. this.a = a
  104. }
  105. asyncAdd(b, callback) {
  106. callback(null, this.a + b)
  107. }
  108. syncAdd(b) {
  109. return this.a + b
  110. }
  111. }
  112. this.Promisified = promisifyClass(this.Class, { without: ['syncAdd'] })
  113. })
  114. it('does not promisify excluded functions', function () {
  115. const adder = new this.Promisified(10)
  116. const sum = adder.syncAdd(12)
  117. expect(sum).to.equal(22)
  118. })
  119. it('promisifies other functions', async function () {
  120. const adder = new this.Promisified(23)
  121. const sum = await adder.asyncAdd(3)
  122. expect(sum).to.equal(26)
  123. })
  124. })
  125. describe('multiResult option', function () {
  126. before(function () {
  127. this.Class = class {
  128. constructor(a) {
  129. this.a = a
  130. }
  131. asyncAdd(b, callback) {
  132. callback(null, this.a + b)
  133. }
  134. asyncArithmetic(b, callback) {
  135. callback(null, this.a + b, this.a * b)
  136. }
  137. }
  138. this.Promisified = promisifyClass(this.Class, {
  139. multiResult: { asyncArithmetic: ['sum', 'product'] },
  140. })
  141. })
  142. it('promisifies multi-result functions', async function () {
  143. const adder = new this.Promisified(3)
  144. const result = await adder.asyncArithmetic(6)
  145. expect(result).to.deep.equal({ sum: 9, product: 18 })
  146. })
  147. it('promisifies other functions normally', async function () {
  148. const adder = new this.Promisified(6)
  149. const sum = await adder.asyncAdd(1)
  150. expect(sum).to.equal(7)
  151. })
  152. })
  153. })
  154. describe('callbackifyMultiResult', function () {
  155. it('callbackifies a multi-result function', function (done) {
  156. async function asyncArithmetic(a, b) {
  157. return { sum: a + b, product: a * b }
  158. }
  159. const callbackified = callbackifyMultiResult(asyncArithmetic, [
  160. 'sum',
  161. 'product',
  162. ])
  163. callbackified(3, 11, (err, sum, product) => {
  164. if (err != null) {
  165. return done(err)
  166. }
  167. expect(sum).to.equal(14)
  168. expect(product).to.equal(33)
  169. done()
  170. })
  171. })
  172. it('propagates errors', function (done) {
  173. async function asyncBomb() {
  174. throw new Error('BOOM!')
  175. }
  176. const callbackified = callbackifyMultiResult(asyncBomb, [
  177. 'explosives',
  178. 'dynamite',
  179. ])
  180. callbackified(err => {
  181. expect(err).to.exist
  182. done()
  183. })
  184. })
  185. })