Updating LocaleSwitcher to use react-testing-library
This commit is contained in:
parent
0d512a1a4d
commit
f11c41b05e
@ -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>
|
||||||
Language
|
<span
|
||||||
</Trans>
|
data-component="Trans"
|
||||||
<div
|
data-props="{}"
|
||||||
className="switcher"
|
|
||||||
>
|
|
||||||
<select
|
|
||||||
onChange={[Function]}
|
|
||||||
value="en"
|
|
||||||
>
|
>
|
||||||
<option
|
Language
|
||||||
key="en"
|
</span>
|
||||||
value="en"
|
<div
|
||||||
|
class="switcher"
|
||||||
|
>
|
||||||
|
<select
|
||||||
|
data-testid="language-select"
|
||||||
>
|
>
|
||||||
English
|
<option
|
||||||
</option>
|
value="en"
|
||||||
<option
|
>
|
||||||
key="other"
|
English
|
||||||
value="other"
|
</option>
|
||||||
>
|
<option
|
||||||
Other
|
value="other"
|
||||||
</option>
|
>
|
||||||
</select>
|
Other
|
||||||
<ChevronsDown
|
</option>
|
||||||
color="currentColor"
|
</select>
|
||||||
size="24"
|
<span
|
||||||
/>
|
data-component="ChevronsDown"
|
||||||
</div>
|
data-props="{}"
|
||||||
</label>
|
/>
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
</DocumentFragment>
|
||||||
`;
|
`;
|
||||||
|
@ -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 }
|
||||||
|
@ -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));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user