import React from 'react';
import TestRenderer from 'react-test-renderer';
import { MemoryRouter } from 'react-router-dom';
import renderRoutes from './renderRoutes';
window.__UMI_BIGFISH_COMPAT = false;
window.__IS_BROWSER = true;
beforeEach(() => {
jest.mock('umi/_runtimePlugin', () => ({
apply(key, { initialValue }) {
return initialValue;
},
}));
});
afterEach(() => {
jest.unmock('umi/_runtimePlugin');
});
function Layout({ children }) {
return (
Layout
{children}
);
}
function IndexPage({ location: { pathname } }) {
return (
Index Page
{pathname}
);
}
function UsersPage({ match }) {
return (
Users Page
{match.params.userId}
);
}
function BigfishParams({ params }) {
return (
BigfishParams Page
{params.userId}
);
}
function Redirect() {
return
Redirect Page
;
}
function NeedLogin({ children }) {
return (
Need Login
{children}
);
}
function NeedConfirm({ children }) {
return (
Need Confirm
{children}
);
}
function Route() {
return Route
;
}
function PassPropsLayout({ children }) {
return React.Children.map(children, child => {
return React.cloneElement(
child,
null,
React.Children.map(child.props.children, child => {
return React.cloneElement(child, { test: 'test' });
}),
);
});
}
function PassPropsRouteComponent({ test }) {
return PassPropsRouteComponent: {test || 'null'}
;
}
function Fallback() {
return Fallback
;
}
function BigfishRoute({ children, params }) {
return (
BigfishRoute: {params.userId}
{children}
);
}
function ShowFoo({ foo }) {
return foo: {foo || 'null'}
;
}
function GetInitialProps({ foo }) {
return {foo}
;
}
GetInitialProps.getInitialProps = async () => {
return new Promise(resolve => {
setTimeout(() => {
resolve({
foo: 'bar',
});
}, 500);
});
};
const routes = [
{
path: '/',
exact: true,
component: Layout,
routes: [{ path: '/', exact: true, component: IndexPage }],
},
{ path: '/users/:userId', exact: true, component: UsersPage },
{ path: '/bigfishParams/:userId', exact: true, component: BigfishParams },
{
path: '/bigfishParamsWithRoutes/:userId',
Routes: [BigfishRoute],
exact: true,
component: BigfishParams,
},
{ path: '/redirect', redirect: '/d' },
{ path: '/d', component: Redirect },
{ path: '/test-Routes', Routes: [NeedLogin], component: Route },
{
path: '/test-Routes-multiple',
Routes: [NeedLogin, NeedConfirm],
component: Route,
},
{
path: '/pass-props-from-layout',
component: PassPropsLayout,
routes: [{ path: '/pass-props-from-layout', component: PassPropsRouteComponent }],
},
{ path: '/g_plugins', component: ShowFoo },
{ path: '/get-initial-props', component: GetInitialProps },
{
path: '/layout-no-component',
routes: [{ path: '/layout-no-component/foo', component: IndexPage }],
},
{ component: Fallback },
];
test('index page with layout', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual([
{ type: 'h1', props: {}, children: ['Layout'] },
{ type: 'h1', props: {}, children: ['Index Page'] },
'/',
]);
});
test('layout no component', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)}
,
);
expect(tr.toJSON()).toEqual([
{ type: 'h1', props: {}, children: ['Index Page'] },
'/layout-no-component/foo',
]);
});
test('dynamic route params', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual([{ type: 'h1', props: {}, children: ['Users Page'] }, '123']);
});
test('redirect', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual({
type: 'h1',
props: {},
children: ['Redirect Page'],
});
});
test('bigfish compatible', () => {
window.__UMI_BIGFISH_COMPAT = true;
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual([{ type: 'h1', props: {}, children: ['BigfishParams Page'] }, '123']);
window.__UMI_BIGFISH_COMPAT = false;
});
xtest('bigfish compatible with Routes', () => {
window.__UMI_BIGFISH_COMPAT = true;
const tr = TestRenderer.create(
{renderRoutes(routes)}
,
);
expect(tr.toJSON()).toEqual([
{ type: 'h1', props: {}, children: ['BigfishRoute: ', '123'] },
{ type: 'h1', props: {}, children: ['BigfishParams Page'] },
'123',
]);
window.__UMI_BIGFISH_COMPAT = false;
});
test('Routes', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual([
{ type: 'h1', props: {}, children: ['Need Login'] },
{ type: 'h1', props: {}, children: ['Route'] },
]);
});
test('Multiple Routes', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual([
{ type: 'h1', props: {}, children: ['Need Login'] },
{ type: 'h1', props: {}, children: ['Need Confirm'] },
{ type: 'h1', props: {}, children: ['Route'] },
]);
});
test('pass props from layout to child routes', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)}
,
);
expect(tr.toJSON()).toEqual({
type: 'h1',
props: {},
children: ['PassPropsRouteComponent: ', 'test'],
});
});
test('fallback routes', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual({
type: 'h1',
props: {},
children: ['Fallback'],
});
});
xtest('patch with g_plugins', () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual({
type: 'h1',
props: {},
children: ['foo: ', 'bar'],
});
});
// TODO: 验证 initial props
test('get intiial props', async () => {
const tr = TestRenderer.create(
{renderRoutes(routes)},
);
expect(tr.toJSON()).toEqual({
type: 'h1',
props: {},
children: null,
});
});