From 564197a26a0131edbbebbebe69ec9b57f862cac1 Mon Sep 17 00:00:00 2001 From: Markku Rontu Date: Mon, 7 Dec 2015 16:22:19 +0200 Subject: [PATCH] Use forced REPLACE only when both location and state are the same - Update tests to include check for changing state - URL stays the same, but state changes. --- modules/__tests__/describePush.js | 39 +++++++++++++++++++++++++- modules/__tests__/describePushState.js | 31 +++++++++++++++++++- modules/createHistory.js | 2 +- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/modules/__tests__/describePush.js b/modules/__tests__/describePush.js index 7dfb6bfa8..c447ceacd 100644 --- a/modules/__tests__/describePush.js +++ b/modules/__tests__/describePush.js @@ -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('/') @@ -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) } ] diff --git a/modules/__tests__/describePushState.js b/modules/__tests__/describePushState.js index 99b15973c..4beb4571d 100644 --- a/modules/__tests__/describePushState.js +++ b/modules/__tests__/describePushState.js @@ -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('/') @@ -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) } ] diff --git a/modules/createHistory.js b/modules/createHistory.js index 2998993dc..225c9c454 100644 --- a/modules/createHistory.js +++ b/modules/createHistory.js @@ -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 }