11describe ( 'angulargmMapController' , function ( ) {
2- var scope ;
2+ var scope , elm ;
33 var mapCtrl , mapCntr ;
44
55 beforeEach ( function ( ) {
@@ -22,7 +22,7 @@ describe('angulargmMapController', function() {
2222 } ;
2323
2424 // 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">' +
2626 '<div id="test"></div>' +
2727 '</div' ) ;
2828
@@ -31,11 +31,19 @@ describe('angulargmMapController', function() {
3131 } ) ) ;
3232
3333
34+ afterEach ( inject ( function ( angulargmContainer ) {
35+ if ( scope && scope . $destroy ) {
36+ scope . $destroy ( ) ;
37+ }
38+ angulargmContainer . removeMap ( 'test' ) ;
39+ } ) ) ;
40+
41+
3442 it ( 'constructs the map using the provided map options' , function ( ) {
3543 expect ( mapCtrl . dragging ) . toBeFalsy ( ) ;
3644 expect ( mapCtrl . center ) . toEqual ( new google . maps . LatLng ( 2 , 3 ) ) ;
3745 expect ( mapCtrl . zoom ) . toEqual ( 1 ) ;
38- var map = mapCntr . getMap ( 'test' ) ;
46+ var map = mapCntr . getMap ( scope . gmMapId ( ) ) ;
3947 expect ( mapCtrl . bounds ) . toEqual ( map . getBounds ( ) ) ;
4048 } ) ;
4149
@@ -47,16 +55,44 @@ describe('angulargmMapController', function() {
4755 return 'test2' ;
4856 } ;
4957
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">' +
5159 '<div id="test2"></div>' +
5260 '</div' ) ;
53- mapCtrl = $controller ( 'angulargmMapController' , { $scope : scope , $element : elm } ) ;
61+ mapCtrl = $controller ( 'angulargmMapController' , { $scope : scope , $element : elm2 } ) ;
5462
5563 expect ( mapCtrl . center ) . toEqual ( angulargmDefaults . mapOptions . center ) ;
5664 expect ( mapCtrl . zoom ) . toEqual ( angulargmDefaults . mapOptions . zoom ) ;
5765 } ) ) ;
5866
5967
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+
6096 it ( 'removes markers on scope destroy' , function ( ) {
6197 var mapId = scope . gmMapId ( ) ;
6298 scope . $destroy ( ) ;
@@ -73,7 +109,7 @@ describe('angulargmMapController', function() {
73109 mapCtrl . addMapListener ( 'center_changed' , function ( ) {
74110 called = true ;
75111 } ) ;
76- google . maps . event . trigger ( mapCntr . getMap ( 'test' ) , 'center_changed' ) ;
112+ google . maps . event . trigger ( mapCntr . getMap ( scope . gmMapId ( ) ) , 'center_changed' ) ;
77113
78114 expect ( called ) . toBeTruthy ( ) ;
79115 } ) ;
@@ -84,8 +120,8 @@ describe('angulargmMapController', function() {
84120 mapCtrl . addMapListenerOnce ( 'center_changed' , function ( ) {
85121 callCount ++ ;
86122 } ) ;
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' ) ;
89125
90126 expect ( callCount ) . toEqual ( 1 ) ;
91127 } ) ;
0 commit comments