Guest User

Untitled

a guest
Dec 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. import Enzyme from 'enzyme'
  2. import Adapter from 'enzyme-adapter-react-16'
  3. import React from 'react'
  4. import { Route, Switch } from 'react-router'
  5. import { Provider } from 'react-redux'
  6. import { createStore, combineReducers, applyMiddleware, compose } from "redux"
  7. import { combineReducers as combineReducersImmutable } from 'redux-immutable'
  8. import { createBrowserHistory } from 'history'
  9. import createConnectedRouter from '../src/ConnectedRouter'
  10. import { connectRouter, routerMiddleware, push } from '../src'
  11. import {
  12. connectRouter as connectRouterImmutable,
  13. routerMiddleware as routerMiddlewareImmutable,
  14. } from '../src/immutable'
  15. import plainStructure from '../src/structure/plain'
  16. import immutableStructure from '../src/structure/immutable'
  17.  
  18.  
  19. Enzyme.configure({ adapter: new Adapter() })
  20.  
  21. const { mount } = Enzyme
  22.  
  23. describe('render test', () => {
  24. let store
  25. let history
  26. let reducer
  27. const TEST_ACTION = 'test-action'
  28. const otherReducer = (state = 0, { type }) => {
  29. switch (type) {
  30. case TEST_ACTION: return ++state
  31. default: return state
  32. }
  33. }
  34.  
  35. beforeEach(() => {
  36. history = createBrowserHistory()
  37. })
  38.  
  39. describe('with plain structure', () => {
  40. let ConnectedRouter
  41.  
  42. beforeEach(() => {
  43. ConnectedRouter = createConnectedRouter(plainStructure)
  44. reducer = combineReducers({
  45. other: otherReducer,
  46. router: connectRouter(history)
  47. })
  48. store = createStore(
  49. reducer,
  50. compose(applyMiddleware(routerMiddleware(history)))
  51. )
  52. })
  53.  
  54. it('render once on initiation', () => {
  55. let renderCount = 0
  56. const RenderCounter = () => {
  57. renderCount++
  58. return null
  59. }
  60. mount(
  61. <Provider store={store}>
  62. <ConnectedRouter history={history}>
  63. <Switch>
  64. <Route path="/" component={RenderCounter} />
  65. </Switch>
  66. </ConnectedRouter>
  67. </Provider>
  68. )
  69. expect(renderCount).toBe(1)
  70. })
  71.  
  72. it('does not render again when non-related action is fired', () => {
  73. let renderCount = 0
  74. const RenderCounter = () => {
  75. renderCount++
  76. return null
  77. }
  78. mount(
  79. <Provider store={store}>
  80. <ConnectedRouter history={history}>
  81. <Route path="/" component={RenderCounter} />
  82. </ConnectedRouter>
  83. </Provider>
  84. )
  85. store.dispatch({ type: TEST_ACTION })
  86. store.dispatch(push('/new-location'))
  87. expect(renderCount).toBe(2)
  88. })
  89. })
  90.  
  91. describe('with immutable structure', () => {
  92. let ConnectedRouter
  93.  
  94. beforeEach(() => {
  95. ConnectedRouter = createConnectedRouter(immutableStructure)
  96. reducer = combineReducersImmutable({
  97. other: otherReducer,
  98. router: connectRouterImmutable(history)
  99. })
  100. store = createStore(
  101. reducer,
  102. compose(applyMiddleware(routerMiddlewareImmutable(history)))
  103. )
  104. })
  105.  
  106. it('render once on initiation', () => {
  107. let renderCount = 0
  108. const RenderCounter = () => {
  109. renderCount++
  110. return null
  111. }
  112. mount(
  113. <Provider store={store}>
  114. <ConnectedRouter history={history}>
  115. <Switch>
  116. <Route path="/" component={RenderCounter} />
  117. </Switch>
  118. </ConnectedRouter>
  119. </Provider>
  120. )
  121. expect(renderCount).toBe(1)
  122. })
  123.  
  124. it('does not render again when non-related action is fired', () => {
  125. let renderCount = 0
  126. const RenderCounter = () => {
  127. renderCount++
  128. return null
  129. }
  130. mount(
  131. <Provider store={store}>
  132. <ConnectedRouter history={history}>
  133. <Route path="/" component={RenderCounter} />
  134. </ConnectedRouter>
  135. </Provider>
  136. )
  137. store.dispatch({ type: TEST_ACTION })
  138. store.dispatch(push('/new-location'))
  139. expect(renderCount).toBe(2)
  140. })
  141. })
  142. })
Add Comment
Please, Sign In to add comment