File size: 3,522 Bytes
1e92f2d |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
/**
* Test the HomePage
*/
import React from 'react';
import { render } from 'react-testing-library';
import { IntlProvider } from 'react-intl';
import { Provider } from 'react-redux';
import { browserHistory } from 'react-router-dom';
import { HomePage, mapDispatchToProps } from '../index';
import { changeUsername } from '../actions';
import { loadRepos } from '../../App/actions';
import configureStore from '../../../configureStore';
describe('<HomePage />', () => {
let store;
beforeAll(() => {
store = configureStore({}, browserHistory);
});
it('should render and match the snapshot', () => {
const {
container: { firstChild },
} = render(
<Provider store={store}>
<IntlProvider locale="en">
<HomePage loading={false} error={false} repos={[]} />
</IntlProvider>
</Provider>,
);
expect(firstChild).toMatchSnapshot();
});
it('should fetch the repos on mount if a username exists', () => {
const submitSpy = jest.fn();
render(
<Provider store={store}>
<IntlProvider locale="en">
<HomePage
username="Not Empty"
onChangeUsername={() => {}}
onSubmitForm={submitSpy}
/>
</IntlProvider>
</Provider>,
);
expect(submitSpy).toHaveBeenCalled();
});
it('should not call onSubmitForm if username is an empty string', () => {
const submitSpy = jest.fn();
render(
<Provider store={store}>
<IntlProvider locale="en">
<HomePage onChangeUsername={() => {}} onSubmitForm={submitSpy} />
</IntlProvider>
</Provider>,
);
expect(submitSpy).not.toHaveBeenCalled();
});
it('should not call onSubmitForm if username is null', () => {
const submitSpy = jest.fn();
render(
<Provider store={store}>
<IntlProvider locale="en">
<HomePage
username=""
onChangeUsername={() => {}}
onSubmitForm={submitSpy}
/>
</IntlProvider>
</Provider>,
);
expect(submitSpy).not.toHaveBeenCalled();
});
describe('mapDispatchToProps', () => {
describe('onChangeUsername', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.onChangeUsername).toBeDefined();
});
it('should dispatch changeUsername when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
const username = 'mxstbr';
result.onChangeUsername({ target: { value: username } });
expect(dispatch).toHaveBeenCalledWith(changeUsername(username));
});
});
describe('onSubmitForm', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.onSubmitForm).toBeDefined();
});
it('should dispatch loadRepos when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
result.onSubmitForm();
expect(dispatch).toHaveBeenCalledWith(loadRepos());
});
it('should preventDefault if called with event', () => {
const preventDefault = jest.fn();
const result = mapDispatchToProps(() => {});
const evt = { preventDefault };
result.onSubmitForm(evt);
expect(preventDefault).toHaveBeenCalledWith();
});
});
});
});
|