Files
next.js/test/e2e/link-on-navigate-prop/index.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

186 lines
6.0 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
describe('<Link /> onNavigate prop', () => {
const { next } = nextTestSetup({
files: __dirname,
})
const routers = [
{ name: 'App Router', path: '/app-router' },
{ name: 'Pages Router', path: '/pages-router' },
]
routers.forEach(({ name, path }) => {
describe(name, () => {
it('should trigger onClick but not onNavigate when using modifier key', async () => {
const browser = await next.browser(path)
// Check initial state
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: false'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
// Click with modifier key based on OS to open in new window
const platform = process.platform
const modifierKey = platform === 'darwin' ? 'Meta' : 'Control'
// First press down the modifier key
await browser.keydown(modifierKey)
// Click the link while the modifier key is pressed
await browser.elementById('link-to-subpage').click()
// Release the modifier key
await browser.keyup(modifierKey)
await browser.waitForIdleNetwork()
// Should trigger onClick but not onNavigate
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: true'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
})
it('should trigger both onClick and onNavigate for internal navigation', async () => {
const browser = await next.browser(path)
// Check initial state
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: false'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
// Click internal link
await browser.elementById('link-to-subpage').click()
await browser.waitForIdleNetwork()
// Should trigger both onClick and onNavigate
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: true'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: true'
)
})
it('should prevent navigation when onNavigate calls preventDefault', async () => {
const browser = await next.browser(path)
// Check initial state
expect(await browser.elementById('is-locked').text()).toBe(
'isLocked: false'
)
// Lock navigation
await browser.elementById('toggle-lock').click()
expect(await browser.elementById('is-locked').text()).toBe(
'isLocked: true'
)
// Try to navigate
await browser.elementById('link-to-subpage').click()
await browser.waitForIdleNetwork()
// Should trigger onClick but not navigate or trigger onNavigate
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: true'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
// Verify we're still on the same page
expect(await browser.url()).toContain(path)
})
it('should only trigger onClick for external links with target="_blank"', async () => {
const browser = await next.browser(path)
// Check initial state
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: false'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
// We can't fully test the new window, but we can verify the events are triggered
await browser.elementById('external-link-with-target').click()
await browser.waitForIdleNetwork()
// Should only trigger onClick for external links with target
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: true'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
})
it('should only trigger onClick for download links', async () => {
const browser = await next.browser(path)
// Check initial state
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: false'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
// Click download link
await browser.elementById('download-link').click()
await browser.waitForIdleNetwork()
// Should trigger both onClick and onNavigate for download links
expect(await browser.elementById('is-clicked').text()).toBe(
'isClicked: true'
)
expect(await browser.elementById('is-navigated').text()).toBe(
'isNavigated: false'
)
})
it('should only trigger both onClick for external links', async () => {
const alerts: string[] = []
const browser = await next.browser(path, {
beforePageLoad(page) {
page.on('dialog', (dialog) => {
alerts.push(dialog.message())
dialog.dismiss()
})
},
})
await browser.elementById('external-link').click()
await browser.waitForIdleNetwork()
expect(alerts).toEqual(['onClick'])
})
it('should replace history state for external links with replace prop', async () => {
const browser = await next.browser(path)
// Get initial history length
const initialLength = await browser.eval('history.length')
// Click external link with replace
await browser.elementById('external-link-with-replace').click()
await browser.waitForIdleNetwork()
// Verify history length hasn't changed (replace instead of push)
const finalLength = await browser.eval('history.length')
expect(finalLength).toBe(initialLength)
})
})
})
})