From 42ef1be75ca882c69d3d193018beb06d9c5f49f1 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 26 Jul 2017 18:30:38 +0100 Subject: [PATCH] feat(aio): redirect marketing pages to `docs` if deploy mode is `archive` See #18287 --- aio/src/app/app.component.spec.ts | 91 ++++++++++++++++++++++++++ aio/src/app/app.component.ts | 5 ++ aio/src/app/shared/location.service.ts | 4 ++ aio/src/testing/location.service.ts | 1 + 4 files changed, 101 insertions(+) diff --git a/aio/src/app/app.component.spec.ts b/aio/src/app/app.component.spec.ts index 30825b2b89..6353be5f80 100644 --- a/aio/src/app/app.component.spec.ts +++ b/aio/src/app/app.component.spec.ts @@ -765,6 +765,97 @@ describe('AppComponent', () => { }); }); + describe('archive redirection', () => { + it('should redirect to `docs` if deployment mode is `archive` and not at a docs page', () => { + createTestingModule('', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).toHaveBeenCalledWith('docs'); + + createTestingModule('resources', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).toHaveBeenCalledWith('docs'); + + createTestingModule('guide/aot-compiler', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('tutorial', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('tutorial/toh-pt1', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('docs', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('api', 'archive'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + }); + + it('should redirect to `docs` if deployment mode is `next` and not at a docs page', () => { + createTestingModule('', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).toHaveBeenCalledWith('docs'); + + createTestingModule('resources', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).toHaveBeenCalledWith('docs'); + + createTestingModule('guide/aot-compiler', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('tutorial', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('tutorial/toh-pt1', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('docs', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('api', 'next'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + }); + + it('should not redirect to `docs` if deployment mode is `stable` and not at a docs page', () => { + createTestingModule('', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('resources', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('guide/aot-compiler', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('tutorial', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('tutorial/toh-pt1', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('docs', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + + createTestingModule('api', 'stable'); + initializeTest(); + expect(TestBed.get(LocationService).replace).not.toHaveBeenCalled(); + }); + }); }); describe('with mocked DocViewer', () => { diff --git a/aio/src/app/app.component.ts b/aio/src/app/app.component.ts index 71426b0c82..84c283ded8 100644 --- a/aio/src/app/app.component.ts +++ b/aio/src/app/app.component.ts @@ -129,6 +129,11 @@ export class AppComponent implements OnInit { }); this.locationService.currentPath.subscribe(path => { + // Redirect to docs if we are in not in stable mode and are not hitting a docs page + // (i.e. we have arrived at a marketing page) + if (this.deployment.mode !== 'stable' && !/^(docs$|api$|guide|tutorial)/.test(path)) { + this.locationService.replace('docs'); + } if (path === this.currentPath) { // scroll only if on same page (most likely a change to the hash) this.autoScroll(); diff --git a/aio/src/app/shared/location.service.ts b/aio/src/app/shared/location.service.ts index 00d042ad70..52ecacfa5c 100644 --- a/aio/src/app/shared/location.service.ts +++ b/aio/src/app/shared/location.service.ts @@ -55,6 +55,10 @@ export class LocationService { window.location.assign(url); } + replace(url: string) { + window.location.replace(url); + } + private stripSlashes(url: string) { return url.replace(/^\/+/, '').replace(/\/+(\?|#|$)/, '$1'); } diff --git a/aio/src/testing/location.service.ts b/aio/src/testing/location.service.ts index 2656769b95..1e03a69626 100644 --- a/aio/src/testing/location.service.ts +++ b/aio/src/testing/location.service.ts @@ -10,6 +10,7 @@ export class MockLocationService { go = jasmine.createSpy('Location.go').and .callFake((url: string) => this.urlSubject.next(url)); goExternal = jasmine.createSpy('Location.goExternal'); + replace = jasmine.createSpy('Location.replace'); handleAnchorClick = jasmine.createSpy('Location.handleAnchorClick') .and.returnValue(false); // prevent click from causing a browser navigation