Skip to content

Commit

Permalink
Use forced REPLACE only when both location and state are the same
Browse files Browse the repository at this point in the history
- Update tests to include check for changing state
- URL stays the same, but state changes.
  • Loading branch information
Macroz committed Jan 3, 2016
1 parent c5cb8ef commit 564197a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
39 changes: 38 additions & 1 deletion modules/__tests__/describePush.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,43 @@ function describePush(createHistory) {
})

it('becomes a REPLACE if path is unchanged', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

history.push({
pathname: '/home',
search: '?the=query',
state: { the: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(PUSH)

history.push({
pathname: '/home',
search: '?the=query',
state: { the: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(REPLACE)
}
]

unlisten = history.listen(execSteps(steps, done))
})

it('stays PUSH if state is changed', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
Expand Down Expand Up @@ -123,7 +160,7 @@ function describePush(createHistory) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ different: 'state' })
expect(location.action).toEqual(REPLACE)
expect(location.action).toEqual(PUSH)
}
]

Expand Down
31 changes: 30 additions & 1 deletion modules/__tests__/describePushState.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ function describePushState(createHistory) {
})

it('becomes a REPLACE if path is unchanged', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

history.pushState({ the: 'state' }, '/home?the=query')
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(PUSH)

history.pushState({ the: 'state' }, '/home?the=query')
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(REPLACE)
}
]

unlisten = history.listen(execSteps(steps, done))
})

it('stays PUSH if state is changed', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
Expand All @@ -57,7 +86,7 @@ function describePushState(createHistory) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ different: 'state' })
expect(location.action).toEqual(REPLACE)
expect(location.action).toEqual(PUSH)
}
]

Expand Down
2 changes: 1 addition & 1 deletion modules/createHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function createHistory(options={}) {
const prevPath = createPath(location)
const nextPath = createPath(nextLocation)

if (nextPath === prevPath)
if (nextPath === prevPath && deepEqual(location.state, nextLocation.state))
nextLocation.action = REPLACE
}

Expand Down

0 comments on commit 564197a

Please sign in to comment.