-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.test.js
37 lines (33 loc) · 1.08 KB
/
async.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const fetch = require('node-fetch');
const swapi = require('./async');
it('calls swapi to get people', () => {
expect.assertions(1);
return swapi.getPeople(fetch).then(data => {
expect(data.count).toEqual(87);
});
});
it('calls swapi to get people with promise', () => {
expect.assertions(2);
return swapi.getPeoplePromise(fetch).then(data => {
expect(data.count).toEqual(87);
expect(data.results.length).toBeGreaterThan(5);
});
});
it('getPeople returns count and results', () => {
const mockFetch = jest.fn().mockReturnValue(
Promise.resolve({
json: () =>
Promise.resolve({
count: 87,
results: [0, 1, 2, 3, 4, 5]
})
})
);
expect.assertions(4);
return swapi.getPeoplePromise(mockFetch).then(data => {
expect(mockFetch.mock.calls.length).toBe(1);
expect(mockFetch).toBeCalledWith('https://swapi.co/api/people');
expect(data.count).toEqual(87);
expect(data.results.length).toBeGreaterThan(5);
});
});