Skip to content

Commit

Permalink
refactor(common): Fix fake navigation to work with relative navigatio…
Browse files Browse the repository at this point in the history
…ns (#53719)

This commit updates the implementation of the fake navigation to support
relative navigation requests.

PR Close #53719
  • Loading branch information
atscott committed Jan 3, 2024
1 parent e1164e4 commit 0b8a649
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
11 changes: 11 additions & 0 deletions packages/common/test/navigation/fake_platform_navigation.spec.ts
Expand Up @@ -227,6 +227,17 @@ describe('navigation', () => {
expect(locals.popStateEvents.length).toBe(1);
});

it('push URL relative', async () => {
locals.pendingInterceptOptions.push({});
await locals.navigation.navigate('/a/b/c').finished;
expect(locals.navigation.currentEntry.url).toBe('https://test.com/a/b/c');
locals.pendingInterceptOptions.push({
handler: () => Promise.resolve(),
});
await locals.navigation.navigate('../').finished;
expect(locals.navigation.currentEntry.url).toBe('https://test.com/a/');
});

it('replace URL', async () => {
const initialEntry = locals.navigation.currentEntry;
locals.pendingInterceptOptions.push({});
Expand Down
33 changes: 14 additions & 19 deletions packages/common/testing/src/navigation/fake_navigation.ts
Expand Up @@ -83,22 +83,17 @@ export class FakeNavigation implements Navigation {
return this.currentEntryIndex < this.entriesArr.length - 1;
}

constructor(private readonly window: Window, private readonly baseURI: string) {
constructor(private readonly window: Window, startURL: `http${string}`) {
// First entry.
this.setInitialEntryForTesting('.');
this.setInitialEntryForTesting(startURL);
}

/**
* Sets the initial entry.
*/
setInitialEntryForTesting(
url: string,
options: {
historyState: unknown;
// Allows setting the URL without resolving it against the base.
absoluteUrl?: boolean;
state?: unknown;
} = {historyState: null},
private setInitialEntryForTesting(
url: `http${string}`,
options: {historyState: unknown; state?: unknown;} = {historyState: null},
) {
if (!this.canSetInitialEntry) {
throw new Error(
Expand All @@ -108,7 +103,7 @@ export class FakeNavigation implements Navigation {
}
const currentInitialEntry = this.entriesArr[0];
this.entriesArr[0] = new FakeNavigationHistoryEntry(
options.absoluteUrl ? url : new URL(url, this.baseURI).toString(),
new URL(url).toString(),
{
index: 0,
key: currentInitialEntry?.key ?? String(this.nextKey++),
Expand Down Expand Up @@ -143,8 +138,8 @@ export class FakeNavigation implements Navigation {
url: string,
options?: NavigationNavigateOptions,
): FakeNavigationResult {
const fromUrl = new URL(this.currentEntry.url!, this.baseURI);
const toUrl = new URL(url, this.baseURI);
const fromUrl = new URL(this.currentEntry.url!);
const toUrl = new URL(url, this.currentEntry.url!);

let navigationType: NavigationTypeString;
if (!options?.history || options.history === 'auto') {
Expand Down Expand Up @@ -200,8 +195,8 @@ export class FakeNavigation implements Navigation {
_title: string,
url?: string,
): void {
const fromUrl = new URL(this.currentEntry.url!, this.baseURI);
const toUrl = url ? new URL(url, this.baseURI) : fromUrl;
const fromUrl = new URL(this.currentEntry.url!);
const toUrl = url ? new URL(url, this.currentEntry.url!) : fromUrl;

const hashChange = isHashChange(fromUrl, toUrl);

Expand All @@ -225,7 +220,7 @@ export class FakeNavigation implements Navigation {

/** Equivalent to `navigation.traverseTo()`. */
traverseTo(key: string, options?: NavigationOptions): FakeNavigationResult {
const fromUrl = new URL(this.currentEntry.url!, this.baseURI);
const fromUrl = new URL(this.currentEntry.url!);
const entry = this.findEntry(key);
if (!entry) {
const domException = new DOMException(
Expand Down Expand Up @@ -255,7 +250,7 @@ export class FakeNavigation implements Navigation {
};
}

const hashChange = isHashChange(fromUrl, new URL(entry.url!, this.baseURI));
const hashChange = isHashChange(fromUrl, new URL(entry.url!, this.currentEntry.url!));
const destination = new FakeNavigationDestination({
url: entry.url!,
state: entry.getState(),
Expand Down Expand Up @@ -344,9 +339,9 @@ export class FakeNavigation implements Navigation {
if (targetIndex >= this.entriesArr.length || targetIndex < 0) {
return;
}
const fromUrl = new URL(this.currentEntry.url!, this.baseURI);
const fromUrl = new URL(this.currentEntry.url!);
const entry = this.entriesArr[targetIndex];
const hashChange = isHashChange(fromUrl, new URL(entry.url!, this.baseURI));
const hashChange = isHashChange(fromUrl, new URL(entry.url!, this.currentEntry.url!));
const destination = new FakeNavigationDestination({
url: entry.url!,
state: entry.getState(),
Expand Down

0 comments on commit 0b8a649

Please sign in to comment.