Improving test coverage

This commit is contained in:
Jeff Avallone 2018-06-08 21:05:35 -04:00
parent 52d378be61
commit 54b48f5581
4 changed files with 38 additions and 7 deletions

View File

@ -42,8 +42,13 @@ describe('Form', () => {
const component = shallow( const component = shallow(
<Form t={ translate } syntaxes={ syntaxes } onSubmit={ onSubmit }/> <Form t={ translate } syntaxes={ syntaxes } onSubmit={ onSubmit }/>
); );
const exprInput = component.find('[name="expr"]');
const syntaxInput = component.find('[name="syntax"]');
exprInput.simulate('change', { target: { name: 'expr', value: 'Test expression' } });
syntaxInput.simulate('change', { target: { name: 'syntax', value: 'test' } });
const eventObj = { preventDefault: jest.fn() }; const eventObj = { preventDefault: jest.fn() };
component.setState({ syntax: 'test', expr: 'Test expression' });
component.find('form').simulate('submit', eventObj); component.find('form').simulate('submit', eventObj);
expect(eventObj.preventDefault).toHaveBeenCalled(); expect(eventObj.preventDefault).toHaveBeenCalled();

View File

@ -11,7 +11,20 @@ exports[`LocaleSwitcher rendering 1`] = `
<select <select
onChange={[Function]} onChange={[Function]}
value="en" value="en"
/> >
<option
key="en"
value="en"
>
/displayName
</option>
<option
key="fr"
value="fr"
>
/displayName
</option>
</select>
<SvgMock /> <SvgMock />
</div> </div>
</label> </label>

View File

@ -26,8 +26,6 @@ class LocaleSwitcher extends React.PureComponent {
current: localeToAvailable(i18n.language || '', Object.keys(locales), 'en') current: localeToAvailable(i18n.language || '', Object.keys(locales), 'en')
} }
localeSelector = React.createRef()
componentDidMount() { componentDidMount() {
i18n.on('languageChanged', this.handleLanguageChange); i18n.on('languageChanged', this.handleLanguageChange);
} }
@ -36,8 +34,8 @@ class LocaleSwitcher extends React.PureComponent {
i18n.off('languageChanged', this.handleLanguageChange); i18n.off('languageChanged', this.handleLanguageChange);
} }
handleSelectChange = () => { handleSelectChange = ({ target }) => {
i18n.changeLanguage(this.localeSelector.current.value); i18n.changeLanguage(target.value);
} }
handleLanguageChange = lang => { handleLanguageChange = lang => {
@ -50,7 +48,7 @@ class LocaleSwitcher extends React.PureComponent {
return <label> return <label>
<Trans>Language</Trans> <Trans>Language</Trans>
<div className={ style.switcher }> <div className={ style.switcher }>
<select value={ current } ref={ this.localeSelector } onChange={ this.handleSelectChange }> <select value={ current } onChange={ this.handleSelectChange }>
{ Object.keys(locales).map(locale => ( { Object.keys(locales).map(locale => (
<option value={ locale } key={ locale }>{ i18n.getFixedT(locale)('/displayName') }</option> <option value={ locale } key={ locale }>{ i18n.getFixedT(locale)('/displayName') }</option>
)) } )) }

View File

@ -1,4 +1,8 @@
jest.mock('components/SVG'); jest.mock('components/SVG');
jest.mock('locales', () => ({
en: {},
fr: {}
}));
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { shallow } from 'enzyme';
@ -13,4 +17,15 @@ describe('LocaleSwitcher', () => {
); );
expect(component).toMatchSnapshot(); expect(component).toMatchSnapshot();
}); });
test('changing language', () => {
const component = shallow(
<LocaleSwitcher t={ translate }/>
);
const selectInput = component.find('select');
selectInput.value = 'fr';
selectInput.simulate('change', { target: { value: 'fr' } });
expect(component.state('current')).toEqual('fr');
});
}); });