|
| 1 | +import { Requester } from '@chainlink/external-adapter-framework/util/requester' |
| 2 | +import { request } from '../../src/transport/requester' |
| 3 | + |
| 4 | +describe('requester.ts', () => { |
| 5 | + let mockRequester: jest.Mocked<Requester> |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + mockRequester = { |
| 9 | + request: jest.fn(), |
| 10 | + } as any |
| 11 | + }) |
| 12 | + |
| 13 | + describe('request function', () => { |
| 14 | + it('should return data from a single page', async () => { |
| 15 | + mockRequester.request.mockResolvedValueOnce({ |
| 16 | + response: { |
| 17 | + data: { |
| 18 | + data: [ |
| 19 | + { id: 1, value: 'test1' }, |
| 20 | + { id: 2, value: 'test2' }, |
| 21 | + ], |
| 22 | + page: { |
| 23 | + next: null, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } as any) |
| 28 | + |
| 29 | + const result = await request(mockRequester, 'url', 'path', 'key', 100) |
| 30 | + |
| 31 | + expect(result).toEqual([ |
| 32 | + { id: 1, value: 'test1' }, |
| 33 | + { id: 2, value: 'test2' }, |
| 34 | + ]) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should handle pagination across multiple pages', async () => { |
| 38 | + mockRequester.request |
| 39 | + .mockResolvedValueOnce({ |
| 40 | + response: { |
| 41 | + data: { |
| 42 | + data: [{ id: 1, value: 'page1' }], |
| 43 | + page: { |
| 44 | + next: 'next1', |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } as any) |
| 49 | + .mockResolvedValueOnce({ |
| 50 | + response: { |
| 51 | + data: { |
| 52 | + data: [{ id: 2, value: 'page2' }], |
| 53 | + page: { |
| 54 | + next: 'next2', |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } as any) |
| 59 | + .mockResolvedValueOnce({ |
| 60 | + response: { |
| 61 | + data: { |
| 62 | + data: [{ id: 3, value: 'page3' }], |
| 63 | + page: { |
| 64 | + next: null, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } as any) |
| 69 | + |
| 70 | + const result = await request(mockRequester, 'url', 'path', 'key', 100) |
| 71 | + |
| 72 | + expect(result).toEqual([ |
| 73 | + { id: 1, value: 'page1' }, |
| 74 | + { id: 2, value: 'page2' }, |
| 75 | + { id: 3, value: 'page3' }, |
| 76 | + ]) |
| 77 | + }) |
| 78 | + |
| 79 | + it('should handle empty data array', async () => { |
| 80 | + mockRequester.request.mockResolvedValueOnce({ |
| 81 | + response: { |
| 82 | + data: { |
| 83 | + data: [], |
| 84 | + page: { |
| 85 | + next: null, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } as any) |
| 90 | + |
| 91 | + const result = await request(mockRequester, 'url', 'path', 'key', 100) |
| 92 | + |
| 93 | + expect(result).toEqual([]) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should throw AdapterError when response is invalid', async () => { |
| 97 | + mockRequester.request.mockResolvedValueOnce(null as any) |
| 98 | + await expect(request(mockRequester, 'url', 'path', 'key', 100)).rejects.toThrow( |
| 99 | + 'API urlpath?limit=100 does not return data', |
| 100 | + ) |
| 101 | + |
| 102 | + mockRequester.request.mockResolvedValueOnce({ response: null } as any) |
| 103 | + await expect(request(mockRequester, 'url', 'path', 'key', 100)).rejects.toThrow( |
| 104 | + 'API urlpath?limit=100 does not return data', |
| 105 | + ) |
| 106 | + |
| 107 | + mockRequester.request.mockResolvedValueOnce({ response: { data: null } } as any) |
| 108 | + await expect(request(mockRequester, 'url', 'path', 'key', 100)).rejects.toThrow( |
| 109 | + 'API urlpath?limit=100 does not return data', |
| 110 | + ) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments