From 70b62949dea47878c969105a6419bcfbcc20cc92 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 26 Jul 2017 22:36:25 +0100 Subject: [PATCH] feat(aio): enable deployment mode to be set via URL query The deployment mode set from the environment provided at build time; or overridden by the `mode` query parameter: e.g. `...?mode=archive` See #18287 --- aio/src/app/app.component.spec.ts | 5 +-- aio/src/app/shared/deployment.service.spec.ts | 32 +++++++++++++++++++ aio/src/app/shared/deployment.service.ts | 12 ++++++- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 aio/src/app/shared/deployment.service.spec.ts diff --git a/aio/src/app/app.component.spec.ts b/aio/src/app/app.component.spec.ts index fb1e9f9b0e..e68b2672d5 100644 --- a/aio/src/app/app.component.spec.ts +++ b/aio/src/app/app.component.spec.ts @@ -906,6 +906,7 @@ describe('AppComponent', () => { //// test helpers //// function createTestingModule(initialUrl: string, mode: string = 'stable') { + const mockLocationService = new MockLocationService(initialUrl); TestBed.resetTestingModule(); TestBed.configureTestingModule({ imports: [ AppModule ], @@ -913,11 +914,11 @@ function createTestingModule(initialUrl: string, mode: string = 'stable') { { provide: APP_BASE_HREF, useValue: '/' }, { provide: GaService, useClass: TestGaService }, { provide: Http, useClass: TestHttp }, - { provide: LocationService, useFactory: () => new MockLocationService(initialUrl) }, + { provide: LocationService, useFactory: () => mockLocationService }, { provide: Logger, useClass: MockLogger }, { provide: SearchService, useClass: MockSearchService }, { provide: Deployment, useFactory: () => { - const deployment = new Deployment(); + const deployment = new Deployment(mockLocationService as any); deployment.mode = mode; return deployment; }}, diff --git a/aio/src/app/shared/deployment.service.spec.ts b/aio/src/app/shared/deployment.service.spec.ts new file mode 100644 index 0000000000..34df4ec92e --- /dev/null +++ b/aio/src/app/shared/deployment.service.spec.ts @@ -0,0 +1,32 @@ +import { ReflectiveInjector } from '@angular/core'; +import { environment } from 'environments/environment'; +import { LocationService } from 'app/shared/location.service'; +import { MockLocationService } from 'testing/location.service'; +import { Deployment } from './deployment.service'; + +describe('Deployment service', () => { + describe('mode', () => { + it('should get the mode from the environment', () => { + environment.mode = 'foo'; + const deployment = getInjector().get(Deployment); + expect(deployment.mode).toEqual('foo'); + }); + + it('should get the mode from the `mode` query parameter if available', () => { + const injector = getInjector(); + + const locationService: MockLocationService = injector.get(LocationService); + locationService.search.and.returnValue({ mode: 'bar' }); + + const deployment = injector.get(Deployment); + expect(deployment.mode).toEqual('bar'); + }); + }); +}); + +function getInjector() { + return ReflectiveInjector.resolveAndCreate([ + Deployment, + { provide: LocationService, useFactory: () => new MockLocationService('') } + ]); +} diff --git a/aio/src/app/shared/deployment.service.ts b/aio/src/app/shared/deployment.service.ts index 9cf53dfd87..6a3f0ee79a 100644 --- a/aio/src/app/shared/deployment.service.ts +++ b/aio/src/app/shared/deployment.service.ts @@ -1,7 +1,17 @@ import { Injectable } from '@angular/core'; +import { LocationService } from 'app/shared/location.service'; import { environment } from 'environments/environment'; +/** + * Information about the deployment of this application. + */ @Injectable() export class Deployment { - mode: string = environment.mode; + /** + * The deployment mode set from the environment provided at build time; + * or overridden by the `mode` query parameter: e.g. `...?mode=archive` + */ + mode: string = this.location.search()['mode'] || environment.mode; + + constructor(private location: LocationService) {} };