Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added condition property to do extra tests on route params, etc. #3098

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions modules/__tests__/Router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,76 @@ describe('Router', function () {

})


describe('conditions', function () {
it('matches route with condition match', function (done) {
class MyComponent extends Component {
render() {
return <div>{this.props.params.fruit}</div>
}
}


let conditionCalled = false
function condition(match, callback) {
conditionCalled = true
callback(null, true)
}

render((
<Router history={createHistory('/apple')}>
<Route path=":fruit" component={MyComponent} condition={condition} />
</Router>
), node, function () {
expect(node.textContent).toEqual('apple')
expect(conditionCalled).toEqual(true)
done()
})
})


it('calls condition on both parent and child routes', function (done) {
class Parent extends Component {
render() {
return <div>{this.props.children}</div>
}
}

class Child extends Component {
render() {
return <h1>{this.props.params.name}</h1>
}
}


let conditionParentCalled = false
function conditionParent(match, callback) {
conditionParentCalled = true
callback(null, true)
}

let conditionChildCalled = false
function conditionChild(match, callback) {
conditionChildCalled = true
callback(null, true)
}

render((
<Router history={createHistory('/apple')}>
<Route component={Parent} condition={conditionParent}>
<Route path="/:name" component={Child} condition={conditionChild} />
</Route>
</Router>
), node, function () {
expect(node.textContent).toEqual('apple')
expect(conditionParentCalled).toEqual(true)
expect(conditionChildCalled).toEqual(true)
done()
})
})

})

describe('render prop', function () {
it('renders with the render prop', function (done) {
render((
Expand Down
101 changes: 101 additions & 0 deletions modules/__tests__/matchRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,107 @@ describe('matchRoutes', function () {
})
})

describe('routes with conditions', function () {
it('matches single route', function (done) {
let Condition, conditionCalled = 0

const routes = [
Condition = {
path: '/',
condition: (match, callback) => {
conditionCalled++
callback(null, true)
}
}
]

matchRoutes(routes, createLocation('/'), function (error, match) {
expect(match).toExist()
expect(match.routes).toEqual([ Condition ])
expect(conditionCalled).toEqual(1)
done()
})
})

it('does not match single route', function (done) {
let conditionCalled = 0

const routes = [ {
path: '/',
condition: (match, callback) => {
conditionCalled++
callback(null, false)
}
} ]

matchRoutes(routes, createLocation('/'), function (error, match) {
expect(match).toNotExist()
expect(conditionCalled).toEqual(1)
done()
})
})

it('matches nested routes', function (done) {
let Parent, Child, parentConditionCalled = 0, childConditionCalled = 0

const routes = [
Parent = {
condition: (match, callback) => {
parentConditionCalled++
callback(null, true)
},
childRoutes: [
Child = {
path: '/',
condition: (match, callback) => {
childConditionCalled++
callback(null, true)
}
}
]
}
]

matchRoutes(routes, createLocation('/'), function (error, match) {
expect(match).toExist()
expect(match.routes).toEqual([ Parent, Child ])
expect(parentConditionCalled).toEqual(1)
expect(childConditionCalled).toEqual(1)
done()
})
})

it('does not match nested routes on parent condition failure', function (done) {
let parentConditionCalled = 0, childConditionCalled = 0

const routes = [
{
path: '/:food/',
condition: (match, callback) => {
parentConditionCalled++
callback(null, false)
},
childRoutes: [
{
path: ':color',
condition: (match, callback) => {
childConditionCalled++
callback(null, true)
}
}
]
}
]

matchRoutes(routes, createLocation('/apple/orange'), function (error, match) {
expect(match).toNotExist()
expect(parentConditionCalled).toEqual(1)
expect(childConditionCalled).toEqual(0)
done()
})
})
})

describe('invalid route configs', function () {
let invalidRoutes, errorSpy

Expand Down
59 changes: 58 additions & 1 deletion modules/__tests__/serverRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ describe('server rendering', function () {
}
}

class Category extends Component {
render() {
return (
<div className="Category">
<h1>Category</h1>
<div>{this.props.params.category}</div>
</div>
)
}
}

class Post extends Component {
render() {
return (
<div className="Post">
<h1>Post</h1>
<div>{this.props.params.slug}</div>
</div>
)
}
}

const DashboardRoute = {
path: '/dashboard',
component: Dashboard
Expand All @@ -80,10 +102,23 @@ describe('server rendering', function () {
}
}

const CategoryRoute = {
path: '/food/:category',
condition(params, callback) {
callback(null, [ 'baking', 'bbq' ].indexOf(params.category) > -1)
},
component: Category
}

const PostRoute = {
path: '/food/:slug',
component: Post
}

const routes = {
path: '/',
component: App,
childRoutes: [ DashboardRoute, AboutRoute, RedirectRoute, AsyncRoute ]
childRoutes: [ DashboardRoute, AboutRoute, RedirectRoute, AsyncRoute, CategoryRoute, PostRoute ]
}

it('works for synchronous route', function (done) {
Expand Down Expand Up @@ -200,4 +235,26 @@ describe('server rendering', function () {
})
})
})

it('condition matches category route', function (done) {
match({ routes, location: '/food/bbq' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
)
expect(string).toMatch(/Category/)
expect(string).toMatch(/bbq/)
done()
})
})

it('condition does not match and moves to next mached route', function (done) {
match({ routes, location: '/food/pizza-hut' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
)
expect(string).toMatch(/Post/)
expect(string).toMatch(/pizza-hut/)
done()
})
})
})
Loading