Skip to content

Commit a3d336f

Browse files
committed
Merge pull request #2138 from unh/master
Add isActive test with a custom parse/stringify function
2 parents a6d4ee6 + 0135eea commit a3d336f

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

modules/__tests__/isActive-test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import createHistory from 'history/lib/createMemoryHistory'
55
import IndexRoute from '../IndexRoute'
66
import Router from '../Router'
77
import Route from '../Route'
8+
import qs from 'qs'
89

910
describe('isActive', function () {
1011

@@ -200,4 +201,61 @@ describe('isActive', function () {
200201
})
201202
})
202203

204+
describe('a pathname that matches URL', function () {
205+
describe('with query that does match', function () {
206+
it('is active', function (done) {
207+
React.render((
208+
<Router history={createHistory('/home?foo[]=bar&foo[]=bar1&foo[]=bar2')}>
209+
<Route path="/" />
210+
<Route path="/home" />
211+
</Router>
212+
), node, function () {
213+
expect(this.history.isActive('/home', { foo: [ 'bar', 'bar1', 'bar2' ] })).toBe(true)
214+
done()
215+
})
216+
})
217+
})
218+
219+
describe('with a custom parse function and a query that does not match', function () {
220+
it('is not active', function (done) {
221+
function stringifyQuery(params) {
222+
return qs.stringify(params, { arrayFormat: 'indices' })
223+
}
224+
function parseQueryString(query) {
225+
return qs.parse(query, { parseArrays: false })
226+
}
227+
228+
React.render((
229+
<Router history={createHistory('/home?foo[1]=bar')} stringifyQuery={stringifyQuery} parseQueryString={parseQueryString}>
230+
<Route path="/" />
231+
<Route path="/home" />
232+
</Router>
233+
), node, function () {
234+
expect(this.history.isActive('/home', { foo: { 4: 'bar' } })).toBe(false)
235+
done()
236+
})
237+
})
238+
})
239+
240+
describe('with a custom parse function and a query that match', function () {
241+
it('is active', function (done) {
242+
function stringifyQuery(params) {
243+
return qs.stringify(params, { arrayFormat: 'indices' })
244+
}
245+
function parseQueryString(query) {
246+
return qs.parse(query, { parseArrays: false })
247+
}
248+
249+
React.render((
250+
<Router history={createHistory('/home?foo[4]=bar&foo[1]=bar2')} stringifyQuery={stringifyQuery} parseQueryString={parseQueryString}>
251+
<Route path="/" />
252+
<Route path="/home" />
253+
</Router>
254+
), node, function () {
255+
expect(this.history.isActive('/home', { foo: { 1: 'bar2', 4: 'bar' } })).toBe(true)
256+
done()
257+
})
258+
})
259+
})
260+
})
203261
})

modules/isActive.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { matchPattern } from './PatternUtils'
2-
2+
import deepEqual from 'deep-equal'
33
/**
44
* Returns true if the given pathname matches the active pathname.
55
*/
@@ -64,11 +64,7 @@ function queryIsActive(query, activeQuery) {
6464
if (query == null)
6565
return true
6666

67-
for (const p in query)
68-
if (query.hasOwnProperty(p) && String(query[p]) !== String(activeQuery[p]))
69-
return false
70-
71-
return true
67+
return deepEqual(query, activeQuery)
7268
}
7369

7470
/**

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"license": "MIT",
2727
"dependencies": {
28+
"deep-equal": "^1.0.0",
2829
"history": "1.12.1",
2930
"invariant": "^2.0.0",
3031
"warning": "^2.0.0"
@@ -51,6 +52,7 @@
5152
"karma-sourcemap-loader": "^0.3.5",
5253
"karma-webpack": "^1.7.0",
5354
"mocha": "^2.0.1",
55+
"qs": "^4.0.0",
5456
"react": "0.13.x",
5557
"rf-changelog": "^0.4.0",
5658
"style-loader": "^0.12.4",

0 commit comments

Comments
 (0)