-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
277 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
263 changes: 263 additions & 0 deletions
263
packages/react-router-config/modules/__tests__/renderRoutes-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
import renderRoutes from '../renderRoutes' | ||
import expect from 'expect' | ||
import React from 'react' | ||
import { renderToString } from 'react-dom/server' | ||
import StaticRouter from 'react-router/StaticRouter' | ||
|
||
describe('renderRoutes', () => { | ||
let rendered | ||
const Comp = ({ match, route, route: { routes } }) => ( | ||
rendered.push(route), | ||
renderRoutes(routes) | ||
) | ||
|
||
beforeEach(() => { | ||
rendered = [] | ||
}) | ||
|
||
it('renders pathless routes', () => { | ||
const routeToMatch = { | ||
component: Comp | ||
} | ||
const routes = [routeToMatch] | ||
|
||
renderToString( | ||
<StaticRouter location='/path' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(1) | ||
expect(rendered[0]).toMatch(routeToMatch) | ||
}) | ||
|
||
describe('Switch usage', () => { | ||
it('renders the first matched route', () => { | ||
const routeToMatch = { | ||
component: Comp, | ||
path: '/' | ||
} | ||
const routes = [routeToMatch, { | ||
component: Comp, | ||
}] | ||
|
||
renderToString( | ||
<StaticRouter location='/' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(1) | ||
expect(rendered[0]).toMatch(routeToMatch) | ||
}) | ||
|
||
it('renders the first matched route in nested routes', () => { | ||
const childRouteToMatch = { | ||
component: Comp, | ||
path: '/', | ||
} | ||
const routeToMatch = { | ||
component: Comp, | ||
path: '/', | ||
routes: [childRouteToMatch, { | ||
component: Comp | ||
}] | ||
} | ||
const routes = [routeToMatch, { | ||
component: Comp, | ||
}] | ||
|
||
renderToString( | ||
<StaticRouter location='/' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(2) | ||
expect(rendered[0]).toMatch(routeToMatch) | ||
expect(rendered[1]).toMatch(childRouteToMatch) | ||
}) | ||
}) | ||
|
||
describe('routes with exact', () => { | ||
it('renders the exact route', () => { | ||
const routeToMatch = { | ||
component: Comp, | ||
path: '/path/child', | ||
exact: true, | ||
routes: [{ | ||
component: Comp | ||
}] | ||
} | ||
const routes = [{ | ||
component: Comp, | ||
path: '/path', | ||
exact: true | ||
}, routeToMatch] | ||
|
||
renderToString( | ||
<StaticRouter location='/path/child' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(2) | ||
expect(rendered[0]).toEqual(routeToMatch) | ||
expect(rendered[1]).toEqual({ component: Comp }) | ||
}) | ||
|
||
it('skips exact route and does not render it and any of its child routes', () => { | ||
const routes = [{ | ||
component: Comp, | ||
path: '/path', | ||
exact: true, | ||
routes: [{ | ||
component: Comp | ||
}, { | ||
component: Comp | ||
}] | ||
}] | ||
|
||
renderToString( | ||
<StaticRouter location='/path/child' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
renderToString( | ||
<StaticRouter location='/' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(0) | ||
}) | ||
|
||
it('renders the matched exact route but not its child routes if they do not match', () => { | ||
const routes = [{ | ||
// should render | ||
component: Comp, | ||
path: '/path', | ||
exact: true, | ||
routes: [{ | ||
// should skip | ||
component: Comp, | ||
path: '/path/child', | ||
exact: true | ||
}, { | ||
// should render | ||
component: Comp | ||
}] | ||
}] | ||
|
||
renderToString( | ||
<StaticRouter location='/path/child/grandchild' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
renderToString( | ||
<StaticRouter location='/path' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(2) | ||
expect(rendered[0]).toEqual(routes[0]) | ||
expect(rendered[1]).toEqual(routes[0].routes[1]) | ||
}) | ||
}) | ||
|
||
describe('routes with exact + strict', () => { | ||
it('renders the exact strict route', () => { | ||
const routeToMatch = { | ||
component: Comp, | ||
path: '/path/', | ||
exact: true, | ||
strict: true | ||
} | ||
const routes = [{ | ||
// should skip | ||
component: Comp, | ||
path: '/path', | ||
exact: true, | ||
strict: true | ||
// should render | ||
}, routeToMatch] | ||
|
||
renderToString( | ||
<StaticRouter location='/path/' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(1) | ||
expect(rendered[0]).toEqual(routeToMatch) | ||
}) | ||
|
||
it('skips exact strict route and does not render it and any of its child routes', () => { | ||
const routes = [{ | ||
component: Comp, | ||
path: '/path/', | ||
exact: true, | ||
strict: true, | ||
routes: [{ | ||
component: Comp | ||
}, { | ||
component: Comp | ||
}] | ||
}] | ||
|
||
renderToString( | ||
<StaticRouter location='/path/child' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
renderToString( | ||
<StaticRouter location='/' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
renderToString( | ||
<StaticRouter location='/path' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(0) | ||
}) | ||
|
||
it('renders the matched exact strict route but not its child routes if they do not match', () => { | ||
const routes = [{ | ||
// should skip | ||
component: Comp, | ||
path: '/path', | ||
exact: true, | ||
strict: true | ||
}, { | ||
// should render | ||
component: Comp, | ||
path: '/path/', | ||
exact: true, | ||
strict: true, | ||
routes: [{ | ||
// should skip | ||
component: Comp, | ||
exact: true, | ||
strict: true, | ||
path: '/path' | ||
}, { | ||
// should render | ||
component: Comp, | ||
exact: true, | ||
strict: true, | ||
path: '/path/' | ||
}] | ||
}] | ||
|
||
renderToString( | ||
<StaticRouter location='/path/child/grandchild' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
renderToString( | ||
<StaticRouter location='/path/' context={{}}> | ||
{renderRoutes(routes)} | ||
</StaticRouter> | ||
) | ||
expect(rendered.length).toEqual(2) | ||
expect(rendered[0]).toEqual(routes[1]) | ||
expect(rendered[1]).toEqual(routes[1].routes[1]) | ||
}) | ||
}) | ||
}) |