Skip to content

Commit

Permalink
Decouple test data from test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mfilenko committed May 3, 2019
1 parent 9a56082 commit 5d7dbac
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/__tests__/search.test.ts
Expand Up @@ -34,39 +34,45 @@ describe('GET /users', () => {
});

test('should return a list of users', async () => {
const headers = {
get: (): string => '<https://api.github.com/search/users?q=language%3Apascal&page=2>; rel="next", <https://api.github.com/search/users?q=language%3Apascal&page=34>; 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 => '<https://api.github.com/search/users?q=language%3Apascal&page=2>; rel="next", <https://api.github.com/search/users?q=language%3Apascal&page=34>; 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();
});
Expand Down

0 comments on commit 5d7dbac

Please sign in to comment.