@@ -2,22 +2,18 @@ import {Component} from '@angular/core';
22import { async , TestBed } from '@angular/core/testing' ;
33import { By } from '@angular/platform-browser' ;
44
5- import { createMapConstructorSpy , createMapSpy } from './testing/fake-google-map-utils' ;
6- import { GoogleMapModule } from './index' ;
7-
8- const DEFAULT_OPTIONS : google . maps . MapOptions = {
9- center : { lat : 37.421995 , lng : - 122.084092 } ,
10- zoom : 17 ,
11- } ;
5+ import { DEFAULT_HEIGHT , DEFAULT_OPTIONS , DEFAULT_WIDTH , GoogleMapModule } from './index' ;
6+ import {
7+ createMapConstructorSpy ,
8+ createMapSpy ,
9+ TestingWindow
10+ } from './testing/fake-google-map-utils' ;
1211
1312describe ( 'GoogleMap' , ( ) => {
1413 let mapConstructorSpy : jasmine . Spy ;
1514 let mapSpy : jasmine . SpyObj < google . maps . Map > ;
1615
1716 beforeEach ( async ( ( ) => {
18- mapSpy = createMapSpy ( DEFAULT_OPTIONS ) ;
19- mapConstructorSpy = createMapConstructorSpy ( mapSpy ) ;
20-
2117 TestBed . configureTestingModule ( {
2218 imports : [ GoogleMapModule ] ,
2319 declarations : [ TestApp ] ,
@@ -28,17 +24,125 @@ describe('GoogleMap', () => {
2824 TestBed . compileComponents ( ) ;
2925 } ) ;
3026
27+ afterEach ( ( ) => {
28+ const testingWindow : TestingWindow = window ;
29+ delete testingWindow . google ;
30+ } ) ;
31+
32+ it ( 'throws an error is the Google Maps JavaScript API was not loaded' , ( ) => {
33+ mapSpy = createMapSpy ( DEFAULT_OPTIONS ) ;
34+ mapConstructorSpy = createMapConstructorSpy ( mapSpy , false ) ;
35+
36+ expect ( ( ) => TestBed . createComponent ( TestApp ) )
37+ . toThrow ( new Error (
38+ 'Namespace google not found, cannot construct embedded google ' +
39+ 'map. Please install the Google Maps JavaScript API: ' +
40+ 'https://developers.google.com/maps/documentation/javascript/' +
41+ 'tutorial#Loading_the_Maps_API' ) ) ;
42+ } ) ;
43+
3144 it ( 'initializes a Google map' , ( ) => {
45+ mapSpy = createMapSpy ( DEFAULT_OPTIONS ) ;
46+ mapConstructorSpy = createMapConstructorSpy ( mapSpy ) ;
47+
3248 const fixture = TestBed . createComponent ( TestApp ) ;
49+ fixture . detectChanges ( ) ;
50+
3351 const container = fixture . debugElement . query ( By . css ( 'div' ) ) ;
52+ expect ( container . nativeElement . style . height ) . toBe ( DEFAULT_HEIGHT ) ;
53+ expect ( container . nativeElement . style . width ) . toBe ( DEFAULT_WIDTH ) ;
54+ expect ( mapConstructorSpy ) . toHaveBeenCalledWith ( container . nativeElement , DEFAULT_OPTIONS ) ;
55+ } ) ;
56+
57+ it ( 'sets height and width of the map' , ( ) => {
58+ mapSpy = createMapSpy ( DEFAULT_OPTIONS ) ;
59+ mapConstructorSpy = createMapConstructorSpy ( mapSpy ) ;
60+
61+ const fixture = TestBed . createComponent ( TestApp ) ;
62+ fixture . componentInstance . height = '750px' ;
63+ fixture . componentInstance . width = '400px' ;
3464 fixture . detectChanges ( ) ;
3565
66+ const container = fixture . debugElement . query ( By . css ( 'div' ) ) ;
67+ expect ( container . nativeElement . style . height ) . toBe ( '750px' ) ;
68+ expect ( container . nativeElement . style . width ) . toBe ( '400px' ) ;
3669 expect ( mapConstructorSpy ) . toHaveBeenCalledWith ( container . nativeElement , DEFAULT_OPTIONS ) ;
70+
71+ fixture . componentInstance . height = '650px' ;
72+ fixture . componentInstance . width = '350px' ;
73+ fixture . detectChanges ( ) ;
74+
75+ expect ( container . nativeElement . style . height ) . toBe ( '650px' ) ;
76+ expect ( container . nativeElement . style . width ) . toBe ( '350px' ) ;
77+ } ) ;
78+
79+ it ( 'sets center and zoom of the map' , ( ) => {
80+ const options = { center : { lat : 3 , lng : 5 } , zoom : 7 } ;
81+ mapSpy = createMapSpy ( options ) ;
82+ mapConstructorSpy = createMapConstructorSpy ( mapSpy ) . and . callThrough ( ) ;
83+
84+ const fixture = TestBed . createComponent ( TestApp ) ;
85+ fixture . componentInstance . center = options . center ;
86+ fixture . componentInstance . zoom = options . zoom ;
87+ fixture . detectChanges ( ) ;
88+
89+ const container = fixture . debugElement . query ( By . css ( 'div' ) ) ;
90+ expect ( mapConstructorSpy ) . toHaveBeenCalledWith ( container . nativeElement , options ) ;
91+
92+ fixture . componentInstance . center = { lat : 8 , lng : 9 } ;
93+ fixture . componentInstance . zoom = 12 ;
94+ fixture . detectChanges ( ) ;
95+
96+ expect ( mapSpy . setOptions ) . toHaveBeenCalledWith ( { center : { lat : 8 , lng : 9 } , zoom : 12 } ) ;
97+ } ) ;
98+
99+ it ( 'sets map options' , ( ) => {
100+ const options = { center : { lat : 3 , lng : 5 } , zoom : 7 , draggable : false } ;
101+ mapSpy = createMapSpy ( options ) ;
102+ mapConstructorSpy = createMapConstructorSpy ( mapSpy ) . and . callThrough ( ) ;
103+
104+ const fixture = TestBed . createComponent ( TestApp ) ;
105+ fixture . componentInstance . options = options ;
106+ fixture . detectChanges ( ) ;
107+
108+ const container = fixture . debugElement . query ( By . css ( 'div' ) ) ;
109+ expect ( mapConstructorSpy ) . toHaveBeenCalledWith ( container . nativeElement , options ) ;
110+
111+ fixture . componentInstance . options = { ...options , heading : 170 } ;
112+ fixture . detectChanges ( ) ;
113+
114+ expect ( mapSpy . setOptions ) . toHaveBeenCalledWith ( { ...options , heading : 170 } ) ;
115+ } ) ;
116+
117+ it ( 'gives precedence to center and zoom over options' , ( ) => {
118+ const inputOptions = { center : { lat : 3 , lng : 5 } , zoom : 7 , heading : 170 } ;
119+ const correctedOptions = { center : { lat : 12 , lng : 15 } , zoom : 5 , heading : 170 } ;
120+ mapSpy = createMapSpy ( correctedOptions ) ;
121+ mapConstructorSpy = createMapConstructorSpy ( mapSpy ) ;
122+
123+ const fixture = TestBed . createComponent ( TestApp ) ;
124+ fixture . componentInstance . center = correctedOptions . center ;
125+ fixture . componentInstance . zoom = correctedOptions . zoom ;
126+ fixture . componentInstance . options = inputOptions ;
127+ fixture . detectChanges ( ) ;
128+
129+ const container = fixture . debugElement . query ( By . css ( 'div' ) ) ;
130+ expect ( mapConstructorSpy ) . toHaveBeenCalledWith ( container . nativeElement , correctedOptions ) ;
37131 } ) ;
38132} ) ;
39133
40134@Component ( {
41135 selector : 'test-app' ,
42- template : `<google-map></google-map>` ,
136+ template : `<google-map [height]="height"
137+ [width]="width"
138+ [center]="center"
139+ [zoom]="zoom"
140+ [options]="options"></google-map>` ,
43141} )
44- class TestApp { }
142+ class TestApp {
143+ height ?: string ;
144+ width ?: string ;
145+ center ?: google . maps . LatLngLiteral ;
146+ zoom ?: number ;
147+ options ?: google . maps . MapOptions ;
148+ }
0 commit comments