import React from 'react'; import { Link } from 'react-router-dom'; // import { mount } from 'enzyme'; import { I18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { mountWithContexts } from './enzymeHelpers'; import { Config } from '../src/contexts/Config'; import { withNetwork } from '../src/contexts/Network'; import { withRootDialog } from '../src/contexts/RootDialog'; describe('mountWithContexts', () => { describe('injected I18nProvider', () => { test('should mount and render', () => { const wrapper = mountWithContexts(
{({ i18n }) => ( {i18n._(t`Text content`)} )}
); expect(wrapper.find('div')).toMatchSnapshot(); }); test('should mount and render deeply nested consumer', () => { const Child = () => ( {({ i18n }) => (
{i18n._(t`Text content`)}
)}
); const Parent = () => (); const wrapper = mountWithContexts(); expect(wrapper.find('Parent')).toMatchSnapshot(); }); }); describe('injected Router', () => { it('should mount and render', () => { const wrapper = mountWithContexts(
home
); expect(wrapper.find('div')).toMatchSnapshot(); }); it('should mount and render with stubbed context', () => { const context = { router: { history: { push: jest.fn(), replace: jest.fn(), createHref: jest.fn(), }, route: { location: {}, match: {} } } }; const wrapper = mountWithContexts( (
home
), { context } ); const link = wrapper.find('Link'); expect(link).toHaveLength(1); link.simulate('click', { button: 0 }); wrapper.update(); expect(context.router.history.push).toHaveBeenCalledWith('/'); }); }); describe('injected ConfigProvider', () => { it('should mount and render with default values', () => { const Foo = () => ( {value => (
{value.custom_virtualenvs[0]} {value.version}
)}
); const wrapper = mountWithContexts(); expect(wrapper.find('Foo')).toMatchSnapshot(); }); it('should mount and render with custom Config value', () => { const config = { custom_virtualenvs: ['Fizz', 'Buzz'], version: '1.1', }; const Foo = () => ( {value => (
{value.custom_virtualenvs[0]} {value.version}
)}
); const wrapper = mountWithContexts( , { context: { config } } ); expect(wrapper.find('Foo')).toMatchSnapshot(); }); }); describe('injected Network', () => { it('should mount and render', () => { const Foo = () => (
test
); const Bar = withNetwork(Foo); const wrapper = mountWithContexts(); expect(wrapper.find('Foo')).toMatchSnapshot(); }); it('should mount and render with stubbed api', () => { const network = { api: { getFoo: jest.fn().mockReturnValue('foo value'), }, }; const Foo = ({ api }) => (
{api.getFoo()}
); const Bar = withNetwork(Foo); const wrapper = mountWithContexts(, { context: { network } }); expect(network.api.getFoo).toHaveBeenCalledTimes(1); expect(wrapper.find('div').text()).toEqual('foo value'); }); }); describe('injected root dialog', () => { it('should mount and render', () => { const Foo = ({ title, setRootDialogMessage }) => (
{title}
); const Bar = withRootDialog(Foo); const wrapper = mountWithContexts(); expect(wrapper.find('span').text()).toEqual(''); wrapper.find('button').simulate('click'); wrapper.update(); expect(wrapper.find('span').text()).toEqual('error'); }); it('should mount and render with stubbed value', () => { const dialog = { title: 'this be the title', setRootDialogMessage: jest.fn(), }; const Foo = ({ title, setRootDialogMessage }) => (
{title}
); const Bar = withRootDialog(Foo); const wrapper = mountWithContexts(, { context: { dialog } }); expect(wrapper.find('span').text()).toEqual('this be the title'); wrapper.find('button').simulate('click'); expect(dialog.setRootDialogMessage).toHaveBeenCalledWith('error'); }); }); });