Skip to content

Commit

Permalink
Pass router via context in component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Dec 8, 2016
1 parent 31fde91 commit 34271b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
7 changes: 6 additions & 1 deletion modules/__tests__/MatchWithRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import MatchWithRoutes from '../MatchWithRoutes'
import React from 'react'
import renderer from 'react-test-renderer'
import { MemoryRouter as Router } from 'react-router'

test('passes routes in as a prop', () => {
const location = { pathname: '/foo' }
Expand All @@ -14,7 +15,11 @@ test('passes routes in as a prop', () => {
<div>{routes[0].pattern}</div>
)
}
const el = <MatchWithRoutes location={location} {...route} />
const el = (
<Router initialEntries={[location]}>
<MatchWithRoutes {...route} />
</Router>
)
const tree = renderer.create(el).toJSON()
expect(tree.children[0]).toBe(route.routes[0].pattern)
})
Expand Down
25 changes: 16 additions & 9 deletions modules/__tests__/NamedLink-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NamedLink from '../NamedLink'
import RoutesProvider from '../RoutesProvider'
import React from 'react'
import renderer from 'react-test-renderer'
import { MemoryRouter as Router } from 'react-router'

const location = { pathname: '/foo' }

Expand All @@ -23,29 +24,35 @@ const routes = [

test('renders correct href for a root route', () => {
const el = (
<RoutesProvider routes={routes}>
<NamedLink to="parent" location={location} />
</RoutesProvider>
<Router initialEntries={[location]}>
<RoutesProvider routes={routes}>
<NamedLink to="parent" />
</RoutesProvider>
</Router>
)
const tree = renderer.create(el).toJSON()
expect(tree.props.href).toBe('/')
})

test('renders correct href for a nested route', () => {
const el = (
<RoutesProvider routes={routes}>
<NamedLink to="child1" location={location} />
</RoutesProvider>
<Router initialEntries={[location]}>
<RoutesProvider routes={routes}>
<NamedLink to="child1" />
</RoutesProvider>
</Router>
)
const tree = renderer.create(el).toJSON()
expect(tree.props.href).toBe('/child1')
})

test('renders correct href for a nested route with params', () => {
const el = (
<RoutesProvider routes={routes}>
<NamedLink to="child2" params={{ id: 'test' }} location={location} />
</RoutesProvider>
<Router initialEntries={[location]}>
<RoutesProvider routes={routes}>
<NamedLink to="child2" params={{ id: 'test' }} />
</RoutesProvider>
</Router>
)
const tree = renderer.create(el).toJSON()
expect(tree.props.href).toBe('/child2/test')
Expand Down

0 comments on commit 34271b8

Please sign in to comment.