Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Rely less on changeId in tests #112

Merged
merged 2 commits into from
Dec 11, 2015
Merged
Changes from all commits
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
199 changes: 53 additions & 146 deletions test/createTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,39 +357,21 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
})

it('updates the router even if path is the same', () => {
expect(store).toContainRoute({
path: '/',
changeId: 1,
replace: false,
state: undefined
const updates = []
const historyUnsubscribe = history.listen(location => {
updates.push(location.pathname);
})

store.dispatch(pushPath('/foo'))
expect(store).toContainRoute({
path: '/foo',
changeId: 2,
replace: false,
state: undefined
})

store.dispatch(pushPath('/foo'))
expect(store).toContainRoute({
path: '/foo',
changeId: 3,
replace: false,
state: undefined
})

store.dispatch(replacePath('/foo'))
expect(store).toContainRoute({
path: '/foo',
changeId: 4,
replace: true,
state: undefined
})

expect(updates).toEqual(['/', '/foo', '/foo', '/foo']);
})

it('does not update the router for other state changes', () => {
const state = store.getState();

store.dispatch({
type: 'RANDOM_ACTION',
payload: {
Expand All @@ -399,22 +381,10 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
}
})

expect(store).toContainRoute({
path: '/',
changeId: 1,
replace: false,
state: undefined
})
expect(state).toEqual(store.getState());
})

it('only updates the router once when dispatching from `listenBefore`', () => {
expect(store).toContainRoute({
path: '/',
changeId: 1,
replace: false,
state: undefined
})

history.listenBefore(location => {
expect(location.pathname).toEqual('/foo')
store.dispatch({
Expand All @@ -427,125 +397,44 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
})
})

store.dispatch(pushPath('/foo'))
expect(store).toContainRoute({
path: '/foo',
changeId: 2,
replace: false,
state: undefined
})
})

it('does not unnecessarily update the store', () => {
const updates = []

const unsubscribeFromStore = store.subscribe(() => {
updates.push(store.getState())
})
const updates = [];
history.listen(location => {
updates.push(location.pathname);
});

store.dispatch(pushPath('/foo'))
store.dispatch(pushPath('/foo'))
store.dispatch(pushPath('/foo', { bar: 'baz' }))
history.pushState({ foo: 'bar' }, '/foo')
store.dispatch(replacePath('/bar'))
store.dispatch(replacePath('/bar', { bar: 'foo' }))

unsubscribeFromStore()

expect(updates.length).toBe(6)
expect(updates).toEqual([
{
routing: {
changeId: 2,
path: '/foo',
state: undefined,
replace: false
}
},
{
routing: {
changeId: 3,
path: '/foo',
state: undefined,
replace: false
}
},
{
routing: {
changeId: 4,
path: '/foo',
state: { bar: 'baz' },
replace: false
}
},
{
routing: {
changeId: 4,
path: '/foo',
state: { foo: 'bar' },
replace: true
}
},
{
routing: {
changeId: 5,
path: '/bar',
state: undefined,
replace: true
}
},
{
routing: {
changeId: 6,
path: '/bar',
state: { bar: 'foo' },
replace: true
}
}
])
expect(updates).toEqual(['/', '/foo'])
})

it('allows updating the route from within `listenBefore`', () => {
expect(store).toContainRoute({
path: '/'
})

history.listenBefore(location => {
if(location.pathname === '/foo') {
expect(store).toContainRoute({
path: '/foo',
changeId: 2,
replace: false,
state: undefined
})
store.dispatch(pushPath('/bar'))
}
else if(location.pathname === '/replace') {
expect(store).toContainRoute({
path: '/replace',
changeId: 4,
replace: false,
state: { bar: 'baz' }
})
store.dispatch(replacePath('/baz', { foo: 'bar' }))
}
})

const updates = [];
history.listen(location => {
updates.push(location.pathname);
});

store.dispatch(pushPath('/foo'))
expect(store).toContainRoute({
path: '/bar',
changeId: 3,
replace: false,
state: undefined
path: '/bar'
})

store.dispatch(pushPath('/replace', { bar: 'baz' }))
expect(store).toContainRoute({
path: '/baz',
changeId: 5,
replace: true,
state: { foo: 'bar' }
state: { foo: 'bar' },
replace: true
})

expect(updates).toEqual(['/', '/bar', '/baz']);
})

it('throws if "routing" key is missing with default selectRouteState', () => {
Expand Down Expand Up @@ -582,10 +471,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)

store.dispatch(pushPath('/bar'))
expect(store).toContainRoute({
path: '/bar',
changeId: 2,
replace: false,
state: undefined
path: '/bar'
})

unsubscribe()
Expand All @@ -602,6 +488,33 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
() => store.dispatch(pushPath('/foo'))
).toNotThrow()
})

it('only triggers history once when updating path via store', () => {
const updates = []
const historyUnsubscribe = history.listen(location => {
updates.push(location.pathname);
})

store.dispatch(pushPath('/bar'));
store.dispatch(pushPath('/baz'));
expect(updates).toEqual(['/', '/bar', '/baz'])

historyUnsubscribe()
})

it('only triggers store once when updating path via store', () => {
const updates = []
const storeUnsubscribe = store.subscribe(() => {
updates.push(store.getState().routing.path);
})

store.dispatch(pushPath('/bar'));
store.dispatch(pushPath('/baz'));
store.dispatch(replacePath('/foo'));
expect(updates).toEqual(['/bar', '/baz', '/foo'])

storeUnsubscribe()
})
})

it('handles basename history option', () => {
Expand All @@ -613,18 +526,12 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)

store.dispatch(pushPath('/bar'))
expect(store).toContainRoute({
path: '/bar',
changeId: 2,
replace: false,
state: undefined
path: '/bar'
})

history.pushState(undefined, '/baz');
expect(store).toContainRoute({
path: '/baz',
changeId: 2,
replace: false,
state: undefined
path: '/baz'
})
})
})
Expand Down