Skip to content

Commit

Permalink
Accept objects in push and replace
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Nov 19, 2015
1 parent 4125bff commit 563b9eb
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 64 deletions.
56 changes: 54 additions & 2 deletions modules/__tests__/describeBasename.js
Expand Up @@ -48,7 +48,7 @@ function describeBasename(createHistory) {
})

describe('in push', function () {
it('works', function (done) {
it('works with string', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
Expand All @@ -70,6 +70,32 @@ function describeBasename(createHistory) {

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

it('works with object', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.push({
pathname: '/home',
state: { the: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(PUSH)
expect(location.basename).toEqual('/base/url')
}
]

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

describe('in replaceState', function () {
Expand Down Expand Up @@ -98,7 +124,7 @@ function describeBasename(createHistory) {
})

describe('in replace', function () {
it('works', function (done) {
it('works with string', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
Expand All @@ -120,6 +146,32 @@ function describeBasename(createHistory) {

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

it('works with object', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)
expect(location.basename).toEqual('')

history.replace({
pathname: '/home',
state: { the: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(REPLACE)
expect(location.basename).toEqual('/base/url')
}
]

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

describe('in createPath', function () {
Expand Down
97 changes: 78 additions & 19 deletions modules/__tests__/describePush.js
Expand Up @@ -15,25 +15,84 @@ function describePush(createHistory) {
unlisten()
})

it('calls change listeners with the new location', 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('/home?the=query')
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual(null)
expect(location.action).toEqual(PUSH)
}
]

unlisten = history.listen(execSteps(steps, done))
describe('with a path string', function () {
it('calls change listeners with the new location', 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('/home?the=query')
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual(null)
expect(location.action).toEqual(PUSH)
}
]

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

describe('with a path object', function () {
it('calls change listeners with the new location', 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)
}
]

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

it('correctly merges with old location', function (done) {
let oldLocation

let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

oldLocation = location

history.push({
...location,
search: '?the=query',
state: { the: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual(oldLocation.pathname)
expect(location.search).toEqual('?the=query')
expect(location.state).toEqual({ the: 'state' })
expect(location.action).toEqual(PUSH)
expect(location.key).toNotEqual(oldLocation.key)
}
]

unlisten = history.listen(execSteps(steps, done))
})
})
})
}
Expand Down
116 changes: 116 additions & 0 deletions modules/__tests__/describeQueries.js
Expand Up @@ -45,6 +45,35 @@ function describeQueries(createHistory) {
})
})

describe('in push', function () {
it('works', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.query).toEqual({})
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

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

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

describe('in replaceState', function () {
it('works', function (done) {
let steps = [
Expand All @@ -70,6 +99,35 @@ function describeQueries(createHistory) {
})
})

describe('in replace', function () {
it('works', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.query).toEqual({})
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

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

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

describe('in createPath', function () {
it('works', function () {
expect(
Expand Down Expand Up @@ -152,6 +210,35 @@ function describeQueries(createHistory) {
})
})

describe('in push', function () {
it('works', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.query).toEqual('PARSE_QUERY_STRING')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

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

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

describe('in replaceState', function () {
it('works', function (done) {
let steps = [
Expand All @@ -177,6 +264,35 @@ function describeQueries(createHistory) {
})
})

describe('in replace', function () {
it('works', function (done) {
let steps = [
function (location) {
expect(location.pathname).toEqual('/')
expect(location.search).toEqual('')
expect(location.query).toEqual('PARSE_QUERY_STRING')
expect(location.state).toEqual(null)
expect(location.action).toEqual(POP)

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

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

describe('in createPath', function () {
it('works', function () {
expect(
Expand Down

0 comments on commit 563b9eb

Please sign in to comment.