@@ -8,13 +8,15 @@ import Router from '../Router'
8
8
9
9
describe ( 'When a router enters a branch' , function ( ) {
10
10
let
11
- node , leaveHookSpy , removeLeaveHook ,
12
- DashboardRoute , NewsFeedRoute , InboxRoute , RedirectToInboxRoute , MessageRoute ,
11
+ node ,
12
+ newsLeaveHookSpy , removeNewsLeaveHook , userLeaveHookSpy ,
13
+ DashboardRoute , NewsFeedRoute , InboxRoute , RedirectToInboxRoute , MessageRoute , UserRoute ,
13
14
routes
14
15
15
16
beforeEach ( function ( ) {
16
17
node = document . createElement ( 'div' )
17
- leaveHookSpy = expect . createSpy ( )
18
+ newsLeaveHookSpy = expect . createSpy ( )
19
+ userLeaveHookSpy = expect . createSpy ( )
18
20
19
21
class Dashboard extends Component {
20
22
render ( ) {
@@ -29,9 +31,9 @@ describe('When a router enters a branch', function () {
29
31
30
32
class NewsFeed extends Component {
31
33
componentWillMount ( ) {
32
- removeLeaveHook = this . context . router . setRouteLeaveHook (
34
+ removeNewsLeaveHook = this . context . router . setRouteLeaveHook (
33
35
this . props . route ,
34
- ( ) => leaveHookSpy ( ) // Break reference equality.
36
+ ( ) => newsLeaveHookSpy ( ) // Break reference equality.
35
37
)
36
38
}
37
39
@@ -50,6 +52,23 @@ describe('When a router enters a branch', function () {
50
52
}
51
53
}
52
54
55
+ class User extends Component {
56
+ componentWillMount ( ) {
57
+ this . context . router . setRouteLeaveHook (
58
+ this . props . route ,
59
+ userLeaveHookSpy
60
+ )
61
+ }
62
+
63
+ render ( ) {
64
+ return < div > User</ div >
65
+ }
66
+ }
67
+
68
+ User . contextTypes = {
69
+ router : React . PropTypes . object . isRequired
70
+ }
71
+
53
72
NewsFeedRoute = {
54
73
path : 'news' ,
55
74
component : NewsFeed ,
@@ -102,6 +121,11 @@ describe('When a router enters a branch', function () {
102
121
}
103
122
}
104
123
124
+ UserRoute = {
125
+ path : 'users/:userId' ,
126
+ component : User
127
+ }
128
+
105
129
DashboardRoute = {
106
130
path : '/' ,
107
131
component : Dashboard ,
@@ -113,7 +137,7 @@ describe('When a router enters a branch', function () {
113
137
onLeave ( ) {
114
138
expect ( this ) . toBe ( DashboardRoute )
115
139
} ,
116
- childRoutes : [ NewsFeedRoute , InboxRoute , RedirectToInboxRoute , MessageRoute ]
140
+ childRoutes : [ NewsFeedRoute , InboxRoute , RedirectToInboxRoute , MessageRoute , UserRoute ]
117
141
}
118
142
119
143
routes = [
@@ -139,17 +163,14 @@ describe('When a router enters a branch', function () {
139
163
it ( 'calls the route leave hooks when leaving the route' , function ( done ) {
140
164
const history = createHistory ( '/news' )
141
165
142
- // Stub this function to exercise the code path.
143
- history . listenBeforeUnload = ( ) => ( ( ) => { } )
144
-
145
166
render ( < Router history = { history } routes = { routes } /> , node , function ( ) {
146
- expect ( leaveHookSpy . calls . length ) . toEqual ( 0 )
167
+ expect ( newsLeaveHookSpy . calls . length ) . toEqual ( 0 )
147
168
history . push ( '/inbox' )
148
- expect ( leaveHookSpy . calls . length ) . toEqual ( 1 )
169
+ expect ( newsLeaveHookSpy . calls . length ) . toEqual ( 1 )
149
170
history . push ( '/news' )
150
- expect ( leaveHookSpy . calls . length ) . toEqual ( 1 )
171
+ expect ( newsLeaveHookSpy . calls . length ) . toEqual ( 1 )
151
172
history . push ( '/inbox' )
152
- expect ( leaveHookSpy . calls . length ) . toEqual ( 2 )
173
+ expect ( newsLeaveHookSpy . calls . length ) . toEqual ( 2 )
153
174
done ( )
154
175
} )
155
176
} )
@@ -158,9 +179,25 @@ describe('When a router enters a branch', function () {
158
179
const history = createHistory ( '/news' )
159
180
160
181
render ( < Router history = { history } routes = { routes } /> , node , function ( ) {
161
- removeLeaveHook ( )
182
+ removeNewsLeaveHook ( )
162
183
history . push ( '/inbox' )
163
- expect ( leaveHookSpy ) . toNotHaveBeenCalled ( )
184
+ expect ( newsLeaveHookSpy ) . toNotHaveBeenCalled ( )
185
+ done ( )
186
+ } )
187
+ } )
188
+
189
+ it ( 'does not remove route leave hooks when changing params' , function ( done ) {
190
+ const history = createHistory ( '/users/foo' )
191
+
192
+ // Stub this function to exercise the code path.
193
+ history . listenBeforeUnload = ( ) => ( ( ) => { } )
194
+
195
+ render ( < Router history = { history } routes = { routes } /> , node , function ( ) {
196
+ expect ( userLeaveHookSpy . calls . length ) . toEqual ( 0 )
197
+ history . push ( '/users/bar' )
198
+ expect ( userLeaveHookSpy . calls . length ) . toEqual ( 1 )
199
+ history . push ( '/users/baz' )
200
+ expect ( userLeaveHookSpy . calls . length ) . toEqual ( 2 )
164
201
done ( )
165
202
} )
166
203
} )
0 commit comments