Skip to content

Commit

Permalink
fix(LazyMapsAPILoader): HTTP loading mode
Browse files Browse the repository at this point in the history
Fixes: #644
Closes #655
  • Loading branch information
sebholstein committed Sep 15, 2016
1 parent 3fb6603 commit cb2c465
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/core/services/maps-api-loader/lazy-maps-api-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
83 changes: 58 additions & 25 deletions test/services/maps-api-loader/lazy-maps-api-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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 = {};
(<jasmine.Spy>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 = {};
(<jasmine.Spy>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>('Document', ['createElement'])
}
]
});

inject([MapsAPILoader, DOCUMENT_GLOBAL], (loader: LazyMapsAPILoader, doc: Document) => {
interface Script {
src?: string;
async?: boolean;
defer?: boolean;
type?: string;
}
const scriptElem: Script = {};
(<jasmine.Spy>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);
});
});
});
}

0 comments on commit cb2c465

Please sign in to comment.