@@ -8,46 +8,66 @@ import {MapsAPILoader} from './maps-api-loader';
8
8
describe ( 'Service: LazyMapsAPILoader' , ( ) => {
9
9
let documentRef : DocumentRef ;
10
10
let doc : any ;
11
- let windowRef : any ;
11
+ let windowRef : WindowRef ;
12
+ let windowObj : any ;
12
13
13
14
beforeEach ( ( ) => {
14
- doc = jasmine . createSpyObj < DocumentRef > ( 'Document' , [ 'createElement' ] ) ;
15
+ doc = jasmine . createSpyObj < DocumentRef > ( 'Document' , [ 'createElement' , 'getElementById' ] ) ;
16
+ ( < jasmine . Spy > doc . getElementById ) . and . returnValue ( null ) ;
17
+ doc . body = jasmine . createSpyObj ( 'body' , [ 'appendChild' ] ) ;
15
18
documentRef = jasmine . createSpyObj < DocumentRef > ( 'Document' , [ 'getNativeDocument' ] ) ;
16
- ( < any > documentRef . getNativeDocument ) . and . returnValue ( doc ) ;
17
- windowRef = { } ;
18
- } ) ;
19
+ ( < jasmine . Spy > documentRef . getNativeDocument ) . and . returnValue ( doc ) ;
20
+
21
+ windowRef = jasmine . createSpyObj < WindowRef > ( 'windowRef' , [ 'getNativeWindow' ] ) ;
22
+ windowObj = { } ;
23
+ ( < jasmine . Spy > windowRef . getNativeWindow ) . and . returnValue ( windowObj ) ;
19
24
20
- it ( 'should create the default script URL' , ( ) => {
21
25
TestBed . configureTestingModule ( {
22
26
providers : [
23
27
{ provide : MapsAPILoader , useClass : LazyMapsAPILoader } ,
24
- { provide : WindowRef , useValue : windowRef } , { provide : DocumentRef , useValue : documentRef }
28
+ { provide : WindowRef , useValue : windowRef } ,
29
+ { provide : DocumentRef , useValue : documentRef }
25
30
]
26
31
} ) ;
32
+ } ) ;
27
33
28
- inject ( [ MapsAPILoader ] , ( loader : LazyMapsAPILoader ) => {
34
+ it ( 'should create the default script URL' , inject ( [ MapsAPILoader ] , ( loader : LazyMapsAPILoader ) => {
29
35
interface Script {
30
36
src ?: string ;
31
37
async ?: boolean ;
32
38
defer ?: boolean ;
33
39
type ?: string ;
40
+ id ?: string ;
34
41
}
35
42
const scriptElem : Script = { } ;
36
43
( < jasmine . Spy > doc . createElement ) . and . returnValue ( scriptElem ) ;
37
- doc . body = jasmine . createSpyObj ( 'body' , [ 'appendChild' ] ) ;
38
44
39
45
loader . load ( ) ;
40
- expect ( doc . createElement ) . toHaveBeenCalled ( ) ;
46
+ expect ( doc . createElement ) . toHaveBeenCalledWith ( 'script' ) ;
41
47
expect ( scriptElem . type ) . toEqual ( 'text/javascript' ) ;
42
48
expect ( scriptElem . async ) . toEqual ( true ) ;
43
49
expect ( scriptElem . defer ) . toEqual ( true ) ;
44
50
expect ( scriptElem . src ) . toBeDefined ( ) ;
51
+ expect ( scriptElem . id ) . toEqual ( 'agmGoogleMapsApiScript' ) ;
45
52
expect ( scriptElem . src ) . toContain ( 'https://maps.googleapis.com/maps/api/js' ) ;
46
53
expect ( scriptElem . src ) . toContain ( 'v=3' ) ;
47
- expect ( scriptElem . src ) . toContain ( 'callback=angular2GoogleMapsLazyMapsAPILoader ' ) ;
54
+ expect ( scriptElem . src ) . toContain ( 'callback=agmLazyMapsAPILoader ' ) ;
48
55
expect ( doc . body . appendChild ) . toHaveBeenCalledWith ( scriptElem ) ;
49
- } ) ;
50
- } ) ;
56
+ } ) ) ;
57
+
58
+ it ( 'should not append a second script to body when theres already one with the fixed ID' , inject ( [ MapsAPILoader ] , ( loader : LazyMapsAPILoader ) => {
59
+ ( < jasmine . Spy > doc . getElementById ) . and . returnValue ( document . createElement ( 'script' ) ) ;
60
+ loader . load ( ) ;
61
+ expect ( doc . body . appendChild ) . not . toHaveBeenCalledWith ( ) ;
62
+ } ) ) ;
63
+
64
+ it ( 'should not append a second script to body when window.google.maps is defined' , inject ( [ MapsAPILoader ] , ( loader : LazyMapsAPILoader ) => {
65
+ windowObj . google = {
66
+ maps : { }
67
+ } ;
68
+ loader . load ( ) ;
69
+ expect ( doc . body . appendChild ) . not . toHaveBeenCalledWith ( ) ;
70
+ } ) ) ;
51
71
52
72
it ( 'should load the script via http when provided' , ( ) => {
53
73
const lazyLoadingConf :
@@ -56,7 +76,8 @@ describe('Service: LazyMapsAPILoader', () => {
56
76
TestBed . configureTestingModule ( {
57
77
providers : [
58
78
{ provide : MapsAPILoader , useClass : LazyMapsAPILoader } ,
59
- { provide : WindowRef , useValue : windowRef } , { provide : DocumentRef , useValue : documentRef } ,
79
+ { provide : WindowRef , useValue : windowRef } ,
80
+ { provide : DocumentRef , useValue : documentRef } ,
60
81
{ provide : LAZY_MAPS_API_CONFIG , useValue : lazyLoadingConf }
61
82
]
62
83
} ) ;
@@ -70,13 +91,10 @@ describe('Service: LazyMapsAPILoader', () => {
70
91
}
71
92
const scriptElem : Script = { } ;
72
93
( < jasmine . Spy > doc . createElement ) . and . returnValue ( scriptElem ) ;
73
- doc . body = jasmine . createSpyObj ( 'body' , [ 'appendChild' ] ) ;
74
94
75
95
loader . load ( ) ;
76
96
expect ( doc . createElement ) . toHaveBeenCalled ( ) ;
77
97
expect ( scriptElem . src ) . toContain ( 'http://maps.googleapis.com/maps/api/js' ) ;
78
- expect ( scriptElem . src ) . toContain ( 'v=3' ) ;
79
- expect ( scriptElem . src ) . toContain ( 'callback=angular2GoogleMapsLazyMapsAPILoader' ) ;
80
98
expect ( doc . body . appendChild ) . toHaveBeenCalledWith ( scriptElem ) ;
81
99
} ) ;
82
100
} ) ;
0 commit comments