diff --git a/src/__tests__/search.test.ts b/src/__tests__/search.test.ts index 73c8af3..8d2e466 100644 --- a/src/__tests__/search.test.ts +++ b/src/__tests__/search.test.ts @@ -34,39 +34,45 @@ describe('GET /users', () => { }); test('should return a list of users', async () => { + const headers = { + get: (): string => '; rel="next", ; rel="last"', + }; + + const users = { items: [{ + id: 905434, + login: 'ruanyf', + url: 'https://api.github.com/users/ruanyf', + }, { + id: 810438, + login: 'gaearon', + url: 'https://api.github.com/users/gaearon', + }], total_count: 2074951 }; + + const user = { + headers: { get: (): string => '' }, + status: 200, + json: () => Promise.resolve({ + avatar_url: 'https://avatars0.githubusercontent.com/u/905434?v=4', + bio: null, + login: 'ruanyf', + name: 'Ruan YiFeng', + followers: 51305, + following: 0, + type: 'User', + updated_at: '2019-04-14T14:52:27Z', + url: 'https://api.github.com/users/ruanyf', + }), + }; + (fetch as any) .mockResolvedValueOnce({ - headers: { - get: (): string => '; rel="next", ; rel="last"', - }, + headers, status: 200, - json: () => Promise.resolve({items: [{ - id: 905434, - login: 'ruanyf', - url: 'https://api.github.com/users/ruanyf', - }, { - id: 810438, - login: 'gaearon', - url: 'https://api.github.com/users/gaearon', - }], total_count: 2074951}), + json: () => Promise.resolve(users), }) - .mockResolvedValue({ - headers: {get: (): string => ''}, - status: 200, - json: () => Promise.resolve({ - avatar_url: 'https://avatars0.githubusercontent.com/u/905434?v=4', - bio: null, - login: 'ruanyf', - name: 'Ruan YiFeng', - followers: 51305, - following: 0, - type: 'User', - updated_at: '2019-04-14T14:52:27Z', - url: 'https://api.github.com/users/ruanyf', - }), - }); + .mockResolvedValue(user); const response = await request(server).get('/users?lang=javascript'); - expect(fetch).toHaveBeenCalledTimes(3); + expect(fetch).toHaveBeenCalledTimes(users.items.length + 1); expect(response.status).toEqual(200); expect(response.body.data).toMatchSnapshot(); });