1
1
describe ( 'angulargmMapController' , function ( ) {
2
- var scope ;
2
+ var scope , elm ;
3
3
var mapCtrl , mapCntr ;
4
4
5
5
beforeEach ( function ( ) {
@@ -22,7 +22,7 @@ describe('angulargmMapController', function() {
22
22
} ;
23
23
24
24
// set up element
25
- var elm = angular . element ( '<div gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds" gm-map-options="mapOptions">' +
25
+ elm = angular . element ( '<div gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds" gm-map-options="mapOptions">' +
26
26
'<div id="test"></div>' +
27
27
'</div' ) ;
28
28
@@ -31,11 +31,19 @@ describe('angulargmMapController', function() {
31
31
} ) ) ;
32
32
33
33
34
+ afterEach ( inject ( function ( angulargmContainer ) {
35
+ if ( scope && scope . $destroy ) {
36
+ scope . $destroy ( ) ;
37
+ }
38
+ angulargmContainer . removeMap ( 'test' ) ;
39
+ } ) ) ;
40
+
41
+
34
42
it ( 'constructs the map using the provided map options' , function ( ) {
35
43
expect ( mapCtrl . dragging ) . toBeFalsy ( ) ;
36
44
expect ( mapCtrl . center ) . toEqual ( new google . maps . LatLng ( 2 , 3 ) ) ;
37
45
expect ( mapCtrl . zoom ) . toEqual ( 1 ) ;
38
- var map = mapCntr . getMap ( 'test' ) ;
46
+ var map = mapCntr . getMap ( scope . gmMapId ( ) ) ;
39
47
expect ( mapCtrl . bounds ) . toEqual ( map . getBounds ( ) ) ;
40
48
} ) ;
41
49
@@ -47,16 +55,44 @@ describe('angulargmMapController', function() {
47
55
return 'test2' ;
48
56
} ;
49
57
50
- var elm = angular . element ( '<div gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds">' +
58
+ var elm2 = angular . element ( '<div gm-map-id="mapId" gm-center="center" gm-zoom="zoom" gm-bounds="bounds">' +
51
59
'<div id="test2"></div>' +
52
60
'</div' ) ;
53
- mapCtrl = $controller ( 'angulargmMapController' , { $scope : scope , $element : elm } ) ;
61
+ mapCtrl = $controller ( 'angulargmMapController' , { $scope : scope , $element : elm2 } ) ;
54
62
55
63
expect ( mapCtrl . center ) . toEqual ( angulargmDefaults . mapOptions . center ) ;
56
64
expect ( mapCtrl . zoom ) . toEqual ( angulargmDefaults . mapOptions . zoom ) ;
57
65
} ) ) ;
58
66
59
67
68
+ it ( 'resets map on controller re-instantiation' , inject ( function ( $rootScope , $controller ) {
69
+ var map = mapCntr . getMap ( scope . gmMapId ( ) ) ;
70
+ var scope2 = $rootScope . $new ( ) ;
71
+ var gmMapOptions = scope . gmMapOptions ( ) ;
72
+ var gmMapId = scope . gmMapId ( ) ;
73
+ scope2 . gmMapOptions = function ( ) { return gmMapOptions } ;
74
+ scope2 . gmMapId = function ( ) { return gmMapId } ;
75
+
76
+ // move map
77
+ var newCenter = new google . maps . LatLng ( gmMapOptions . center . lat ( ) + 5 ,
78
+ gmMapOptions . center . lng ( ) + 5 ) ;
79
+ map . setCenter ( newCenter ) ;
80
+ map . setZoom ( gmMapOptions . zoom + 2 ) ;
81
+ map . setMapTypeId ( google . maps . MapTypeId . SATELLITE ) ;
82
+ expect ( mapCtrl . center ) . toEqual ( newCenter ) ; // sanity check--we actually changed something
83
+
84
+ // destroy scope
85
+ scope . $destroy ( ) ;
86
+
87
+ // re-instantiate controller
88
+ mapCtrl = $controller ( 'angulargmMapController' , { $scope : scope2 , $element : elm } ) ;
89
+
90
+ expect ( mapCtrl . center ) . toEqual ( gmMapOptions . center ) ;
91
+ expect ( mapCtrl . zoom ) . toEqual ( gmMapOptions . zoom ) ;
92
+ expect ( map . getMapTypeId ( ) ) . toEqual ( gmMapOptions . mapTypeId ) ;
93
+ } ) ) ;
94
+
95
+
60
96
it ( 'removes markers on scope destroy' , function ( ) {
61
97
var mapId = scope . gmMapId ( ) ;
62
98
scope . $destroy ( ) ;
@@ -73,7 +109,7 @@ describe('angulargmMapController', function() {
73
109
mapCtrl . addMapListener ( 'center_changed' , function ( ) {
74
110
called = true ;
75
111
} ) ;
76
- google . maps . event . trigger ( mapCntr . getMap ( 'test' ) , 'center_changed' ) ;
112
+ google . maps . event . trigger ( mapCntr . getMap ( scope . gmMapId ( ) ) , 'center_changed' ) ;
77
113
78
114
expect ( called ) . toBeTruthy ( ) ;
79
115
} ) ;
@@ -84,8 +120,8 @@ describe('angulargmMapController', function() {
84
120
mapCtrl . addMapListenerOnce ( 'center_changed' , function ( ) {
85
121
callCount ++ ;
86
122
} ) ;
87
- google . maps . event . trigger ( mapCntr . getMap ( 'test' ) , 'center_changed' ) ;
88
- google . maps . event . trigger ( mapCntr . getMap ( 'test' ) , 'center_changed' ) ;
123
+ google . maps . event . trigger ( mapCntr . getMap ( scope . gmMapId ( ) ) , 'center_changed' ) ;
124
+ google . maps . event . trigger ( mapCntr . getMap ( scope . gmMapId ( ) ) , 'center_changed' ) ;
89
125
90
126
expect ( callCount ) . toEqual ( 1 ) ;
91
127
} ) ;
0 commit comments