Ahmed Ayed e05a6f3bb3 feat(common): add historyGo method to Location service (#38890)
Add new method `historyGo`, that will let
the user navigate to a specific page from session history identified by its
relative position to the current page.

We add some tests to `location_spec.ts` to validate the behavior of the
`historyGo` and `forward` methods.

Add more tests for `location_spec` to test `location.historyGo(0)`, `location.historyGo()`,
`location.historyGo(100)` and `location.historyGo(-100)`. We also add new tests for
`Integration` spec to validate the navigation when we using
`location#historyGo`.

Update the `historyGo` function docs

Note that this was made an optional function in the abstract classes to
avoid a breaking change. Because our location classes use `implements PlatformLocation`
rather than `extends PlatformLocation`, simply adding a default
implementation was not sufficient to make this a non-breaking change.
While we could fix the classes internal to Angular, this would still have been
a breaking change for any external developers who may have followed our
implementations as an example.

PR Close #38890
2021-04-06 09:25:58 -07:00

67 lines
2.4 KiB
TypeScript

export declare const MOCK_PLATFORM_LOCATION_CONFIG: InjectionToken<MockPlatformLocationConfig>;
export declare class MockLocationStrategy extends LocationStrategy {
internalBaseHref: string;
internalPath: string;
internalTitle: string;
urlChanges: string[];
constructor();
back(): void;
forward(): void;
getBaseHref(): string;
getState(): unknown;
onPopState(fn: (value: any) => void): void;
path(includeHash?: boolean): string;
prepareExternalUrl(internal: string): string;
pushState(ctx: any, title: string, path: string, query: string): void;
replaceState(ctx: any, title: string, path: string, query: string): void;
simulatePopState(url: string): void;
}
export declare class MockPlatformLocation implements PlatformLocation {
get hash(): string;
get hostname(): string;
get href(): string;
get pathname(): string;
get port(): string;
get protocol(): string;
get search(): string;
get state(): unknown;
get url(): string;
constructor(config?: MockPlatformLocationConfig);
back(): void;
forward(): void;
getBaseHrefFromDOM(): string;
getState(): unknown;
historyGo(relativePosition?: number): void;
onHashChange(fn: LocationChangeListener): VoidFunction;
onPopState(fn: LocationChangeListener): VoidFunction;
pushState(state: any, title: string, newUrl: string): void;
replaceState(state: any, title: string, newUrl: string): void;
}
export declare interface MockPlatformLocationConfig {
appBaseHref?: string;
startUrl?: string;
}
export declare class SpyLocation implements Location {
urlChanges: string[];
back(): void;
forward(): void;
getState(): unknown;
go(path: string, query?: string, state?: any): void;
historyGo(relativePosition?: number): void;
isCurrentPathEqualTo(path: string, query?: string): boolean;
normalize(url: string): string;
onUrlChange(fn: (url: string, state: unknown) => void): void;
path(): string;
prepareExternalUrl(url: string): string;
replaceState(path: string, query?: string, state?: any): void;
setBaseHref(url: string): void;
setInitialPath(url: string): void;
simulateHashChange(pathname: string): void;
simulateUrlPop(pathname: string): void;
subscribe(onNext: (value: any) => void, onThrow?: ((error: any) => void) | null, onReturn?: (() => void) | null): SubscriptionLike;
}