@@ -24,8 +24,62 @@ describe('SwitcherProvider', () => {
24
24
expect ( component . text ( ) ) . toEqual ( 'Another ChildHomeHome' ) ;
25
25
helpers . currentPath . restore ( ) ;
26
26
} ) ;
27
+
28
+ it ( 'removes event listeners when a child Switcher is unmounted' , ( ) => {
29
+ sinon . stub ( helpers , 'currentPath' ) . returns ( '/' ) ;
30
+ const component = renderNested ( ) ;
31
+ const innerSwitcher = component . find ( 'Switcher' ) . last ( ) ;
32
+ const listenerId = innerSwitcher . node . _id ;
33
+ const instance = component . instance ( ) ;
34
+
35
+ expect ( instance . switcherProvider . loadListeners . length ) . toEqual ( 2 ) ;
36
+ expect ( instance . switcherProvider . hashChangeListeners . length ) . toEqual ( 2 ) ;
37
+ expect (
38
+ instance . switcherProvider . loadListeners
39
+ . map ( ( { id } ) => id )
40
+ . indexOf ( listenerId )
41
+ ) . not . toEqual ( - 1 ) ;
42
+ expect (
43
+ instance . switcherProvider . hashChangeListeners
44
+ . map ( ( { id } ) => id )
45
+ . indexOf ( listenerId )
46
+ ) . not . toEqual ( - 1 ) ;
47
+ helpers . currentPath . restore ( ) ;
48
+
49
+ sinon . stub ( helpers , 'currentPath' ) . returns ( '/hello' ) ;
50
+ component . update ( ) ;
51
+ expect ( instance . switcherProvider . loadListeners . length ) . toEqual ( 1 ) ;
52
+ expect ( instance . switcherProvider . hashChangeListeners . length ) . toEqual ( 1 ) ;
53
+ expect (
54
+ instance . switcherProvider . loadListeners
55
+ . map ( ( { id } ) => id )
56
+ . indexOf ( listenerId )
57
+ ) . toEqual ( - 1 ) ;
58
+ expect (
59
+ instance . switcherProvider . hashChangeListeners
60
+ . map ( ( { id } ) => id )
61
+ . indexOf ( listenerId )
62
+ ) . toEqual ( - 1 ) ;
63
+ helpers . currentPath . restore ( ) ;
64
+ } ) ;
27
65
} ) ;
28
66
67
+ function renderNested ( ) {
68
+ return mount (
69
+ < SwitcherProvider >
70
+ < div >
71
+ < Switcher >
72
+ < Switcher path = "/" >
73
+ < div path = { [ '/' , '/other' ] } > Home</ div >
74
+ < div path = "/second" > Second</ div >
75
+ </ Switcher >
76
+ < div path = "/hello" > Hello</ div >
77
+ </ Switcher >
78
+ </ div >
79
+ </ SwitcherProvider >
80
+ ) ;
81
+ }
82
+
29
83
function renderComponent ( ) {
30
84
return mount (
31
85
< SwitcherProvider >
0 commit comments