|
| 1 | +import Query from './query'; |
| 2 | + |
| 3 | +const sumFn = async (...args: number[]) => args.reduce((a, b) => a + b, 0); |
| 4 | + |
| 5 | +describe('constructor', () => { |
| 6 | + it('should instantiate valid query object.', () => { |
| 7 | + const queryInstance = new Query(sumFn, ['base']); |
| 8 | + |
| 9 | + expect(queryInstance).toEqual({ |
| 10 | + baseKey: ['base'], |
| 11 | + queryFn: sumFn, |
| 12 | + getKey: expect.any(Function), |
| 13 | + create: expect.any(Function), |
| 14 | + }); |
| 15 | + }); |
| 16 | +}); |
| 17 | + |
| 18 | +describe('getKey', () => { |
| 19 | + it('should return a combination of baseKey and arguments.', () => { |
| 20 | + const queryInstance = new Query(sumFn, ['sum']); |
| 21 | + |
| 22 | + const actual = queryInstance.getKey(2, 3); |
| 23 | + expect(actual).toEqual(['sum', '2', '3']); |
| 24 | + }); |
| 25 | + |
| 26 | + it.each([ |
| 27 | + {params: [], expected: ['base']}, |
| 28 | + {params: [1], expected: ['base', '1']}, |
| 29 | + {params: [1, 2], expected: ['base', '1', '2']}, |
| 30 | + {params: [1, 2, 3], expected: ['base', '1', '2', '3']}, |
| 31 | + ])('should return $expected when called with $params', ({params, expected}) => { |
| 32 | + const queryInstance = new Query(jest.fn(), ['base']); |
| 33 | + const actual = queryInstance.getKey(...params); |
| 34 | + expect(actual).toEqual(expected); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('create', () => { |
| 39 | + it('should return a query object.', () => { |
| 40 | + const queryInstance = new Query(sumFn, ['sum']); |
| 41 | + |
| 42 | + const query = queryInstance.create(2, 3); |
| 43 | + expect(query).toEqual({ |
| 44 | + queryKey: ['sum', '2', '3'], |
| 45 | + queryFn: expect.any(Function), |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it.each([ |
| 50 | + {params: [], expected: 0}, |
| 51 | + {params: [1], expected: 1}, |
| 52 | + {params: [1, 2], expected: 3}, |
| 53 | + {params: [1, 2, 3], expected: 6}, |
| 54 | + {params: [1, 2, 3, 4], expected: 10}, |
| 55 | + ])('should return $expected when called with $params', ({params, expected}) => { |
| 56 | + const sum = new Query(sumFn, ['sum']); |
| 57 | + |
| 58 | + const query = sum.create(...params).queryFn(); |
| 59 | + expect(query).resolves.toBe(expected); |
| 60 | + }); |
| 61 | +}); |
0 commit comments