Skip to content

Commit

Permalink
Update Router tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Starotitorov committed Nov 8, 2018
1 parent 231dd6b commit fd1ffb4
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 322 deletions.
275 changes: 93 additions & 182 deletions package-lock.json

Large diffs are not rendered by default.

47 changes: 0 additions & 47 deletions packages/peregrine/src/Peregrine/Peregrine.js

This file was deleted.

28 changes: 18 additions & 10 deletions packages/peregrine/src/Router/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@ export default class MagentoRouter extends Component {
};

render() {
const { apiBase, routerProps, using: Router, renderRoutes, renderRoutingError } = this.props;
const {
apiBase,
routerProps,
using: Router,
renderRoutes,
renderRoutingError
} = this.props;

return (
<Router {...routerProps}>
{renderRoutes({
magentoRoute: <Route>
{routeProps => (
<MagentoRouteHandler
{ ...routeProps }
apiBase={apiBase}
renderRoutingError={renderRoutingError}
/>
)}
</Route>
magentoRoute: (
<Route>
{routeProps => (
<MagentoRouteHandler
{...routeProps}
apiBase={apiBase}
renderRoutingError={renderRoutingError}
/>
)}
</Route>
)
})}
</Router>
);
Expand Down
38 changes: 19 additions & 19 deletions packages/peregrine/src/Router/__tests__/MagentoRouteHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ configure({ adapter: new Adapter() });
jest.mock('../resolveUnknownRoute');

const apiBase = 'https://store.com';
const children = jest.fn();
const renderRoutingError = jest.fn();
const location = { pathname: '/foo.html' };

const props = { apiBase, children, location };
const props = { apiBase, renderRoutingError, location };

const resolvedRoute = {
type: 'CMS_PAGE',
id: 2
};

beforeEach(() => {
children.mockClear();
renderRoutingError.mockClear();
});

afterEach(() => {
Expand All @@ -34,8 +34,8 @@ afterEach(() => {
test('renders `loading` while loading', () => {
shallow(<MagentoRouteHandler {...props} />);

expect(children).toHaveBeenCalledTimes(1);
expect(children).toHaveBeenNthCalledWith(1, {
expect(renderRoutingError).toHaveBeenCalledTimes(1);
expect(renderRoutingError).toHaveBeenNthCalledWith(1, {
hasError: false,
internalError: false,
loading: true,
Expand All @@ -46,10 +46,10 @@ test('renders `loading` while loading', () => {
test('renders `null` while loading if `children` is not a function', () => {
const localProps = { ...props };

delete localProps.children;
delete localProps.renderRoutingError;
shallow(<MagentoRouteHandler {...localProps} />);

expect(children).not.toHaveBeenCalled();
expect(renderRoutingError).not.toHaveBeenCalled();
});

test('renders `internalError` if `resolveUnknownRoute` fails', async () => {
Expand All @@ -58,14 +58,14 @@ test('renders `internalError` if `resolveUnknownRoute` fails', async () => {

await Promise.resolve(); // resolveUnknownRoute

expect(children).toHaveBeenCalledTimes(2);
expect(children).toHaveBeenNthCalledWith(1, {
expect(renderRoutingError).toHaveBeenCalledTimes(2);
expect(renderRoutingError).toHaveBeenNthCalledWith(1, {
hasError: false,
internalError: false,
loading: true,
notFound: false
});
expect(children).toHaveBeenNthCalledWith(2, {
expect(renderRoutingError).toHaveBeenNthCalledWith(2, {
hasError: true,
internalError: true,
loading: false,
Expand All @@ -79,14 +79,14 @@ test('renders `notFound` if resolved route is not matched', async () => {

await Promise.resolve(); // resolveUnknownRoute

expect(children).toHaveBeenCalledTimes(2);
expect(children).toHaveBeenNthCalledWith(1, {
expect(renderRoutingError).toHaveBeenCalledTimes(2);
expect(renderRoutingError).toHaveBeenNthCalledWith(1, {
hasError: false,
internalError: false,
loading: true,
notFound: false
});
expect(children).toHaveBeenNthCalledWith(2, {
expect(renderRoutingError).toHaveBeenNthCalledWith(2, {
hasError: true,
internalError: false,
loading: false,
Expand All @@ -104,14 +104,14 @@ test('renders `internalError` if `fetchRootComponent` fails', async () => {
await Promise.resolve(); // fetchRootComponent

expect(wrapper.state('componentMap').size).toBe(1);
expect(children).toHaveBeenCalledTimes(2);
expect(children).toHaveBeenNthCalledWith(1, {
expect(renderRoutingError).toHaveBeenCalledTimes(2);
expect(renderRoutingError).toHaveBeenNthCalledWith(1, {
hasError: false,
internalError: false,
loading: true,
notFound: false
});
expect(children).toHaveBeenNthCalledWith(2, {
expect(renderRoutingError).toHaveBeenNthCalledWith(2, {
hasError: true,
internalError: true,
loading: false,
Expand All @@ -130,8 +130,8 @@ test('renders RootComponent if `fetchRootComponent` succeeds', async () => {
await Promise.resolve(); // resolveUnknownRoute
await Promise.resolve(); // fetchRootComponent

expect(children).toHaveBeenCalledTimes(1);
expect(children).toHaveBeenNthCalledWith(1, {
expect(renderRoutingError).toHaveBeenCalledTimes(1);
expect(renderRoutingError).toHaveBeenNthCalledWith(1, {
hasError: false,
internalError: false,
loading: true,
Expand Down Expand Up @@ -163,7 +163,7 @@ test('skips `fetchRootComponent` if path is known', async () => {
await Promise.resolve(); // resolveUnknownRoute
await Promise.resolve(); // fetchRootComponent

expect(children).toHaveBeenCalledTimes(2);
expect(renderRoutingError).toHaveBeenCalledTimes(2);
expect(fetchRootComponent).toHaveBeenCalledTimes(2);
expect(wrapper.find(RootComponent)).toHaveLength(1);
});
70 changes: 13 additions & 57 deletions packages/peregrine/src/Router/__tests__/Router.test.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,26 @@
import React from 'react';
import { configure, mount, shallow } from 'enzyme';
import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { MemoryRouter } from 'react-router-dom';

import MagentoRouter, { Consumer as RouteConsumer } from '../Router';
import MagentoRouter from '../Router';

jest.mock('FETCH_ROOT_COMPONENT', () => jest.fn(), { virtual: true });

configure({ adapter: new Adapter() });

const apiBase = 'https://store.com';

const initialEntries = ['/some-product.html'];
const routerProps = { initialEntries };

test('renders a single, catch-all route', () => {
const routesWrapper = shallow(
<MagentoRouter using={MemoryRouter} apiBase={apiBase} />
).find('Route');
expect(routesWrapper.length).toBe(1);
expect(routesWrapper.prop('path')).toBeUndefined();
});

test('passes `config` and route props to context provider', () => {
const fn = jest.fn();
const props = { apiBase, using: MemoryRouter };
test('calls render routes function', () => {
const renderRoutesMock = jest.fn();

// we need to test context consumer, so we can't shallow render
mount(
<MagentoRouter {...props}>
<RouteConsumer>{fn}</RouteConsumer>
</MagentoRouter>
shallow(
<MagentoRouter
using={MemoryRouter}
apiBase={apiBase}
renderRoutes={renderRoutesMock}
/>
);

expect(fn).toHaveBeenCalledWith(
expect.objectContaining({
apiBase,
history: expect.anything(), // from Route
location: expect.anything(), // from Route
match: expect.anything() // from Route
})
);
});

test('passes `routerProps` to router, not context provider', () => {
const fn = jest.fn();
const props = { apiBase, routerProps, using: MemoryRouter };

// we need to test context consumer, so we can't shallow render
const wrapper = mount(
<MagentoRouter {...props}>
<RouteConsumer>{fn}</RouteConsumer>
</MagentoRouter>
);

expect(fn).toHaveBeenCalledWith(
expect.not.objectContaining({
initialEntries: expect.anything()
})
);
expect(wrapper.find('Router').instance().props).toEqual(
expect.objectContaining({})
);
expect(fn).toHaveBeenCalledWith(
expect.objectContaining({
history: expect.objectContaining({
length: initialEntries.length
})
})
);
expect(renderRoutesMock).toBeCalled();
});
6 changes: 5 additions & 1 deletion packages/venia-concept/src/RootComponents/CMS/CMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Page from 'src/components/Page';

export default class CMS extends Component {
render() {
return <Page><CategoryList title="Shop by category" id={2} /></Page>;
return (
<Page>
<CategoryList title="Shop by category" id={2} />
</Page>
);
}
}
4 changes: 1 addition & 3 deletions packages/venia-concept/src/components/Page/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class AppShell extends Component {

return (
<div className={className}>
<Main isMasked={overlay}>
{ children }
</Main>
<Main isMasked={overlay}>{children}</Main>
<Mask isActive={overlay} dismiss={closeDrawer} />
<Navigation isOpen={navIsOpen} />
<MiniCart isOpen={cartIsOpen} />
Expand Down
3 changes: 2 additions & 1 deletion packages/venia-concept/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ ReactDOM.render(
<Router
apiBase={apiBase}
renderRoutes={renderRoutes}
renderRoutingError={renderRoutingError}>
renderRoutingError={renderRoutingError}
>
<AppShell />
</Router>
</ReduxProvider>
Expand Down
2 changes: 1 addition & 1 deletion packages/venia-concept/src/renderRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CreateAccountPage from 'src/components/CreateAccountPage/index';
const renderRoutes = ({ magentoRoute }) => (
<Switch>
<Route exact path="/create-account" component={CreateAccountPage} />
{ magentoRoute }
{magentoRoute}
</Switch>
);

Expand Down
2 changes: 1 addition & 1 deletion packages/venia-concept/src/renderRoutingError.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ErrorView from 'src/components/ErrorView';

const renderRoutingError = props => <ErrorView { ...props } />;
const renderRoutingError = props => <ErrorView {...props} />;

export default renderRoutingError;

0 comments on commit fd1ffb4

Please sign in to comment.