Created
February 1, 2019 15:19
-
-
Save muscaiu/bfc8a1e9cef5b515bbd5edf1ce623abc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetchData from './Api'; | |
describe('API Caller', () => { | |
it('should call YesNo API', () => { | |
const fetchSpy = jest.spyOn(global, 'fetch') | |
.mockImplementation(() => Promise.resolve({ | |
json: () => {}, | |
})); | |
return fetchData() | |
.then(() => { | |
expect(fetchSpy).toHaveBeenCalledWith('https://yesno.wtf/api/'); | |
}) | |
}); | |
it('should return YesNo response in JSON format', () => { | |
jest.spyOn(global, 'fetch') | |
.mockImplementation(() => Promise.resolve({ | |
json: () => ({ foo: 'bar' }), | |
})); | |
return fetchData() | |
.then(response => { | |
expect(response).toEqual({ foo: 'bar' }); | |
}); | |
}); | |
afterEach(() => { | |
jest.resetAllMocks(); | |
}); | |
}); | |
//Api.js | |
export default () => fetch(`https://yesno.wtf/api/`) | |
.then(response => response.json()); | |
//__mocks__/Api.js | |
export default () => Promise.resolve({ | |
answer: 'no', | |
forced: false, | |
image: 'https://yesno.wtf/assets/no/0-b6d3e555af2c09094def76cf2fbddf46.gif' | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment