Skip to content

Commit

Permalink
feat: google map language to default to LOCALE_ID (#1754)
Browse files Browse the repository at this point in the history
the language of google maps should default to angular's LOCALE_ID, and
not en-US
  • Loading branch information
doom777 committed Nov 5, 2019
1 parent 57685f2 commit 56a858e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
57 changes: 42 additions & 15 deletions packages/core/services/maps-api-loader/lazy-maps-api-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LOCALE_ID } from '@angular/core';
import { inject, TestBed } from '@angular/core/testing';

import { DocumentRef, WindowRef } from '../../utils/browser-globals';

import { GoogleMapsScriptProtocol, LAZY_MAPS_API_CONFIG, LazyMapsAPILoader, LazyMapsAPILoaderConfigLiteral } from './lazy-maps-api-loader';
import { MapsAPILoader } from './maps-api-loader';

Expand Down Expand Up @@ -32,6 +31,7 @@ describe('Service: LazyMapsAPILoader', () => {
{provide: MapsAPILoader, useClass: LazyMapsAPILoader},
{provide: WindowRef, useValue: windowRef},
{provide: DocumentRef, useValue: documentRef},
{provide: LOCALE_ID, useValue: 'en-US'},
],
});
});
Expand Down Expand Up @@ -83,24 +83,51 @@ describe('Service: LazyMapsAPILoader', () => {
{provide: MapsAPILoader, useClass: LazyMapsAPILoader},
{provide: WindowRef, useValue: windowRef},
{provide: DocumentRef, useValue: documentRef},
{provide: LOCALE_ID, useValue: 'en-US'},
{provide: LAZY_MAPS_API_CONFIG, useValue: lazyLoadingConf},
],
});

inject([MapsAPILoader], (loader: LazyMapsAPILoader) => {
interface Script {
src?: string;
async?: boolean;
defer?: boolean;
type?: string;
}
const scriptElem: Script = {};
(doc.createElement as jest.Mock).mockReturnValue(scriptElem);
const loader: LazyMapsAPILoader = TestBed.get(MapsAPILoader);

loader.load();
expect(doc.createElement).toHaveBeenCalled();
expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js');
expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem);
interface Script {
src?: string;
async?: boolean;
defer?: boolean;
type?: string;
}
const scriptElem: Script = {};
(doc.createElement as jest.Mock).mockReturnValue(scriptElem);

loader.load();
expect(doc.createElement).toHaveBeenCalled();
expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js');
expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem);
});

it('should load language based on locale', () => {
TestBed.configureTestingModule({
providers: [
{provide: MapsAPILoader, useClass: LazyMapsAPILoader},
{provide: WindowRef, useValue: windowRef},
{provide: DocumentRef, useValue: documentRef},
{provide: LOCALE_ID, useValue: 'es'},
],
});

const loader: LazyMapsAPILoader = TestBed.get(MapsAPILoader);
interface Script {
src?: string;
async?: boolean;
defer?: boolean;
type?: string;
}
const scriptElem: Script = {};
(doc.createElement as jest.Mock).mockReturnValue(scriptElem);

loader.load();
expect(doc.createElement).toHaveBeenCalled();
const url = new URL(scriptElem.src);
expect(url.searchParams.get('language')).toEqual('es');
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { Inject, Injectable, InjectionToken, LOCALE_ID, Optional } from '@angular/core';

import { DocumentRef, WindowRef } from '../../utils/browser-globals';

Expand Down Expand Up @@ -87,7 +87,8 @@ export class LazyMapsAPILoader extends MapsAPILoader {
protected readonly _SCRIPT_ID: string = 'agmGoogleMapsApiScript';
protected readonly callbackName: string = `agmLazyMapsAPILoader`;

constructor(@Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any = null, w: WindowRef, d: DocumentRef) {
constructor(@Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any = null, w: WindowRef, d: DocumentRef,
@Inject(LOCALE_ID) private localeId: string) {
super();
this._config = config || {};
this._windowRef = w;
Expand Down Expand Up @@ -161,7 +162,7 @@ export class LazyMapsAPILoader extends MapsAPILoader {
channel: this._config.channel,
libraries: this._config.libraries,
region: this._config.region,
language: this._config.language,
language: this._config.language || this.localeId !== 'en-US' ? this.localeId : null,
};
const params: string = Object.keys(queryParams)
.filter((k: string) => queryParams[k] != null)
Expand Down

0 comments on commit 56a858e

Please sign in to comment.