Skip to content

Commit

Permalink
Revert "refactor(router): Update integration tests to cover navigatio…
Browse files Browse the repository at this point in the history
…n and history API (#53799)" (#53860)

This reverts commit eb2e879.

PR Close #53860
  • Loading branch information
atscott committed Jan 9, 2024
1 parent acf1793 commit 5996d3f
Show file tree
Hide file tree
Showing 7 changed files with 5,892 additions and 6,024 deletions.
1 change: 0 additions & 1 deletion packages/common/src/private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
*/

export {DomAdapter as ɵDomAdapter, getDOM as ɵgetDOM, setRootDomAdapter as ɵsetRootDomAdapter} from './dom_adapter';
export {PlatformNavigation as ɵPlatformNavigation} from './navigation/platform_navigation';
85 changes: 2 additions & 83 deletions packages/common/testing/src/mock_platform_location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
* found in the LICENSE file at https://angular.io/license
*/

import {DOCUMENT, LocationChangeEvent, LocationChangeListener, PlatformLocation, ɵPlatformNavigation as PlatformNavigation} from '@angular/common';
import {Inject, inject, Injectable, InjectionToken, Optional} from '@angular/core';
import {LocationChangeEvent, LocationChangeListener, PlatformLocation} from '@angular/common';
import {Inject, Injectable, InjectionToken, Optional} from '@angular/core';
import {Subject} from 'rxjs';

import {FakeNavigation} from './navigation/fake_navigation';

/**
* Parser from https://tools.ietf.org/html/rfc3986#appendix-B
* ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
Expand Down Expand Up @@ -252,82 +250,3 @@ export class MockPlatformLocation implements PlatformLocation {
}
}
}

/**
* Mock implementation of URL state.
*/
@Injectable()
export class FakeNavigationPlatformLocation implements PlatformLocation {
private _platformNavigation = inject(PlatformNavigation) as FakeNavigation;
private window = inject(DOCUMENT).defaultView!;

constructor() {
if (!(this._platformNavigation instanceof FakeNavigation)) {
throw new Error(
'FakePlatformNavigation cannot be used without FakeNavigation. Use ' +
'`provideFakeNavigation` to have all these services provided together.',
);
}
}

private config = inject(MOCK_PLATFORM_LOCATION_CONFIG, {optional: true});
getBaseHrefFromDOM(): string {
return this.config?.appBaseHref ?? '';
}

onPopState(fn: LocationChangeListener): VoidFunction {
this.window.addEventListener('popstate', fn);
return () => this.window.removeEventListener('popstate', fn);
}

onHashChange(fn: LocationChangeListener): VoidFunction {
this.window.addEventListener('hashchange', fn as any);
return () => this.window.removeEventListener('hashchange', fn as any);
}

get href(): string {
return this._platformNavigation.currentEntry.url!;
}
get protocol(): string {
return new URL(this._platformNavigation.currentEntry.url!).protocol;
}
get hostname(): string {
return new URL(this._platformNavigation.currentEntry.url!).hostname;
}
get port(): string {
return new URL(this._platformNavigation.currentEntry.url!).port;
}
get pathname(): string {
return new URL(this._platformNavigation.currentEntry.url!).pathname;
}
get search(): string {
return new URL(this._platformNavigation.currentEntry.url!).search;
}
get hash(): string {
return new URL(this._platformNavigation.currentEntry.url!).hash;
}

pushState(state: any, title: string, url: string): void {
this._platformNavigation.pushState(state, title, url);
}

replaceState(state: any, title: string, url: string): void {
this._platformNavigation.replaceState(state, title, url);
}

forward(): void {
this._platformNavigation.forward();
}

back(): void {
this._platformNavigation.back();
}

historyGo(relativePosition: number = 0): void {
this._platformNavigation.go(relativePosition);
}

getState(): unknown {
return this._platformNavigation.currentEntry.getHistoryState();
}
}
3 changes: 3 additions & 0 deletions packages/common/testing/src/navigation/fake_navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

// Prevents deletion of `Event` from `globalThis` during module loading.
const Event = globalThis.Event;

/**
* Fake implementation of user agent history and navigation behavior. This is a
* high-fidelity implementation of browser behavior that attempts to emulate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@
* found in the LICENSE file at https://angular.io/license
*/

import {DOCUMENT, PlatformLocation} from '@angular/common';
import {inject, Provider} from '@angular/core';
import {Provider} from '@angular/core';

// @ng_package: ignore-cross-repo-import
import {PlatformNavigation} from '../../../src/navigation/platform_navigation';
import {FakeNavigationPlatformLocation, MOCK_PLATFORM_LOCATION_CONFIG} from '../mock_platform_location';

import {FakeNavigation} from './fake_navigation';

/**
* Return a provider for the `FakeNavigation` in place of the real Navigation API.
*
* @internal
*/
export function provideFakePlatformNavigation(): Provider[] {
return [
{
provide: PlatformNavigation,
useFactory: () => {
const config = inject(MOCK_PLATFORM_LOCATION_CONFIG, {optional: true});
return new FakeNavigation(
inject(DOCUMENT).defaultView!,
config?.startUrl as `http${string}` ?? 'http://_empty_/');
return new FakeNavigation(window, 'https://test.com');
}
},
{provide: PlatformLocation, useClass: FakeNavigationPlatformLocation},
];
}
9 changes: 0 additions & 9 deletions packages/common/testing/src/private_export.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/common/testing/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
* @description
* Entry point for all public APIs of the common/testing package.
*/

export * from './private_export';
export {SpyLocation} from './location_mock';
export {MockLocationStrategy} from './mock_location_strategy';
export {MOCK_PLATFORM_LOCATION_CONFIG, MockPlatformLocation, MockPlatformLocationConfig} from './mock_platform_location';
Expand Down
Loading

0 comments on commit 5996d3f

Please sign in to comment.