-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(angular): honor modifier-click on routerLink #31230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/angular/test/base/e2e/src/standalone/router-link-modifier-click.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { test, expect, type Page } from '@playwright/test'; | ||
|
|
||
| /** | ||
| * Issue #26394: a modifier click (ctrl/meta/shift/alt) or a non-`_self` target on | ||
| * a `routerLink` over a non-anchor Ionic component (ion-item, ion-button) must let | ||
| * the browser handle the navigation natively (new tab, window, download) instead | ||
| * of navigating the current page in-app. | ||
| * | ||
| * Playwright headless can't dispatch a real modifier-click or observe the | ||
| * browser's native new-tab default, so we dispatch a synthetic click on the | ||
| * host (not the shadow anchor, which would fire its own default navigation) and | ||
| * assert the JS handler chain leaves the page unchanged with `preventDefault` | ||
| * uncalled. | ||
| */ | ||
| test.describe('RouterLink: modifier click', () => { | ||
| const dispatchClick = (page: Page, selector: string, modifiers: Record<string, boolean>) => | ||
| page.evaluate( | ||
| ({ selector, mods }: { selector: string; mods: Record<string, boolean> }) => { | ||
| const item = document.querySelector(selector) as HTMLElement; | ||
| const ev = new MouseEvent('click', { bubbles: true, composed: true, cancelable: true, button: 0, ...mods }); | ||
| item.dispatchEvent(ev); | ||
| return { defaultPrevented: ev.defaultPrevented }; | ||
| }, | ||
| { selector, mods: modifiers } | ||
| ); | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto('/standalone/router-link'); | ||
| }); | ||
|
|
||
| test('normal click navigates the current page in-app', async ({ page }) => { | ||
| const { defaultPrevented } = await dispatchClick(page, '#modifier-item', {}); | ||
|
|
||
| await expect(page).toHaveURL(/.*\/standalone\/popover/); | ||
| // The delegate prevents the default to stop a hard page reload. | ||
| expect(defaultPrevented).toBe(true); | ||
| }); | ||
|
|
||
| for (const modifier of ['ctrlKey', 'metaKey', 'shiftKey', 'altKey']) { | ||
| test(`${modifier}+click does not navigate the current page and allows native handling`, async ({ | ||
| page, | ||
| }, testInfo) => { | ||
| testInfo.annotations.push({ | ||
| type: 'issue', | ||
| description: 'https://github.com/ionic-team/ionic-framework/issues/26394', | ||
| }); | ||
|
|
||
| const { defaultPrevented } = await dispatchClick(page, '#modifier-item', { [modifier]: true }); | ||
|
|
||
| // Give any in-app navigation a chance to run before asserting it did not. | ||
| await page.waitForTimeout(300); | ||
| await expect(page).toHaveURL(/.*\/standalone\/router-link/); | ||
| // Default is left intact so the browser can handle the link natively | ||
| // (new tab, new window, or download, depending on the browser and OS). | ||
| expect(defaultPrevented).toBe(false); | ||
| }); | ||
| } | ||
|
|
||
| test('click on a non-_self target does not navigate the current page and allows a native new tab', async ({ | ||
| page, | ||
| }, testInfo) => { | ||
| testInfo.annotations.push({ | ||
| type: 'issue', | ||
| description: 'https://github.com/ionic-team/ionic-framework/issues/26394', | ||
| }); | ||
|
|
||
| const { defaultPrevented } = await dispatchClick(page, '#target-item', {}); | ||
|
|
||
| // Give any in-app navigation a chance to run before asserting it did not. | ||
| await page.waitForTimeout(300); | ||
| await expect(page).toHaveURL(/.*\/standalone\/router-link/); | ||
| // Default is left intact so the browser can open the link in a new tab. | ||
| expect(defaultPrevented).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/angular/test/base/src/app/standalone/router-link/router-link.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| import { Component } from '@angular/core'; | ||
| import { RouterLink } from '@angular/router'; | ||
| import { IonRouterLink, IonRouterLinkWithHref } from '@ionic/angular/standalone'; | ||
| import { IonItem, IonRouterLink, IonRouterLinkWithHref } from '@ionic/angular/standalone'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-router-link', | ||
| templateUrl: './router-link.component.html', | ||
| standalone: true, | ||
| imports: [RouterLink, IonRouterLink, IonRouterLinkWithHref], | ||
| imports: [RouterLink, IonItem, IonRouterLink, IonRouterLinkWithHref], | ||
| }) | ||
| export class RouterLinkComponent {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.