-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.test.js
35 lines (32 loc) · 1.08 KB
/
script2.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
const fetch = require('node-fetch');
const swapi = require('./script2');
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 a 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 => {
// what did mockFetch do inside swapi.getPeoplePromise function
expect(mockFetch.mock.calls.length).toBe(1);
expect(mockFetch).toBeCalledWith('http://swapi.py4e.com/api/people')
expect(data.count).toEqual(87)
expect (data.results.length).toBeGreaterThan(5)
})
})