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

Commit

Permalink
Don't leave test side effects with history singltons.
Browse files Browse the repository at this point in the history
  • Loading branch information
timdorr committed Feb 8, 2016
1 parent 0b7b322 commit 9b2dc36
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"eslint-config-rackt": "^1.1.1",
"eslint-plugin-react": "^3.15.0",
"expect": "^1.13.0",
"history": "^2.0.0",
"isparta": "^4.0.0",
"isparta-loader": "^2.0.0",
"karma": "^0.13.3",
Expand All @@ -63,7 +64,6 @@
"karma-webpack": "^1.7.0",
"mocha": "^2.3.4",
"react": "^0.14.3",
"react-router": "^2.0.0-rc5",
"redux": "^3.0.4",
"redux-devtools": "^3.0.0",
"redux-devtools-dock-monitor": "^1.0.1",
Expand Down
25 changes: 12 additions & 13 deletions test/_createSyncTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ expect.extend({
search = '',
hash = '',
state = null,
query = {},
query,
action = 'PUSH'
}) {
const { locationBeforeTransitions } = this.actual.getState().routing
Expand All @@ -28,19 +28,19 @@ expect.extend({
})


function createSyncedHistoryAndStore(testHistory) {
function createSyncedHistoryAndStore(originalHistory) {

const store = createStore(combineReducers({
routing: routerReducer
}))
const history = syncHistoryWithStore(testHistory, store)
const history = syncHistoryWithStore(originalHistory, store)

return { history, store }
}

const defaultReset = () => {}

export default function createTests(testHistory, name, reset = defaultReset) {
export default function createTests(createHistory, name, reset = defaultReset) {
describe(name, () => {

beforeEach(reset)
Expand All @@ -49,7 +49,7 @@ export default function createTests(testHistory, name, reset = defaultReset) {
let history, store

beforeEach(() => {
let synced = createSyncedHistoryAndStore(testHistory)
let synced = createSyncedHistoryAndStore(createHistory())
history = synced.history
store = synced.store
})
Expand All @@ -58,7 +58,7 @@ export default function createTests(testHistory, name, reset = defaultReset) {
history.unsubscribe()
})

it('syncs router -> redux', () => {
it('syncs history -> redux', () => {
expect(store).toContainLocation({
pathname: '/',
action: 'POP'
Expand Down Expand Up @@ -91,8 +91,7 @@ export default function createTests(testHistory, name, reset = defaultReset) {
history.push('/bar?query=1')
expect(store).toContainLocation({
pathname: '/bar',
search: '?query=1',
query: { query: '1' }
search: '?query=1'
})

history.push('/bar#baz')
Expand All @@ -109,7 +108,6 @@ export default function createTests(testHistory, name, reset = defaultReset) {
expect(store).toContainLocation({
pathname: '/bar',
search: '?query=1',
query: { query: '1' },
state: { bar: 'baz' },
action: 'REPLACE'
})
Expand All @@ -123,7 +121,6 @@ export default function createTests(testHistory, name, reset = defaultReset) {
expect(store).toContainLocation({
pathname: '/bar',
search: '?query=1',
query: { query: '1' },
hash: '#hash=2',
state: { bar: 'baz' },
action: 'REPLACE'
Expand Down Expand Up @@ -163,11 +160,13 @@ export default function createTests(testHistory, name, reset = defaultReset) {
})

describe('Redux DevTools', () => {
let history, store, devToolsStore
let originalHistory, history, store, devToolsStore

beforeEach(() => {
originalHistory = createHistory()

// Set initial URL before syncing
testHistory.push('/foo')
originalHistory.push('/foo')

store = createStore(
combineReducers({
Expand All @@ -177,7 +176,7 @@ export default function createTests(testHistory, name, reset = defaultReset) {
)
devToolsStore = store.liftedStore

history = syncHistoryWithStore(testHistory, store)
history = syncHistoryWithStore(originalHistory, store)
})

afterEach(() => {
Expand Down
6 changes: 3 additions & 3 deletions test/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'babel-polyfill'

import { hashHistory, browserHistory } from 'react-router'
import { createHashHistory, createHistory } from 'history'
import createTests from '../_createSyncTest'

createTests(hashHistory, 'Hash History', () => window.location = '#/')
createTests(browserHistory, 'Browser History', () => window.history.replaceState(null, null, '/'))
createTests(createHashHistory, 'Hash History', () => window.location = '#/')
createTests(createHistory, 'Browser History', () => window.history.replaceState(null, null, '/'))
4 changes: 2 additions & 2 deletions test/sync.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMemoryHistory } from 'react-router'
import { createMemoryHistory } from 'history'
import createTests from './_createSyncTest'

createTests(createMemoryHistory(), 'Memory History')
createTests(createMemoryHistory, 'Memory History')

0 comments on commit 9b2dc36

Please sign in to comment.