@@ -2,22 +2,18 @@ import {Component} from '@angular/core';
2
2
import { async , TestBed } from '@angular/core/testing' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
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' ;
12
11
13
12
describe ( 'GoogleMap' , ( ) => {
14
13
let mapConstructorSpy : jasmine . Spy ;
15
14
let mapSpy : jasmine . SpyObj < google . maps . Map > ;
16
15
17
16
beforeEach ( async ( ( ) => {
18
- mapSpy = createMapSpy ( DEFAULT_OPTIONS ) ;
19
- mapConstructorSpy = createMapConstructorSpy ( mapSpy ) ;
20
-
21
17
TestBed . configureTestingModule ( {
22
18
imports : [ GoogleMapModule ] ,
23
19
declarations : [ TestApp ] ,
@@ -28,17 +24,125 @@ describe('GoogleMap', () => {
28
24
TestBed . compileComponents ( ) ;
29
25
} ) ;
30
26
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
+
31
44
it ( 'initializes a Google map' , ( ) => {
45
+ mapSpy = createMapSpy ( DEFAULT_OPTIONS ) ;
46
+ mapConstructorSpy = createMapConstructorSpy ( mapSpy ) ;
47
+
32
48
const fixture = TestBed . createComponent ( TestApp ) ;
49
+ fixture . detectChanges ( ) ;
50
+
33
51
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' ;
34
64
fixture . detectChanges ( ) ;
35
65
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' ) ;
36
69
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 ) ;
37
131
} ) ;
38
132
} ) ;
39
133
40
134
@Component ( {
41
135
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>` ,
43
141
} )
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