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

Add history.navigate API #472

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions modules/LocationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ export const locationsAreEqual = (a, b) =>
a.hash === b.hash &&
a.key === b.key &&
valueEqual(a.state, b.state)

export const locationPathsAreEqual = (a, b) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names are hard 🤷‍♂️

a.pathname === b.pathname &&
a.search === b.search &&
a.hash === b.hash
18 changes: 18 additions & 0 deletions modules/__tests__/BrowserHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ describeHistory('a browser history', () => {
})
})

describe('navigate to new location', () => {
it('pushes new location', (done) => {
TestSequences.NavigateNewLocation(history, done)
})
})

describe('navigate to same location', () => {
it('replaces instead of pushes', (done) => {
TestSequences.NavigateSameLocation(history, done)
})
})

describe('navigate to same location with different state', () => {
it('replaces intead of pushes', (done) => {
TestSequences.NavigateSameLocationDifferentState(history, done)
})
})

describe('push a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.PushNewLocation(history, done)
Expand Down
18 changes: 18 additions & 0 deletions modules/__tests__/HashHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ describeHistory('a hash history', () => {
})
})

describe('navigate to new location', () => {
it('pushes new location', (done) => {
TestSequences.NavigateNewLocation(history, done)
})
})

describe('navigate to same location', () => {
it('replaces instead of pushes', (done) => {
TestSequences.NavigateSameLocation(history, done)
})
})

describe('navigate to same location with different state', () => {
it('replaces intead of pushes', (done) => {
TestSequences.NavigateSameLocationDifferentState(history, done)
})
})

describe('push a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.PushNewLocation(history, done)
Expand Down
18 changes: 18 additions & 0 deletions modules/__tests__/MemoryHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ describe('a memory history', () => {
})
})

describe('navigate to new location', () => {
it('pushes new location', (done) => {
TestSequences.NavigateNewLocation(history, done)
})
})

describe('navigate to same location', () => {
it('replaces instead of pushes', (done) => {
TestSequences.NavigateSameLocation(history, done)
})
})

describe('navigate to same location with different state', () => {
it('replaces intead of pushes', (done) => {
TestSequences.NavigateSameLocationDifferentState(history, done)
})
})

describe('push a new path', () => {
it('calls change listeners with the new location', (done) => {
TestSequences.PushNewLocation(history, done)
Expand Down
32 changes: 32 additions & 0 deletions modules/__tests__/TestSequences/NavigateNewLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
(location) => {
expect(location).toMatch({
pathname: '/'
})

history.navigate('/home#room')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/home',
hash: '#room'
})

history.navigate('/home#stead')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/home',
hash: '#stead'
})
}
]

execSteps(steps, history, done)
}
49 changes: 49 additions & 0 deletions modules/__tests__/TestSequences/NavigateSameLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
(location) => {
expect(location).toMatch({
pathname: '/'
})

history.navigate('/home#room')
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/home',
hash: '#room'
})

history.navigate('/home#room')
},
(location, action) => {
expect(action).toBe('REPLACE')
expect(location).toMatch({
pathname: '/home',
hash: '#room'
})

history.navigate({ pathname: '/home', hash: '#room' })
},
(location, action) => {
expect(action).toBe('REPLACE')
expect(location).toMatch({
pathname: '/home',
hash: '#room'
})

history.goBack()
},
(location, action) => {
expect(action).toBe('POP')
expect(location).toMatch({
pathname: '/'
})
}
]

execSteps(steps, history, done)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import expect from 'expect'
import execSteps from './execSteps'

export default (history, done) => {
const steps = [
(location) => {
expect(location).toMatch({
pathname: '/'
})

history.navigate({
pathname: '/home',
state: { 'push': true }
})
},
(location, action) => {
expect(action).toBe('PUSH')
expect(location).toMatch({
pathname: '/home',
state: { 'push': true }
})

history.navigate({
pathname: '/home',
state: { 'push': false }
})
},
(location, action) => {
expect(action).toBe('REPLACE')
expect(location).toMatch({
pathname: '/home',
state: { 'push': false }
})
}
]

execSteps(steps, history, done)
}
3 changes: 3 additions & 0 deletions modules/__tests__/TestSequences/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export HashChangeTransitionHook from './HashChangeTransitionHook'
export InitialLocationNoKey from './InitialLocationNoKey'
export InitialLocationHasKey from './InitialLocationHasKey'
export Listen from './Listen'
export NavigateNewLocation from './NavigateNewLocation'
export NavigateSameLocation from './NavigateSameLocation'
export NavigateSameLocationDifferentState from './NavigateSameLocationDifferentState'
export NoslashHashPathCoding from './NoslashHashPathCoding'
export PushEncodedLocation from './PushEncodedLocation'
export PushNewLocation from './PushNewLocation'
Expand Down
97 changes: 66 additions & 31 deletions modules/createBrowserHistory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warning from 'warning'
import invariant from 'invariant'
import { createLocation } from './LocationUtils'
import { createLocation, locationPathsAreEqual } from './LocationUtils'
import {
addLeadingSlash,
stripTrailingSlash,
Expand Down Expand Up @@ -146,22 +146,8 @@ const createBrowserHistory = (props = {}) => {
const initialLocation = getDOMLocation(getHistoryState())
let allKeys = [ initialLocation.key ]

// Public interface

const createHref = (location) =>
basename + createPath(location)

const push = (path, state) => {
warning(
!(typeof path === 'object' && path.state !== undefined && state !== undefined),
'You should avoid providing a 2nd state argument to push when the 1st ' +
'argument is a location-like object that already has state; it is ignored'
)

const action = 'PUSH'
const location = createLocation(path, state, createKey(), history.location)

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
const createPushCallback = (location, action) =>
(ok) => {
if (!ok)
return

Expand Down Expand Up @@ -190,20 +176,10 @@ const createBrowserHistory = (props = {}) => {

window.location.href = href
}
})
}

const replace = (path, state) => {
warning(
!(typeof path === 'object' && path.state !== undefined && state !== undefined),
'You should avoid providing a 2nd state argument to replace when the 1st ' +
'argument is a location-like object that already has state; it is ignored'
)

const action = 'REPLACE'
const location = createLocation(path, state, createKey(), history.location)
}

transitionManager.confirmTransitionTo(location, action, getUserConfirmation, (ok) => {
const createReplaceCallback = (location, action) =>
(ok) => {
if (!ok)
return

Expand Down Expand Up @@ -231,7 +207,65 @@ const createBrowserHistory = (props = {}) => {

window.location.replace(href)
}
})
}

// Public interface

const createHref = (location) =>
basename + createPath(location)


const push = (path, state) => {
warning(
!(typeof path === 'object' && path.state !== undefined && state !== undefined),
'You should avoid providing a 2nd state argument to push when the 1st ' +
'argument is a location-like object that already has state; it is ignored'
)

const location = createLocation(path, state, createKey(), history.location)
const action = 'PUSH'
transitionManager.confirmTransitionTo(
location,
action,
getUserConfirmation,
createPushCallback(location, action)
)
}

const replace = (path, state) => {
warning(
!(typeof path === 'object' && path.state !== undefined && state !== undefined),
'You should avoid providing a 2nd state argument to replace when the 1st ' +
'argument is a location-like object that already has state; it is ignored'
)

const location = createLocation(path, state, createKey(), history.location)
const action = 'REPLACE'
transitionManager.confirmTransitionTo(
location,
action,
getUserConfirmation,
createReplaceCallback(location, action)
)
}

const navigate = (path, state) => {
warning(
!(typeof path === 'object' && path.state !== undefined && state !== undefined),
'You should avoid providing a 2nd state argument to navigate when the 1st ' +
'argument is a location-like object that already has state; it is ignored'
)
const location = createLocation(path, state, createKey(), history.location)
const samePath = locationPathsAreEqual(location, history.location)
const action = samePath ? 'REPLACE' : 'PUSH'
const callback = samePath ? createReplaceCallback(location, action) : createPushCallback(location, action)

transitionManager.confirmTransitionTo(
location,
action,
getUserConfirmation,
callback
)
}

const go = (n) => {
Expand Down Expand Up @@ -297,6 +331,7 @@ const createBrowserHistory = (props = {}) => {
action: 'POP',
location: initialLocation,
createHref,
navigate,
push,
replace,
go,
Expand Down
Loading