|
| 1 | +import { reuseOngoingRequest } from '../../src/lang/functional.js' |
| 2 | + |
| 3 | +describe('functional', () => { |
| 4 | + describe('reuseOnGoingRequest', () => { |
| 5 | + it('should call supplied function with the params', async () => { |
| 6 | + const expectedParams = ['a', 1, { a: 'a' }] |
| 7 | + const func = jest.fn(() => Promise.resolve()) |
| 8 | + |
| 9 | + const decoratedFunction = reuseOngoingRequest(func) |
| 10 | + await decoratedFunction(...expectedParams) |
| 11 | + |
| 12 | + expect(func).toHaveBeenCalledWith(...expectedParams) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should call supplied function with this', async () => { |
| 16 | + const expectedParams = ['a', 1, { a: 'a' }] |
| 17 | + const thisArg = { t: 'his' } |
| 18 | + const func = jest.fn(async function () { |
| 19 | + return this |
| 20 | + }) |
| 21 | + |
| 22 | + const decoratedFunction = reuseOngoingRequest(func, thisArg) |
| 23 | + const receivedThis = await decoratedFunction(...expectedParams) |
| 24 | + |
| 25 | + expect(receivedThis).toBe(thisArg) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should return values returned by the supplied function', async () => { |
| 29 | + const expectedResult = { a: 'abc' } |
| 30 | + const func = jest.fn(() => Promise.resolve(expectedResult)) |
| 31 | + |
| 32 | + const decoratedFunction = reuseOngoingRequest(func) |
| 33 | + const result = await decoratedFunction() |
| 34 | + |
| 35 | + expect(result).toBe(expectedResult) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should throw value thrown by supplied function', async () => { |
| 39 | + const error = new Error('Oops, I did it again!') |
| 40 | + const func = jest.fn(() => Promise.reject(error)) |
| 41 | + |
| 42 | + const decoratedFunction = reuseOngoingRequest(func) |
| 43 | + const promise = decoratedFunction() |
| 44 | + expect(promise).rejects.toThrow(error) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should share ongoing request with same params', async () => { |
| 48 | + const expectedParams = ['a', 1, [3]] |
| 49 | + const expectedResult = { a: 'abc' } |
| 50 | + const { promises, func } = mockPromiseFunction() |
| 51 | + |
| 52 | + const decoratedFunction = reuseOngoingRequest(func) |
| 53 | + |
| 54 | + const resultPromises = [ |
| 55 | + decoratedFunction(...expectedParams), |
| 56 | + decoratedFunction(...expectedParams), |
| 57 | + decoratedFunction(...expectedParams) |
| 58 | + ] |
| 59 | + |
| 60 | + expect(func).toBeCalledTimes(1) |
| 61 | + expect(promises.length).toBe(1) |
| 62 | + |
| 63 | + promises[0].resolve(expectedResult) // closing ongoing request |
| 64 | + |
| 65 | + const results = await Promise.all(resultPromises) |
| 66 | + |
| 67 | + expect(results).toEqual([expectedResult, expectedResult, expectedResult]) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not share ongoing request with different params', async () => { |
| 71 | + const expectedParams1 = ['a', 1, [3]] |
| 72 | + const expectedResult1 = { a: 'abc' } |
| 73 | + const expectedParams2 = [4, 'a', []] |
| 74 | + const expectedResult2 = { k: 'bbk' } |
| 75 | + const { promises, func } = mockPromiseFunction() |
| 76 | + |
| 77 | + const decoratedFunction = reuseOngoingRequest(func) |
| 78 | + |
| 79 | + const resultPromises = [ |
| 80 | + decoratedFunction(...expectedParams1), |
| 81 | + decoratedFunction(...expectedParams2) |
| 82 | + ] |
| 83 | + |
| 84 | + expect(func).toBeCalledTimes(2) |
| 85 | + expect(func).toBeCalledWith(...expectedParams1) |
| 86 | + expect(func).toBeCalledWith(...expectedParams2) |
| 87 | + |
| 88 | + expect(promises.length).toBe(2) |
| 89 | + |
| 90 | + promises[0].resolve(expectedResult1) // closing ongoing request 1 |
| 91 | + promises[1].resolve(expectedResult2) // closing ongoing request 2 |
| 92 | + |
| 93 | + const results = await Promise.all(resultPromises) |
| 94 | + |
| 95 | + expect(results).toEqual([expectedResult1, expectedResult2]) |
| 96 | + }) |
| 97 | + |
| 98 | + it('should not share resolved requests with same params', async () => { |
| 99 | + const expectedParams = ['a', 1, [3]] |
| 100 | + const expectedResult1 = { a: 'abc' } |
| 101 | + const expectedResult2 = { k: 'bbk' } |
| 102 | + const { promises, func } = mockPromiseFunction() |
| 103 | + |
| 104 | + const decoratedFunction = reuseOngoingRequest(func) |
| 105 | + |
| 106 | + const resultPromises = [ |
| 107 | + decoratedFunction(...expectedParams) |
| 108 | + ] |
| 109 | + |
| 110 | + expect(func).toBeCalledTimes(1) |
| 111 | + expect(promises.length).toBe(1) |
| 112 | + |
| 113 | + promises[0].resolve(expectedResult1) // closing ongoing request |
| 114 | + |
| 115 | + const results = await Promise.all(resultPromises) |
| 116 | + |
| 117 | + resultPromises.push(decoratedFunction(...expectedParams)) |
| 118 | + |
| 119 | + expect(func).toBeCalledTimes(2) |
| 120 | + expect(promises.length).toBe(2) |
| 121 | + |
| 122 | + promises[1].resolve(expectedResult2) // closing ongoing request |
| 123 | + |
| 124 | + results.push(await resultPromises[1]) |
| 125 | + |
| 126 | + expect(results).toEqual([expectedResult1, expectedResult2]) |
| 127 | + }) |
| 128 | + |
| 129 | + it('should not share rejected requests with same params', async () => { |
| 130 | + const expectedParams = ['a', 1, [3]] |
| 131 | + const expectedResult1 = new Error('Ops, I did it again!') |
| 132 | + const expectedResult2 = { k: 'bbk' } |
| 133 | + const { promises, func } = mockPromiseFunction() |
| 134 | + |
| 135 | + const decoratedFunction = reuseOngoingRequest(func) |
| 136 | + |
| 137 | + const resultPromises = [ |
| 138 | + decoratedFunction(...expectedParams) |
| 139 | + ] |
| 140 | + |
| 141 | + expect(func).toBeCalledTimes(1) |
| 142 | + expect(promises.length).toBe(1) |
| 143 | + |
| 144 | + promises[0].reject(expectedResult1) // closing ongoing request |
| 145 | + |
| 146 | + const results = await Promise.all( |
| 147 | + resultPromises.map(promise => promise.catch(error => error)) |
| 148 | + ) |
| 149 | + |
| 150 | + resultPromises.push(decoratedFunction(...expectedParams)) |
| 151 | + |
| 152 | + expect(func).toBeCalledTimes(2) |
| 153 | + expect(promises.length).toBe(2) |
| 154 | + |
| 155 | + promises[1].resolve(expectedResult2) // closing ongoing request |
| 156 | + |
| 157 | + results.push(await resultPromises[1]) |
| 158 | + |
| 159 | + expect(results).toEqual([expectedResult1, expectedResult2]) |
| 160 | + }) |
| 161 | + |
| 162 | + function mockPromiseFunction () { |
| 163 | + const promises = [] |
| 164 | + const func = jest.fn(() => new Promise((resolve, reject) => { |
| 165 | + promises.push({ resolve, reject }) |
| 166 | + })) |
| 167 | + return { promises, func } |
| 168 | + } |
| 169 | + }) |
| 170 | +}) |
0 commit comments