|
| 1 | +import { intercept, onError, onFinish, onStart, onSuccess } from './interceptor' |
| 2 | + |
| 3 | +beforeEach(() => { |
| 4 | + vi.clearAllMocks() |
| 5 | +}) |
| 6 | + |
| 7 | +describe('intercept', () => { |
| 8 | + const interceptor1 = vi.fn(({ next }) => next()) |
| 9 | + const interceptor2 = vi.fn(({ next }) => next()) |
| 10 | + const main = vi.fn(() => Promise.resolve('__main__')) |
| 11 | + |
| 12 | + it('can intercept', async () => { |
| 13 | + interceptor2.mockReturnValueOnce(Promise.resolve('__interceptor2__')) |
| 14 | + |
| 15 | + const result = await intercept( |
| 16 | + [ |
| 17 | + interceptor1, |
| 18 | + interceptor2, |
| 19 | + ], |
| 20 | + { |
| 21 | + foo: 'bar', |
| 22 | + }, |
| 23 | + main, |
| 24 | + ) |
| 25 | + |
| 26 | + expect(result).toEqual('__interceptor2__') |
| 27 | + expect(interceptor1).toHaveBeenCalledTimes(1) |
| 28 | + expect(interceptor1).toHaveBeenCalledWith({ |
| 29 | + foo: 'bar', |
| 30 | + next: expect.any(Function), |
| 31 | + }) |
| 32 | + |
| 33 | + expect(interceptor2).toHaveBeenCalledTimes(1) |
| 34 | + expect(interceptor2).toHaveBeenCalledWith({ |
| 35 | + foo: 'bar', |
| 36 | + next: expect.any(Function), |
| 37 | + }) |
| 38 | + |
| 39 | + expect(main).toHaveBeenCalledTimes(0) |
| 40 | + |
| 41 | + expect(await interceptor2.mock.calls[0]![0].next()).toEqual('__main__') |
| 42 | + expect(main).toHaveBeenCalledTimes(1) |
| 43 | + expect(main).toHaveBeenCalledWith({ |
| 44 | + foo: 'bar', |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('can override options', async () => { |
| 49 | + interceptor1.mockImplementationOnce(({ next }) => next({ bar: 'foo' })) |
| 50 | + |
| 51 | + const result = await intercept( |
| 52 | + [ |
| 53 | + interceptor1, |
| 54 | + interceptor2, |
| 55 | + ], |
| 56 | + { |
| 57 | + foo: 'bar', |
| 58 | + }, |
| 59 | + main, |
| 60 | + ) |
| 61 | + |
| 62 | + expect(result).toEqual('__main__') |
| 63 | + |
| 64 | + expect(interceptor1).toHaveBeenCalledTimes(1) |
| 65 | + expect(interceptor1).toHaveBeenCalledWith({ |
| 66 | + foo: 'bar', |
| 67 | + next: expect.any(Function), |
| 68 | + }) |
| 69 | + |
| 70 | + expect(interceptor2).toHaveBeenCalledTimes(1) |
| 71 | + expect(interceptor2).toHaveBeenCalledWith({ |
| 72 | + bar: 'foo', |
| 73 | + next: expect.any(Function), |
| 74 | + }) |
| 75 | + |
| 76 | + expect(main).toHaveBeenCalledTimes(1) |
| 77 | + expect(main).toHaveBeenCalledWith({ |
| 78 | + bar: 'foo', |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('ignores conflict in the `next` options', async () => { |
| 83 | + /** Ensure even conflict still can override the `next` options */ |
| 84 | + interceptor2.mockImplementationOnce(({ next }) => next({ bar: 'foo', next: 'hello2' })) |
| 85 | + |
| 86 | + const result = await intercept( |
| 87 | + [ |
| 88 | + interceptor1, |
| 89 | + interceptor2, |
| 90 | + ], |
| 91 | + { |
| 92 | + foo: 'bar', |
| 93 | + next: 'hello', |
| 94 | + }, |
| 95 | + main, |
| 96 | + ) |
| 97 | + |
| 98 | + expect(result).toEqual('__main__') |
| 99 | + |
| 100 | + expect(interceptor1).toHaveBeenCalledTimes(1) |
| 101 | + expect(interceptor1).toHaveBeenCalledWith({ |
| 102 | + foo: 'bar', |
| 103 | + next: expect.any(Function), |
| 104 | + }) |
| 105 | + |
| 106 | + expect(interceptor2).toHaveBeenCalledTimes(1) |
| 107 | + expect(interceptor2).toHaveBeenCalledWith({ |
| 108 | + foo: 'bar', |
| 109 | + next: expect.any(Function), |
| 110 | + }) |
| 111 | + |
| 112 | + expect(main).toHaveBeenCalledTimes(1) |
| 113 | + expect(main).toHaveBeenCalledWith({ |
| 114 | + bar: 'foo', |
| 115 | + next: 'hello2', |
| 116 | + }) |
| 117 | + }) |
| 118 | +}) |
| 119 | + |
| 120 | +describe('onStart / onSuccess / onError / onFinish', () => { |
| 121 | + const onStartFn = vi.fn() |
| 122 | + const onSuccessFn = vi.fn() |
| 123 | + const onErrorFn = vi.fn() |
| 124 | + const onFinishFn = vi.fn() |
| 125 | + |
| 126 | + it('on success', async () => { |
| 127 | + const result = await intercept( |
| 128 | + [ |
| 129 | + onStart(onStartFn), |
| 130 | + onSuccess(onSuccessFn), |
| 131 | + onError(onErrorFn), |
| 132 | + onFinish(onFinishFn), |
| 133 | + ], |
| 134 | + { |
| 135 | + foo: 'bar', |
| 136 | + }, |
| 137 | + () => Promise.resolve('__main__'), |
| 138 | + ) |
| 139 | + |
| 140 | + expect(result).toEqual('__main__') |
| 141 | + |
| 142 | + expect(onStartFn).toHaveBeenCalledTimes(1) |
| 143 | + expect(onStartFn).toHaveBeenCalledWith({ |
| 144 | + foo: 'bar', |
| 145 | + next: expect.any(Function), |
| 146 | + }) |
| 147 | + |
| 148 | + expect(onSuccessFn).toHaveBeenCalledTimes(1) |
| 149 | + expect(onSuccessFn).toHaveBeenCalledWith( |
| 150 | + '__main__', |
| 151 | + { |
| 152 | + foo: 'bar', |
| 153 | + next: expect.any(Function), |
| 154 | + }, |
| 155 | + ) |
| 156 | + |
| 157 | + expect(onFinishFn).toHaveBeenCalledTimes(1) |
| 158 | + expect(onFinishFn).toHaveBeenCalledWith( |
| 159 | + ['__main__', null, 'success'], |
| 160 | + { |
| 161 | + foo: 'bar', |
| 162 | + next: expect.any(Function), |
| 163 | + }, |
| 164 | + ) |
| 165 | + |
| 166 | + expect(onErrorFn).toHaveBeenCalledTimes(0) |
| 167 | + }) |
| 168 | + |
| 169 | + it('on error', async () => { |
| 170 | + await expect(intercept( |
| 171 | + [ |
| 172 | + onStart(onStartFn), |
| 173 | + onSuccess(onSuccessFn), |
| 174 | + onError(onErrorFn), |
| 175 | + onFinish(onFinishFn), |
| 176 | + ], |
| 177 | + { |
| 178 | + foo: 'bar', |
| 179 | + }, |
| 180 | + () => Promise.reject(new Error('__error__')), |
| 181 | + )).rejects.toThrowError('__error__') |
| 182 | + |
| 183 | + expect(onStartFn).toHaveBeenCalledTimes(1) |
| 184 | + expect(onStartFn).toHaveBeenCalledWith({ |
| 185 | + foo: 'bar', |
| 186 | + next: expect.any(Function), |
| 187 | + }) |
| 188 | + |
| 189 | + expect(onErrorFn).toHaveBeenCalledTimes(1) |
| 190 | + expect(onErrorFn).toHaveBeenCalledWith( |
| 191 | + new Error('__error__'), |
| 192 | + { |
| 193 | + foo: 'bar', |
| 194 | + next: expect.any(Function), |
| 195 | + }, |
| 196 | + ) |
| 197 | + |
| 198 | + expect(onFinishFn).toHaveBeenCalledTimes(1) |
| 199 | + expect(onFinishFn).toHaveBeenCalledWith( |
| 200 | + [undefined, new Error('__error__'), 'error'], |
| 201 | + { |
| 202 | + foo: 'bar', |
| 203 | + next: expect.any(Function), |
| 204 | + }, |
| 205 | + ) |
| 206 | + |
| 207 | + expect(onSuccessFn).toHaveBeenCalledTimes(0) |
| 208 | + }) |
| 209 | +}) |
0 commit comments