File tree 8 files changed +101
-20
lines changed
8 files changed +101
-20
lines changed Original file line number Diff line number Diff line change @@ -152,4 +152,30 @@ describe('Gmaps', function () {
152
152
expect ( parent . refs . gmaps . getMap ( ) . setOptions ) . toBeCalled ( ) ;
153
153
} ) ;
154
154
} ) ;
155
+
156
+ describe ( 'unmounted' , function ( ) {
157
+
158
+ beforeEach ( function ( ) {
159
+ GoogleMaps . fireCallbacks = jest . genMockFunction ( ) ;
160
+ } ) ;
161
+
162
+ it ( 'does not fire the callback (unloaded)' , function ( ) {
163
+ var gmaps = TestUtils . renderIntoDocument ( React . createElement ( Gmaps , null ) ) ;
164
+ ReactDOM . unmountComponentAtNode ( ReactDOM . findDOMNode ( gmaps ) . parentNode ) ;
165
+ expect ( GoogleMaps . fireCallbacks ) . not . toBeCalled ( ) ;
166
+ } ) ;
167
+
168
+ it ( 'does not fire the callback (loaded)' , function ( ) {
169
+ window . google = {
170
+ maps : {
171
+ event : {
172
+ removeListener : jest . genMockFunction ( )
173
+ }
174
+ }
175
+ } ;
176
+ var gmaps = TestUtils . renderIntoDocument ( React . createElement ( Gmaps , null ) ) ;
177
+ ReactDOM . unmountComponentAtNode ( ReactDOM . findDOMNode ( gmaps ) . parentNode ) ;
178
+ expect ( GoogleMaps . fireCallbacks ) . not . toBeCalled ( ) ;
179
+ } ) ;
180
+ } ) ;
155
181
} ) ;
Original file line number Diff line number Diff line change @@ -50,10 +50,13 @@ var Gmaps = _react2['default'].createClass({
50
50
} ,
51
51
52
52
componentDidMount : function componentDidMount ( ) {
53
- _utilsGoogleMaps2 [ 'default' ] . load ( this . props . params , this . mapsCallback ) ;
53
+ this . setState ( {
54
+ callbackIndex : _utilsGoogleMaps2 [ 'default' ] . load ( this . props . params , this . mapsCallback )
55
+ } ) ;
54
56
} ,
55
57
56
58
componentWillUnmount : function componentWillUnmount ( ) {
59
+ _utilsGoogleMaps2 [ 'default' ] . removeCallback ( this . state . callbackIndex ) ;
57
60
this . removeListeners ( ) ;
58
61
} ,
59
62
@@ -83,7 +86,7 @@ var Gmaps = _react2['default'].createClass({
83
86
isMapCreated : true
84
87
} ) ;
85
88
if ( this . props . onMapCreated ) {
86
- this . props . onMapCreated ( this . map , google . maps ) ;
89
+ this . props . onMapCreated ( this . map ) ;
87
90
}
88
91
} ,
89
92
Original file line number Diff line number Diff line change @@ -19,9 +19,11 @@ var Listener = {
19
19
} ,
20
20
21
21
removeListeners : function removeListeners ( ) {
22
- this . listeners . forEach ( function ( listener ) {
23
- google . maps . event . removeListener ( listener ) ;
24
- } ) ;
22
+ if ( window . google ) {
23
+ this . listeners . forEach ( function ( listener ) {
24
+ google . maps . event . removeListener ( listener ) ;
25
+ } ) ;
26
+ }
25
27
}
26
28
27
29
} ;
Original file line number Diff line number Diff line change @@ -17,15 +17,16 @@ exports['default'] = {
17
17
appended : false ,
18
18
19
19
load : function load ( params , callback ) {
20
- if ( ! window . google ) {
21
- this . callbacks . push ( callback ) ;
20
+ var index = this . callbacks . push ( callback ) ;
21
+ if ( window . google ) {
22
+ setTimeout ( this . fireCallbacks . bind ( this ) ) ;
23
+ } else {
22
24
if ( ! this . appended ) {
23
25
window . mapsCallback = this . mapsCallback . bind ( this ) ;
24
26
this . appendScript ( params ) ;
25
27
}
26
- } else {
27
- setTimeout ( callback ) ;
28
28
}
29
+ return index ;
29
30
} ,
30
31
31
32
getSrc : function getSrc ( params ) {
@@ -44,11 +45,19 @@ exports['default'] = {
44
45
} ,
45
46
46
47
mapsCallback : function mapsCallback ( ) {
47
- window . mapsCallback = undefined ; ;
48
+ window . mapsCallback = undefined ;
49
+ this . fireCallbacks ( ) ;
50
+ } ,
51
+
52
+ fireCallbacks : function fireCallbacks ( ) {
48
53
this . callbacks . forEach ( function ( callback ) {
49
54
return callback ( ) ;
50
55
} ) ;
51
56
this . callbacks = [ ] ;
57
+ } ,
58
+
59
+ removeCallback : function removeCallback ( index ) {
60
+ this . callbacks . splice ( index - 1 , 1 ) ;
52
61
}
53
62
54
63
} ;
Original file line number Diff line number Diff line change @@ -148,4 +148,31 @@ describe('Gmaps', () => {
148
148
149
149
} ) ;
150
150
151
+ describe ( 'unmounted' , ( ) => {
152
+
153
+ beforeEach ( ( ) => {
154
+ GoogleMaps . fireCallbacks = jest . genMockFunction ( ) ;
155
+ } ) ;
156
+
157
+ it ( 'does not fire the callback (unloaded)' , ( ) => {
158
+ const gmaps = TestUtils . renderIntoDocument ( < Gmaps /> ) ;
159
+ ReactDOM . unmountComponentAtNode ( ReactDOM . findDOMNode ( gmaps ) . parentNode ) ;
160
+ expect ( GoogleMaps . fireCallbacks ) . not . toBeCalled ( ) ;
161
+ } ) ;
162
+
163
+ it ( 'does not fire the callback (loaded)' , ( ) => {
164
+ window . google = {
165
+ maps : {
166
+ event : {
167
+ removeListener : jest . genMockFunction ( )
168
+ }
169
+ }
170
+ } ;
171
+ const gmaps = TestUtils . renderIntoDocument ( < Gmaps /> ) ;
172
+ ReactDOM . unmountComponentAtNode ( ReactDOM . findDOMNode ( gmaps ) . parentNode ) ;
173
+ expect ( GoogleMaps . fireCallbacks ) . not . toBeCalled ( ) ;
174
+ } ) ;
175
+
176
+ } ) ;
177
+
151
178
} ) ;
Original file line number Diff line number Diff line change @@ -19,10 +19,13 @@ const Gmaps = React.createClass({
19
19
} ,
20
20
21
21
componentDidMount ( ) {
22
- GoogleMaps . load ( this . props . params , this . mapsCallback ) ;
22
+ this . setState ( {
23
+ callbackIndex : GoogleMaps . load ( this . props . params , this . mapsCallback )
24
+ } ) ;
23
25
} ,
24
26
25
27
componentWillUnmount ( ) {
28
+ GoogleMaps . removeCallback ( this . state . callbackIndex ) ;
26
29
this . removeListeners ( ) ;
27
30
} ,
28
31
@@ -54,7 +57,7 @@ const Gmaps = React.createClass({
54
57
isMapCreated : true
55
58
} ) ;
56
59
if ( this . props . onMapCreated ) {
57
- this . props . onMapCreated ( this . map , google . maps ) ;
60
+ this . props . onMapCreated ( this . map ) ;
58
61
}
59
62
} ,
60
63
Original file line number Diff line number Diff line change @@ -14,9 +14,11 @@ const Listener = {
14
14
} ,
15
15
16
16
removeListeners ( ) {
17
- this . listeners . forEach ( ( listener ) => {
18
- google . maps . event . removeListener ( listener ) ;
19
- } ) ;
17
+ if ( window . google ) {
18
+ this . listeners . forEach ( ( listener ) => {
19
+ google . maps . event . removeListener ( listener ) ;
20
+ } ) ;
21
+ }
20
22
}
21
23
22
24
} ;
Original file line number Diff line number Diff line change @@ -7,15 +7,16 @@ export default {
7
7
appended : false ,
8
8
9
9
load ( params , callback ) {
10
- if ( ! window . google ) {
11
- this . callbacks . push ( callback ) ;
10
+ const index = this . callbacks . push ( callback ) ;
11
+ if ( window . google ) {
12
+ setTimeout ( this . fireCallbacks . bind ( this ) ) ;
13
+ } else {
12
14
if ( ! this . appended ) {
13
15
window . mapsCallback = this . mapsCallback . bind ( this ) ;
14
16
this . appendScript ( params ) ;
15
17
}
16
- } else {
17
- setTimeout ( callback ) ;
18
18
}
19
+ return index ;
19
20
} ,
20
21
21
22
getSrc ( params ) {
@@ -34,9 +35,17 @@ export default {
34
35
} ,
35
36
36
37
mapsCallback ( ) {
37
- window . mapsCallback = undefined ; ;
38
+ window . mapsCallback = undefined ;
39
+ this . fireCallbacks ( ) ;
40
+ } ,
41
+
42
+ fireCallbacks ( ) {
38
43
this . callbacks . forEach ( callback => callback ( ) ) ;
39
44
this . callbacks = [ ] ;
45
+ } ,
46
+
47
+ removeCallback ( index ) {
48
+ this . callbacks . splice ( index - 1 , 1 ) ;
40
49
}
41
50
42
51
} ;
You can’t perform that action at this time.
0 commit comments