Skip to content

Instantly share code, notes, and snippets.

@elderfo
Last active September 28, 2017 22:11
Show Gist options
  • Save elderfo/21d56b93a79c4e2fac6598df48bcef16 to your computer and use it in GitHub Desktop.
Save elderfo/21d56b93a79c4e2fac6598df48bcef16 to your computer and use it in GitHub Desktop.
import fetch from 'node-fetch';
import { getCategories } from './data';
beforeEach(() => {
// Reset the modules back to their original state.
// In this case, we are setting `node-fetch` back
// to the original, unmocked version
jest.resetModules();
});
describe('getCategories', () => {
it('should return expected data from server', async () => {
const categories = await getCategories();
expect(categories).toEqual([
{ id: 1, name: 'Category 1' },
{ id: 2, name: 'Category 2' },
{ id: 3, name: 'Category 3' },
{ id: 4, name: 'Category 4' },
]);
});
it('should return expected mock data', async () => {
const mockResponse = {
categories: [
{ catId: 1, catName: 'Category 1' },
{ catId: 2, catName: 'Category 2' },
{ catId: 3, catName: 'Category 3' },
{ catId: 4, catName: 'Category 4' },
],
};
// Here we are telling jest to mock `node-fetch` with the
// specified factory method. The factory method creates
// a mock function, which returns the same implementation
// as when the files were separate
jest.doMock('node-fetch', () => {
return jest.fn(async () => {
return { json: async () => mockResponse };
});
});
// We now have to re-import the module we are testing so
// it picks up the mocked version of `node-fetch`
const data = require('./data');
// Make the call to the newly imported module
const categories = await data.getCategories();
expect(categories).toEqual([
{ id: 1, name: 'Category 1' },
{ id: 2, name: 'Category 2' },
{ id: 3, name: 'Category 3' },
{ id: 4, name: 'Category 4' },
]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment