@@ -7,33 +7,48 @@ import execSteps from './execSteps'
7
7
import Router from '../Router'
8
8
9
9
describe ( 'When a router enters a branch' , function ( ) {
10
+ let
11
+ node , leaveHookSpy , removeLeaveHook ,
12
+ DashboardRoute , NewsFeedRoute , InboxRoute , RedirectToInboxRoute , MessageRoute ,
13
+ routes
10
14
11
- class Dashboard extends Component {
12
- render ( ) {
13
- return (
14
- < div className = "Dashboard" >
15
- < h1 > The Dashboard</ h1 >
16
- { this . props . children }
17
- </ div >
18
- )
15
+ beforeEach ( function ( ) {
16
+ node = document . createElement ( 'div' )
17
+ leaveHookSpy = expect . createSpy ( )
18
+
19
+ class Dashboard extends Component {
20
+ render ( ) {
21
+ return (
22
+ < div className = "Dashboard" >
23
+ < h1 > The Dashboard</ h1 >
24
+ { this . props . children }
25
+ </ div >
26
+ )
27
+ }
19
28
}
20
- }
21
29
22
- class NewsFeed extends Component {
23
- render ( ) {
24
- return < div > News</ div >
30
+ class NewsFeed extends Component {
31
+ componentWillMount ( ) {
32
+ removeLeaveHook = this . context . router . addRouteLeaveHook (
33
+ this . props . route ,
34
+ ( ) => leaveHookSpy ( ) // Break reference equality.
35
+ )
36
+ }
37
+
38
+ render ( ) {
39
+ return < div > News</ div >
40
+ }
25
41
}
26
- }
27
42
28
- class Inbox extends Component {
29
- render ( ) {
30
- return < div > Inbox</ div >
43
+ NewsFeed . contextTypes = {
44
+ router : React . PropTypes . object . isRequired
31
45
}
32
- }
33
46
34
- let node , DashboardRoute , NewsFeedRoute , InboxRoute , RedirectToInboxRoute , MessageRoute , routes
35
- beforeEach ( function ( ) {
36
- node = document . createElement ( 'div' )
47
+ class Inbox extends Component {
48
+ render ( ) {
49
+ return < div > Inbox</ div >
50
+ }
51
+ }
37
52
38
53
NewsFeedRoute = {
39
54
path : 'news' ,
@@ -121,6 +136,35 @@ describe('When a router enters a branch', function () {
121
136
} )
122
137
} )
123
138
139
+ it ( 'calls the route leave hooks when leaving the route' , function ( done ) {
140
+ const history = createHistory ( '/news' )
141
+
142
+ // Stub this function to exercise the code path.
143
+ history . listenBeforeUnload = ( ) => ( ( ) => { } )
144
+
145
+ render ( < Router history = { history } routes = { routes } /> , node , function ( ) {
146
+ expect ( leaveHookSpy . calls . length ) . toEqual ( 0 )
147
+ history . push ( '/inbox' )
148
+ expect ( leaveHookSpy . calls . length ) . toEqual ( 1 )
149
+ history . push ( '/news' )
150
+ expect ( leaveHookSpy . calls . length ) . toEqual ( 1 )
151
+ history . push ( '/inbox' )
152
+ expect ( leaveHookSpy . calls . length ) . toEqual ( 2 )
153
+ done ( )
154
+ } )
155
+ } )
156
+
157
+ it ( 'does not call removed route leave hooks' , function ( done ) {
158
+ const history = createHistory ( '/news' )
159
+
160
+ render ( < Router history = { history } routes = { routes } /> , node , function ( ) {
161
+ removeLeaveHook ( )
162
+ history . push ( '/inbox' )
163
+ expect ( leaveHookSpy ) . toNotHaveBeenCalled ( )
164
+ done ( )
165
+ } )
166
+ } )
167
+
124
168
describe ( 'and one of the transition hooks navigates to another route' , function ( ) {
125
169
it ( 'immediately transitions to the new route' , function ( done ) {
126
170
const redirectRouteEnterSpy = spyOn ( RedirectToInboxRoute , 'onEnter' ) . andCallThrough ( )
0 commit comments