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

Accept objects in push and replace #141

Merged
merged 3 commits into from Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 78 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,44 @@ 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')

history.push({
...location,
pathname: '/foo'
})
},
function (location) {
expect(location.pathname).toEqual('/foo')
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 +136,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 +158,44 @@ 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')

history.replace({
...location,
pathname: '/foo'
})
},
function (location) {
expect(location.pathname).toEqual('/foo')
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
142 changes: 142 additions & 0 deletions modules/__tests__/describeQueries.js
Expand Up @@ -45,6 +45,48 @@ 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)

history.push({
...location,
query: { other: 'query value' },
state: { other: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?other=query+value')
expect(location.query).toEqual({ other: 'query value' })
Copy link
Member

Choose a reason for hiding this comment

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

I would expect this object to be { the: 'query value', other: 'query value' } if you want to keep with the existing behavior. I agree w you that nobody will actually want this though, and instead will just prefer to overwrite the existing query as this test appears to do. Just seems like you're introducing a breaking change here, which you said you were trying to avoid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's technically not a breaking change because you can't currently push an object at all, so there's no old behavior to break. The existing behavior covered by e.g. this test case: https://github.com/taion/history/blob/push-replace-location-object/modules/__tests__/describeQueries.js#L178-L184 still works.

I think for a 2.x release, we should always overwrite the query search and change that linked test case above. I dunno. The workaround I have right now is quite ugly, but I'd like to minimize the number of major version bumps.

expect(location.state).toEqual({ other: '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 +112,48 @@ 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)

history.replace({
...location,
query: { other: 'query value' },
state: { other: 'state' }
})
},
function (location) {
expect(location.pathname).toEqual('/home')
expect(location.search).toEqual('?other=query+value')
expect(location.query).toEqual({ other: 'query value' })
expect(location.state).toEqual({ other: '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 +236,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 +290,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