Skip to content

Instantly share code, notes, and snippets.

@muscaiu
Created February 1, 2019 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muscaiu/bfc8a1e9cef5b515bbd5edf1ce623abc to your computer and use it in GitHub Desktop.
Save muscaiu/bfc8a1e9cef5b515bbd5edf1ce623abc to your computer and use it in GitHub Desktop.
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