1
1
import expect , { spyOn } from 'expect'
2
2
import React , { Component } from 'react'
3
- import { renderToString } from 'react-dom/server'
4
- import match from '../match'
3
+ import { renderToStaticMarkup , renderToString } from 'react-dom/server'
4
+
5
5
import createMemoryHistory from '../createMemoryHistory'
6
- import RouterContext from '../RouterContext'
7
6
import Link from '../Link'
7
+ import match from '../match'
8
+ import Router from '../Router'
9
+ import RouterContext from '../RouterContext'
8
10
9
11
describe ( 'server rendering' , function ( ) {
10
12
@@ -43,6 +45,16 @@ describe('server rendering', function () {
43
45
}
44
46
}
45
47
48
+ class Async extends Component {
49
+ render ( ) {
50
+ return (
51
+ < div className = "Async" >
52
+ < h1 > Async</ h1 >
53
+ </ div >
54
+ )
55
+ }
56
+ }
57
+
46
58
const DashboardRoute = {
47
59
path : '/dashboard' ,
48
60
component : Dashboard
@@ -60,13 +72,20 @@ describe('server rendering', function () {
60
72
}
61
73
}
62
74
75
+ const AsyncRoute = {
76
+ path : '/async' ,
77
+ getComponent ( location , cb ) {
78
+ setTimeout ( cb ( null , Async ) )
79
+ }
80
+ }
81
+
63
82
const routes = {
64
83
path : '/' ,
65
84
component : App ,
66
- childRoutes : [ DashboardRoute , AboutRoute , RedirectRoute ]
85
+ childRoutes : [ DashboardRoute , AboutRoute , RedirectRoute , AsyncRoute ]
67
86
}
68
87
69
- it ( 'works' , function ( done ) {
88
+ it ( 'works for synchronous route ' , function ( done ) {
70
89
match ( { routes, location : '/dashboard' } , function ( error , redirectLocation , renderProps ) {
71
90
const string = renderToString (
72
91
< RouterContext { ...renderProps } />
@@ -76,6 +95,16 @@ describe('server rendering', function () {
76
95
} )
77
96
} )
78
97
98
+ it ( 'works for asynchronous route' , function ( done ) {
99
+ match ( { routes, location : '/async' } , function ( error , redirectLocation , renderProps ) {
100
+ const string = renderToString (
101
+ < RouterContext { ...renderProps } />
102
+ )
103
+ expect ( string ) . toMatch ( / A s y n c / )
104
+ done ( )
105
+ } )
106
+ } )
107
+
79
108
it ( 'accepts a custom history' , function ( done ) {
80
109
const history = createMemoryHistory ( )
81
110
const spy = spyOn ( history , 'createLocation' ) . andCallThrough ( )
@@ -122,4 +151,35 @@ describe('server rendering', function () {
122
151
} )
123
152
} )
124
153
154
+ describe ( 'server/client consistency' , function ( ) {
155
+ // Just render to static markup here to avoid having to normalize markup.
156
+
157
+ it ( 'should match for synchronous route' , function ( done ) {
158
+ match ( { routes, location : '/dashboard' } , function ( error , redirectLocation , renderProps ) {
159
+ const serverString = renderToStaticMarkup (
160
+ < RouterContext { ...renderProps } />
161
+ )
162
+ const browserString = renderToStaticMarkup (
163
+ < Router { ...renderProps } history = { createMemoryHistory ( '/dashboard' ) } />
164
+ )
165
+
166
+ expect ( browserString ) . toEqual ( serverString )
167
+ done ( )
168
+ } )
169
+ } )
170
+
171
+ it ( 'should match for asynchronous route' , function ( done ) {
172
+ match ( { routes, location : '/async' } , function ( error , redirectLocation , renderProps ) {
173
+ const serverString = renderToStaticMarkup (
174
+ < RouterContext { ...renderProps } />
175
+ )
176
+ const browserString = renderToStaticMarkup (
177
+ < Router { ...renderProps } history = { createMemoryHistory ( '/async' ) } />
178
+ )
179
+
180
+ expect ( browserString ) . toEqual ( serverString )
181
+ done ( )
182
+ } )
183
+ } )
184
+ } )
125
185
} )
0 commit comments