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
This commit is contained in:
Peter Bacon Darwin 2017-07-26 22:36:25 +01:00 committed by Victor Berchet
parent 36161d99f6
commit 70b62949de
3 changed files with 46 additions and 3 deletions

View File

@ -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;
}},

View File

@ -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('') }
]);
}

View File

@ -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) {}
};