Skip to content

Commit

Permalink
added/fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyfigaro authored and supasate committed Sep 23, 2018
1 parent b5de5a7 commit 285ccba
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 138 deletions.
98 changes: 69 additions & 29 deletions test/ConnectedRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import createConnectedRouter from '../src/ConnectedRouter'
import { onLocationChanged } from '../src/actions'
import plainStructure from '../src/structure/plain'
import immutableStructure from '../src/structure/immutable'
import seamlessImmutableStructure from '../src/structure/seamless-immutable'
import { connectRouter, ConnectedRouter } from '../src'

Enzyme.configure({ adapter: new Adapter() })
Expand All @@ -26,8 +27,8 @@ describe('ConnectedRouter', () => {

beforeEach(() => {
// Rewire `onLocationChanged` of `createConnectedRouter` to contain a spy function
onLocationChangedSpy = jest.fn(
(location, action) => onLocationChanged(location, action)
onLocationChangedSpy = jest.fn((location, action) =>
onLocationChanged(location, action)
)
createConnectedRouter.__Rewire__('onLocationChanged', onLocationChangedSpy)

Expand All @@ -38,18 +39,18 @@ describe('ConnectedRouter', () => {
props = {
action: 'POP',
location: {
pathname: '/path/to/somewhere',
pathname: '/path/to/somewhere'
},
history,
history
}

// Mock store
const mockStore = configureStore()
store = mockStore({
router: {
action: 'POP',
location: props.history.location,
},
location: props.history.location
}
})
})

Expand All @@ -74,14 +75,12 @@ describe('ConnectedRouter', () => {
</ContextWrapper>
)

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(1)
expect(onLocationChangedSpy.mock.calls).toHaveLength(1)

history.push('/new-location')
history.push('/new-location-2')

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(3)
expect(onLocationChangedSpy.mock.calls).toHaveLength(3)
})

it('unlistens the history object when unmounted.', () => {
Expand All @@ -93,20 +92,17 @@ describe('ConnectedRouter', () => {
</ContextWrapper>
)

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(1)
expect(onLocationChangedSpy.mock.calls).toHaveLength(1)

props.history.push('/new-location')

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(2)
expect(onLocationChangedSpy.mock.calls).toHaveLength(2)

wrapper.unmount()

history.push('/new-location-after-unmounted')

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(2)
expect(onLocationChangedSpy.mock.calls).toHaveLength(2)
})
})

Expand All @@ -126,14 +122,59 @@ describe('ConnectedRouter', () => {
</ContextWrapper>
)

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(1)
expect(onLocationChangedSpy.mock.calls).toHaveLength(1)

history.push('/new-location')
history.push('/new-location-2')

expect(onLocationChangedSpy.mock.calls).toHaveLength(3)
})

it('unlistens the history object when unmounted.', () => {
const wrapper = mount(
<ContextWrapper store={store}>
<ConnectedRouter {...props}>
<Route path="/" render={() => <div>Home</div>} />
</ConnectedRouter>
</ContextWrapper>
)

expect(onLocationChangedSpy.mock.calls).toHaveLength(1)

history.push('/new-location')

expect(onLocationChangedSpy.mock.calls).toHaveLength(2)

wrapper.unmount()

history.push('/new-location-after-unmounted')

expect(onLocationChangedSpy.mock.calls).toHaveLength(2)
})
})

describe('with seamless immutable structure', () => {
let ConnectedRouter

beforeEach(() => {
ConnectedRouter = createConnectedRouter(seamlessImmutableStructure)
})

it('calls `props.onLocationChanged()` when location changes.', () => {
mount(
<ContextWrapper store={store}>
<ConnectedRouter {...props}>
<Route path="/" render={() => <div>Home</div>} />
</ConnectedRouter>
</ContextWrapper>
)

expect(onLocationChangedSpy.mock.calls).toHaveLength(1)

history.push('/new-location')
history.push('/new-location-2')

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(3)
expect(onLocationChangedSpy.mock.calls).toHaveLength(3)
})

it('unlistens the history object when unmounted.', () => {
Expand All @@ -145,20 +186,17 @@ describe('ConnectedRouter', () => {
</ContextWrapper>
)

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(1)
expect(onLocationChangedSpy.mock.calls).toHaveLength(1)

history.push('/new-location')

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(2)
expect(onLocationChangedSpy.mock.calls).toHaveLength(2)

wrapper.unmount()

history.push('/new-location-after-unmounted')

expect(onLocationChangedSpy.mock.calls)
.toHaveLength(2)
expect(onLocationChangedSpy.mock.calls).toHaveLength(2)
})
})

Expand All @@ -171,7 +209,9 @@ describe('ConnectedRouter', () => {

// Create redux store with router state
store = createStore(
connectRouter(history)(combineReducers({ test: (state = 'test') => (state) })),
connectRouter(history)(
combineReducers({ test: (state = 'test') => state })
),
instrument()
)
devToolsStore = store.liftedStore
Expand Down Expand Up @@ -252,9 +292,9 @@ const storeShape = PropTypes.shape({

ContextWrapper.propTypes = {
store: storeShape.isRequired,
children: PropTypes.element.isRequired,
children: PropTypes.element.isRequired
}

ContextWrapper.childContextTypes = {
store: storeShape.isRequired,
store: storeShape.isRequired
}

0 comments on commit 285ccba

Please sign in to comment.