Skip to content

Instantly share code, notes, and snippets.

@sandwichsudo
Last active July 27, 2017 14:33
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 sandwichsudo/7a202a7f943c6551e3ec1dbb7b96ee3c to your computer and use it in GitHub Desktop.
Save sandwichsudo/7a202a7f943c6551e3ec1dbb7b96ee3c to your computer and use it in GitHub Desktop.
Test for calling API when component mounts
// src/containers/RepoSearchPage/page/RepoSearchPage.js
// export the class to test in isolation
export class RepoSearchPage extends Component {
constructor(props) {
super(props);
}
...
}
// src/containers/RepoSearchPage/page/RepoSearchPage.spec.js
import React from 'react';
import { mount } from 'enzyme';
import { RepoSearchPage } from './RepoSearchPage';
describe('<RepoSearchPage />', () => {
let props = {};
let repoDataMock = {
name: 'foo',
id: 'bar',
stargazers_count: '5'
};
beforeEach(() => {
props = {
actions: {
fetchRepos: jest.fn(),
},
searchResults: [ repoDataMock ]
};
});
describe('componentWillMount', () => {
it('should call fetchRepos', () => {
mount(<RepoSearchPage {...props} />);
expect(props.actions.fetchRepos).toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment