From cb2c4654318aa3e5d40509b86ac18d765e2272ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20M=C3=BCller?= Date: Thu, 15 Sep 2016 19:36:24 +0200 Subject: [PATCH] fix(LazyMapsAPILoader): HTTP loading mode Fixes: #644 Closes #655 --- .../maps-api-loader/lazy-maps-api-loader.ts | 6 +- .../lazy-maps-api-loader.spec.ts | 83 +++++++++++++------ 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/core/services/maps-api-loader/lazy-maps-api-loader.ts b/src/core/services/maps-api-loader/lazy-maps-api-loader.ts index 334ac10ff..c59bd8603 100644 --- a/src/core/services/maps-api-loader/lazy-maps-api-loader.ts +++ b/src/core/services/maps-api-loader/lazy-maps-api-loader.ts @@ -5,9 +5,9 @@ import {DOCUMENT_GLOBAL, WINDOW_GLOBAL} from '../../utils/browser-globals'; import {MapsAPILoader} from './maps-api-loader'; export enum GoogleMapsScriptProtocol { - HTTP, - HTTPS, - AUTO + HTTP = 1, + HTTPS = 2, + AUTO = 3 } /** diff --git a/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts b/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts index 317d42b33..11321c7c5 100644 --- a/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts +++ b/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts @@ -1,12 +1,12 @@ import {TestBed, inject} from '@angular/core/testing'; -import {LazyMapsAPILoader} from '../../../src/core/services/maps-api-loader/lazy-maps-api-loader'; +import {GoogleMapsScriptProtocol, LazyMapsAPILoader, LazyMapsAPILoaderConfig} from '../../../src/core/services/maps-api-loader/lazy-maps-api-loader'; import {MapsAPILoader} from '../../../src/core/services/maps-api-loader/maps-api-loader'; import {DOCUMENT_GLOBAL, WINDOW_GLOBAL} from '../../../src/core/utils/browser-globals'; export function main() { describe('Service: LazyMapsAPILoader', () => { - beforeEach(() => { + it('should create the default script URL', () => { TestBed.configureTestingModule({ providers: [ {provide: MapsAPILoader, useClass: LazyMapsAPILoader}, @@ -16,30 +16,63 @@ export function main() { } ] }); + + inject([MapsAPILoader, DOCUMENT_GLOBAL], (loader: LazyMapsAPILoader, doc: Document) => { + interface Script { + src?: string; + async?: boolean; + defer?: boolean; + type?: string; + } + const scriptElem: Script = {}; + (doc.createElement).and.returnValue(scriptElem); + doc.body = jasmine.createSpyObj('body', ['appendChild']); + + loader.load(); + expect(doc.createElement).toHaveBeenCalled(); + expect(scriptElem.type).toEqual('text/javascript'); + expect(scriptElem.async).toEqual(true); + expect(scriptElem.defer).toEqual(true); + expect(scriptElem.src).toBeDefined(); + expect(scriptElem.src).toContain('https://maps.googleapis.com/maps/api/js'); + expect(scriptElem.src).toContain('v=3'); + expect(scriptElem.src).toContain('callback=angular2GoogleMapsLazyMapsAPILoader'); + expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem); + }); }); - it('should create the default script URL', - inject([MapsAPILoader, DOCUMENT_GLOBAL], (loader: LazyMapsAPILoader, doc: Document) => { - interface Script { - src?: string; - async?: boolean; - defer?: boolean; - type?: string; - } - const scriptElem: Script = {}; - (doc.createElement).and.returnValue(scriptElem); - doc.body = jasmine.createSpyObj('body', ['appendChild']); - - loader.load(); - expect(doc.createElement).toHaveBeenCalled(); - expect(scriptElem.type).toEqual('text/javascript'); - expect(scriptElem.async).toEqual(true); - expect(scriptElem.defer).toEqual(true); - expect(scriptElem.src).toBeDefined(); - expect(scriptElem.src).toContain('https://maps.googleapis.com/maps/api/js'); - expect(scriptElem.src).toContain('v=3'); - expect(scriptElem.src).toContain('callback=angular2GoogleMapsLazyMapsAPILoader'); - expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem); - })); + it('should load the script via http when provided', () => { + const lazyLoadingConf: LazyMapsAPILoaderConfig = new LazyMapsAPILoaderConfig(); + lazyLoadingConf.protocol = GoogleMapsScriptProtocol.HTTP; + + TestBed.configureTestingModule({ + providers: [ + {provide: MapsAPILoader, useClass: LazyMapsAPILoader}, + {provide: WINDOW_GLOBAL, useValue: {}}, { + provide: DOCUMENT_GLOBAL, + useValue: jasmine.createSpyObj('Document', ['createElement']) + } + ] + }); + + inject([MapsAPILoader, DOCUMENT_GLOBAL], (loader: LazyMapsAPILoader, doc: Document) => { + interface Script { + src?: string; + async?: boolean; + defer?: boolean; + type?: string; + } + const scriptElem: Script = {}; + (doc.createElement).and.returnValue(scriptElem); + doc.body = jasmine.createSpyObj('body', ['appendChild']); + + loader.load(); + expect(doc.createElement).toHaveBeenCalled(); + expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js'); + expect(scriptElem.src).toContain('v=3'); + expect(scriptElem.src).toContain('callback=angular2GoogleMapsLazyMapsAPILoader'); + expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem); + }); + }); }); }