|
1 | 1 | import expect from 'expect'
|
2 | 2 |
|
| 3 | +import React from 'react' |
| 4 | +import ReactDOM from 'react-dom' |
| 5 | +import { Router, Route, useRouterHistory } from 'react-router' |
| 6 | +import { Provider } from 'react-redux' |
3 | 7 | import { createStore, combineReducers } from 'redux'
|
4 | 8 | import { ActionCreators, instrument } from 'redux-devtools'
|
5 | 9 |
|
@@ -214,5 +218,81 @@ export default function createTests(createHistory, name, reset = defaultReset) {
|
214 | 218 | historyUnsubscribe()
|
215 | 219 | })
|
216 | 220 | })
|
| 221 | + |
| 222 | + if (typeof(document) !== 'undefined') { |
| 223 | + describe('Redux Router component', () => { |
| 224 | + let store, history, rootElement |
| 225 | + |
| 226 | + beforeEach(() => { |
| 227 | + store = createStore(combineReducers({ |
| 228 | + routing: routerReducer |
| 229 | + })) |
| 230 | + |
| 231 | + history = syncHistoryWithStore(useRouterHistory(createHistory)(), store) |
| 232 | + |
| 233 | + rootElement = document.createElement('div') |
| 234 | + document.body.appendChild(rootElement) |
| 235 | + }) |
| 236 | + |
| 237 | + afterEach(() => { |
| 238 | + history.unsubscribe() |
| 239 | + rootElement.parentNode.removeChild(rootElement) |
| 240 | + }) |
| 241 | + |
| 242 | + it('syncs history -> components', () => { |
| 243 | + history.push('/foo') |
| 244 | + |
| 245 | + ReactDOM.render( |
| 246 | + React.createElement(Provider, { store }, |
| 247 | + React.createElement(Router, { history }, |
| 248 | + React.createElement(Route, |
| 249 | + { |
| 250 | + path: '/', |
| 251 | + component: props => React.createElement('span', {}, props.children) |
| 252 | + }, |
| 253 | + [ 'foo', 'bar' ].map(path => |
| 254 | + React.createElement(Route, { |
| 255 | + path: path, |
| 256 | + component: () => React.createElement('span', {}, `at /${path}`) |
| 257 | + }) |
| 258 | + ) |
| 259 | + ) |
| 260 | + ) |
| 261 | + ), |
| 262 | + rootElement |
| 263 | + ) |
| 264 | + expect(rootElement.textContent).toEqual('at /foo') |
| 265 | + |
| 266 | + history.push('/bar') |
| 267 | + expect(rootElement.textContent).toEqual('at /bar') |
| 268 | + }) |
| 269 | + |
| 270 | + it('syncs history -> components when the initial route gets replaced', () => { |
| 271 | + history.push('/foo') |
| 272 | + |
| 273 | + ReactDOM.render( |
| 274 | + React.createElement(Provider, { store }, |
| 275 | + React.createElement(Router, { history }, [ |
| 276 | + React.createElement(Route, { |
| 277 | + path: '/', |
| 278 | + component: props => React.createElement('span', {}, props.children) |
| 279 | + }, [ |
| 280 | + React.createElement(Route, { |
| 281 | + path: 'foo', |
| 282 | + onEnter: (nextState, replace) => replace('/bar') |
| 283 | + }), |
| 284 | + React.createElement(Route, { |
| 285 | + path: 'bar', |
| 286 | + component: () => React.createElement('span', {}, [ 'at /bar' ]) |
| 287 | + }) |
| 288 | + ]) |
| 289 | + ]) |
| 290 | + ), |
| 291 | + rootElement |
| 292 | + ) |
| 293 | + expect(rootElement.textContent).toEqual('at /bar') |
| 294 | + }) |
| 295 | + }) |
| 296 | + } |
217 | 297 | })
|
218 | 298 | }
|
0 commit comments