1 | import {TestBed, inject} from '@angular/core/testing';
|
2 |
|
3 | import {DocumentRef, WindowRef} from '../../utils/browser-globals';
|
4 |
|
5 | import {GoogleMapsScriptProtocol, LAZY_MAPS_API_CONFIG, LazyMapsAPILoader, LazyMapsAPILoaderConfigLiteral} from './lazy-maps-api-loader';
|
6 | import {MapsAPILoader} from './maps-api-loader';
|
7 |
|
8 | describe('Service: LazyMapsAPILoader', () => {
|
9 | let documentRef: DocumentRef;
|
10 | let doc: any;
|
11 | let windowRef: any;
|
12 |
|
13 | beforeEach(() => {
|
14 | doc = jasmine.createSpyObj<DocumentRef>('Document', ['createElement']);
|
15 | documentRef = jasmine.createSpyObj<DocumentRef>('Document', ['getNativeDocument']);
|
16 | (<any>documentRef.getNativeDocument).and.returnValue(doc);
|
17 | windowRef = {};
|
18 | });
|
19 |
|
20 | it('should create the default script URL', () => {
|
21 | TestBed.configureTestingModule({
|
22 | providers: [
|
23 | {provide: MapsAPILoader, useClass: LazyMapsAPILoader},
|
24 | {provide: WindowRef, useValue: windowRef}, {provide: DocumentRef, useValue: documentRef}
|
25 | ]
|
26 | });
|
27 |
|
28 | inject([MapsAPILoader], (loader: LazyMapsAPILoader) => {
|
29 | interface Script {
|
30 | src?: string;
|
31 | async?: boolean;
|
32 | defer?: boolean;
|
33 | type?: string;
|
34 | }
|
35 | const scriptElem: Script = {};
|
36 | (<jasmine.Spy>doc.createElement).and.returnValue(scriptElem);
|
37 | doc.body = jasmine.createSpyObj('body', ['appendChild']);
|
38 |
|
39 | loader.load();
|
40 | expect(doc.createElement).toHaveBeenCalled();
|
41 | expect(scriptElem.type).toEqual('text/javascript');
|
42 | expect(scriptElem.async).toEqual(true);
|
43 | expect(scriptElem.defer).toEqual(true);
|
44 | expect(scriptElem.src).toBeDefined();
|
45 | expect(scriptElem.src).toContain('https://maps.googleapis.com/maps/api/js');
|
46 | expect(scriptElem.src).toContain('v=3');
|
47 | expect(scriptElem.src).toContain('callback=angular2GoogleMapsLazyMapsAPILoader');
|
48 | expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem);
|
49 | });
|
50 | });
|
51 |
|
52 | it('should load the script via http when provided', () => {
|
53 | const lazyLoadingConf:
|
54 | LazyMapsAPILoaderConfigLiteral = {protocol: GoogleMapsScriptProtocol.HTTP};
|
55 |
|
56 | TestBed.configureTestingModule({
|
57 | providers: [
|
58 | {provide: MapsAPILoader, useClass: LazyMapsAPILoader},
|
59 | {provide: WindowRef, useValue: windowRef}, {provide: DocumentRef, useValue: documentRef},
|
60 | {provide: LAZY_MAPS_API_CONFIG, useValue: lazyLoadingConf}
|
61 | ]
|
62 | });
|
63 |
|
64 | inject([MapsAPILoader], (loader: LazyMapsAPILoader) => {
|
65 | interface Script {
|
66 | src?: string;
|
67 | async?: boolean;
|
68 | defer?: boolean;
|
69 | type?: string;
|
70 | }
|
71 | const scriptElem: Script = {};
|
72 | (<jasmine.Spy>doc.createElement).and.returnValue(scriptElem);
|
73 | doc.body = jasmine.createSpyObj('body', ['appendChild']);
|
74 |
|
75 | loader.load();
|
76 | expect(doc.createElement).toHaveBeenCalled();
|
77 | 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 | expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem);
|
81 | });
|
82 | });
|
83 | });
|