Updating LocaleSwitcher to use react-testing-library

This commit is contained in:
Jeff Avallone 2019-03-24 14:32:11 -04:00
parent 0d512a1a4d
commit f11c41b05e
3 changed files with 56 additions and 44 deletions

View File

@ -1,34 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`LocaleSwitcher rendering 1`] = ` exports[`LocaleSwitcher rendering 1`] = `
<label> <DocumentFragment>
<Trans> <label>
<span
data-component="Trans"
data-props="{}"
>
Language Language
</Trans> </span>
<div <div
className="switcher" class="switcher"
> >
<select <select
onChange={[Function]} data-testid="language-select"
value="en"
> >
<option <option
key="en"
value="en" value="en"
> >
English English
</option> </option>
<option <option
key="other"
value="other" value="other"
> >
Other Other
</option> </option>
</select> </select>
<ChevronsDown <span
color="currentColor" data-component="ChevronsDown"
size="24" data-props="{}"
/> />
</div> </div>
</label> </label>
</DocumentFragment>
`; `;

View File

@ -38,7 +38,10 @@ export 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 } onChange={ this.handleSelectChange }> <select data-testid="language-select"
value={ current }
onChange={ this.handleSelectChange }
>
{ locales.map(locale => ( { locales.map(locale => (
<option value={ locale.code } key={ locale.code }> <option value={ locale.code } key={ locale.code }>
{ locale.name } { locale.name }

View File

@ -1,5 +1,9 @@
jest.mock('react-feather/dist/icons/chevrons-down', () =>
require('__mocks__/component-mock')(
'react-feather/dist/icons/chevrons-down'));
import React from 'react'; import React from 'react';
import { shallow } from 'enzyme'; import { render, fireEvent } from 'react-testing-library';
import i18n from 'i18n'; import i18n from 'i18n';
import { LocaleSwitcher } from 'components/LocaleSwitcher'; import { LocaleSwitcher } from 'components/LocaleSwitcher';
@ -9,44 +13,47 @@ jest.mock('./locale-to-available', () => jest.fn(() => 'en'));
describe('LocaleSwitcher', () => { describe('LocaleSwitcher', () => {
test('rendering', () => { test('rendering', () => {
const component = shallow( const { asFragment } = render(
<LocaleSwitcher /> <LocaleSwitcher />
); );
expect(component).toMatchSnapshot(); expect(asFragment()).toMatchSnapshot();
}); });
test('changing language', () => { test('changing language', () => {
jest.spyOn(i18n, 'changeLanguage'); jest.spyOn(i18n, 'changeLanguage');
const component = shallow( const { getByTestId } = render(
<LocaleSwitcher /> <LocaleSwitcher />
); );
const selectInput = component.find('select'); const event = new Event('change', { bubbles: true });
selectInput.value = 'other'; const select = getByTestId('language-select');
selectInput.simulate('change', { target: { value: 'other' } }); select.value = 'other';
fireEvent(select, event);
expect(i18n.changeLanguage).toHaveBeenCalledWith('other'); expect(i18n.changeLanguage).toHaveBeenCalledWith('other');
}); });
test('interface update from language change', () => { test('interface update from language change', () => {
const component = shallow( const { getByTestId } = render(
<LocaleSwitcher /> <LocaleSwitcher />
); );
expect(component.find('select').prop('value')).toEqual('en'); expect(getByTestId('language-select').value).toEqual('en');
i18n.emit('languageChanged', 'other'); i18n.emit('languageChanged', 'other');
expect(component.find('select').prop('value')).toEqual('other'); expect(getByTestId('language-select').value).toEqual('other');
}); });
test('disconnecting event handler on unmount', () => { test('disconnecting event handler on unmount', () => {
const component = shallow( const { unmount } = render(
<LocaleSwitcher /> <LocaleSwitcher />
); );
jest.spyOn(component, 'setState'); jest.spyOn(i18n, 'off');
component.unmount(); unmount();
i18n.emit('languageChanged', 'other'); expect(i18n.off).toHaveBeenCalledWith(
expect(component.setState).not.toHaveBeenCalled(); 'languageChanged',
expect.any(Function));
}); });
}); });