From c5eebfcc55b4fd18916fa47d4be2f4fd2dec8eb3 Mon Sep 17 00:00:00 2001 From: Supasate Choochaisri Date: Fri, 28 Dec 2018 00:37:44 +0700 Subject: [PATCH] Add double-rendering test for immutable and seamless-immutable --- test/ConnectedRouter.test.js | 114 ++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/test/ConnectedRouter.test.js b/test/ConnectedRouter.test.js index a2dabd8d..af05a0b5 100644 --- a/test/ConnectedRouter.test.js +++ b/test/ConnectedRouter.test.js @@ -178,7 +178,7 @@ describe('ConnectedRouter', () => { history.push('/new-location') expect(renderCount).toBe(2) }) - }) + }) describe('with immutable structure', () => { let ConnectedRouter @@ -243,6 +243,62 @@ describe('ConnectedRouter', () => { expect(onLocationChangedSpy.mock.calls).toHaveLength(3) }) + + it('only renders one time when mounted', () => { + let renderCount = 0 + + const RenderCounter = () => { + renderCount++ + return null + } + + mount( + + + + + + ) + + expect(renderCount).toBe(1) + }) + + it('does not render again when non-related action is fired', () => { + // Initialize the render counter variable + let renderCount = 0 + + // Create redux store with router state + store = createStore( + combineReducers({ + incrementReducer: (state = 0, action = {}) => { + if (action.type === 'testAction') + return ++state + + return state + }, + router: connectRouter(history) + }), + compose(applyMiddleware(routerMiddleware(history))) + ) + + + const RenderCounter = () => { + renderCount++ + return null + } + + mount( + + + + + + ) + + store.dispatch({ type: 'testAction' }) + history.push('/new-location') + expect(renderCount).toBe(2) + }) }) describe('with seamless immutable structure', () => { @@ -290,6 +346,62 @@ describe('ConnectedRouter', () => { expect(onLocationChangedSpy.mock.calls).toHaveLength(2) }) + + it('only renders one time when mounted', () => { + let renderCount = 0 + + const RenderCounter = () => { + renderCount++ + return null + } + + mount( + + + + + + ) + + expect(renderCount).toBe(1) + }) + + it('does not render again when non-related action is fired', () => { + // Initialize the render counter variable + let renderCount = 0 + + // Create redux store with router state + store = createStore( + combineReducers({ + incrementReducer: (state = 0, action = {}) => { + if (action.type === 'testAction') + return ++state + + return state + }, + router: connectRouter(history) + }), + compose(applyMiddleware(routerMiddleware(history))) + ) + + + const RenderCounter = () => { + renderCount++ + return null + } + + mount( + + + + + + ) + + store.dispatch({ type: 'testAction' }) + history.push('/new-location') + expect(renderCount).toBe(2) + }) }) describe('Redux DevTools', () => {