Files
next.js/test/e2e/ignore-invalid-popstateevent/without-i18n.test.ts
Janka UrygaandGitHub 4282bc6f91 remove BrowserInterface (#78308)
rip. you served a purpose once, but now you're just getting in the way. this improves typesafety quite a bit, because `BrowserInterface` had a whole bunch of random `any`s everywhere

also
- removes `evalAsync`. no idea why that was needed, but we're happily using promises in normal eval, so it can be dropped
- adds more safety to `chain`
2025-04-18 11:16:15 -07:00

76 lines
2.2 KiB
TypeScript

import { join } from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'e2e-utils'
import { check, waitFor } from 'next-test-utils'
import webdriver, { Playwright } from 'next-webdriver'
import type { HistoryState } from 'next/dist/shared/lib/router/router'
const emitPopsStateEvent = (browser: Playwright, state: HistoryState) =>
browser.eval(
`window.dispatchEvent(new PopStateEvent("popstate", { state: ${JSON.stringify(
state
)} }))`
)
describe('Event with stale state - static route previously was dynamic', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
// Don't use next.config.js to avoid getting i18n
},
dependencies: {},
})
})
afterAll(() => next.destroy())
test('Ignore event without query param', async () => {
const browser = await webdriver(next.url, '/static')
const state: HistoryState = {
url: '/[dynamic]?',
as: '/static',
options: {},
__N: true,
key: '',
}
expect(await browser.elementByCss('#page-type').text()).toBe('static')
// 1st event is ignored
await emitPopsStateEvent(browser, state)
await waitFor(1000)
expect(await browser.elementByCss('#page-type').text()).toBe('static')
// 2nd event isn't ignored
await emitPopsStateEvent(browser, state)
await check(() => browser.elementByCss('#page-type').text(), 'dynamic')
})
test('Ignore event with query param', async () => {
const browser = await webdriver(next.url, '/static?param=1')
const state: HistoryState = {
url: '/[dynamic]?param=1',
as: '/static?param=1',
options: {},
__N: true,
key: '',
}
expect(await browser.elementByCss('#page-type').text()).toBe('static')
// 1st event is ignored
await emitPopsStateEvent(browser, state)
await waitFor(1000)
expect(await browser.elementByCss('#page-type').text()).toBe('static')
// 2nd event isn't ignored
await emitPopsStateEvent(browser, state)
await check(() => browser.elementByCss('#page-type').text(), 'dynamic')
})
})