Unverified Commit 837e0864 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

chore(CI): Break pages-dir client-navigation dev tests into smaller…

chore(CI): Break pages-dir client-navigation dev tests into smaller parallelizable files/suites (#78787)

Jest can run different suites in parallel, but tests within the same suite must run sequentially in the same worker.

You don't want suites that are too small, because each suite requires relatively expensive repository setup, but you also don't want suites that are too large because it inhibits parallelism.

This is a big problem for arewerspackyet, where many of these test cases are currently failing. Each test can take up to 4 minutes to time out, so we easily time out the entire 90 minute long test runner job.

## Test plan

Tested by running all these tests in parallel with:

```
NEXT_TEST_MODE=dev HEADLESS=true node_modules/.bin/jest test/development/pages-dir/client-navigation
```

It completes in about 2m40s minutes on my machine.
parent 2bb2b63b
/* eslint-env jest */
import { waitFor } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client Navigation', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
describe('with <a/> tag inside the <Link />', () => {
it('should navigate the page', async () => {
const browser = await next.browser('/nav/about')
const text = await browser
.elementByCss('#home-link')
.click()
.waitForElementByCss('.nav-home')
.elementByCss('p')
.text()
expect(text).toBe('This is the home.')
await browser.close()
})
it('should not navigate if the <a/> tag has a target', async () => {
const browser = await next.browser('/nav')
await browser
.elementByCss('#increase')
.click()
.elementByCss('#target-link')
.click()
await waitFor(1000)
const counterText = await browser.elementByCss('#counter').text()
expect(counterText).toBe('Counter: 1')
await browser.close()
})
it('should not navigate if the click-event is modified', async () => {
const browser = await next.browser('/nav')
await browser.elementByCss('#increase').click()
const key = process.platform === 'darwin' ? 'Meta' : 'Control'
await browser.keydown(key)
await browser.elementByCss('#in-svg-link').click()
await browser.keyup(key)
await waitFor(1000)
const counterText = await browser.elementByCss('#counter').text()
expect(counterText).toBe('Counter: 1')
await browser.close()
})
it('should not reload when link in svg is clicked', async () => {
const browser = await next.browser('/nav')
await browser.eval('window.hello = true')
await browser
.elementByCss('#in-svg-link')
.click()
.waitForElementByCss('.nav-about')
expect(await browser.eval('window.hello')).toBe(true)
await browser.close()
})
})
describe('with unexpected <a/> nested tag', () => {
it('should not redirect if passHref prop is not defined in Link', async () => {
const browser = await next.browser('/nav/pass-href-prop')
const text = await browser
.elementByCss('#without-href')
.click()
.waitForElementByCss('.nav-pass-href-prop')
.elementByCss('p')
.text()
expect(text).toBe('This is the passHref prop page.')
await browser.close()
})
it('should redirect if passHref prop is defined in Link', async () => {
const browser = await next.browser('/nav/pass-href-prop')
const text = await browser
.elementByCss('#with-href')
.click()
.waitForElementByCss('.nav-home')
.elementByCss('p')
.text()
expect(text).toBe('This is the home.')
await browser.close()
})
})
})
/* eslint-env jest */
import { check } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation with asPath', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
describe('inside getInitialProps', () => {
it('should show the correct asPath with a Link with as prop', async () => {
const browser = await next.browser('/nav')
const asPath = await browser
.elementByCss('#as-path-link')
.click()
.waitForElementByCss('.as-path-content')
.elementByCss('.as-path-content')
.text()
expect(asPath).toBe('/as/path')
await browser.close()
})
it('should show the correct asPath with a Link without the as prop', async () => {
const browser = await next.browser('/nav')
const asPath = await browser
.elementByCss('#as-path-link-no-as')
.click()
.waitForElementByCss('.as-path-content')
.elementByCss('.as-path-content')
.text()
expect(asPath).toBe('/nav/as-path')
await browser.close()
})
})
describe('with next/router', () => {
it('should show the correct asPath', async () => {
const browser = await next.browser('/nav')
const asPath = await browser
.elementByCss('#as-path-using-router-link')
.click()
.waitForElementByCss('.as-path-content')
.elementByCss('.as-path-content')
.text()
expect(asPath).toBe('/nav/as-path-using-router')
await browser.close()
})
it('should navigate an absolute url on push', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
await browser.waitForElementByCss('#router-push').click()
await check(
() => browser.eval(() => window.location.origin),
'https://vercel.com'
)
})
it('should navigate an absolute url on replace', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
await browser.waitForElementByCss('#router-replace').click()
await check(
() => browser.eval(() => window.location.origin),
'https://vercel.com'
)
})
it('should navigate an absolute local url on push', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
// @ts-expect-error _didNotNavigate is set intentionally
await browser.eval(() => (window._didNotNavigate = true))
await browser.waitForElementByCss('#router-local-push').click()
const text = await browser
.waitForElementByCss('.nav-about')
.elementByCss('p')
.text()
expect(text).toBe('This is the about page.')
// @ts-expect-error _didNotNavigate is set intentionally
expect(await browser.eval(() => window._didNotNavigate)).toBe(true)
})
it('should navigate an absolute local url on replace', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
// @ts-expect-error _didNotNavigate is set intentionally
await browser.eval(() => (window._didNotNavigate = true))
await browser.waitForElementByCss('#router-local-replace').click()
const text = await browser
.waitForElementByCss('.nav-about')
.elementByCss('p')
.text()
expect(text).toBe('This is the about page.')
// @ts-expect-error _didNotNavigate is set intentionally
expect(await browser.eval(() => window._didNotNavigate)).toBe(true)
})
})
describe('with next/link', () => {
it('should use pushState with same href and different asPath', async () => {
const browser = await next.browser('/nav/as-path-pushstate')
await browser
.elementByCss('#hello')
.click()
.waitForElementByCss('#something-hello')
const queryOne = JSON.parse(
await browser.elementByCss('#router-query').text()
)
expect(queryOne.something).toBe('hello')
await browser
.elementByCss('#same-query')
.click()
.waitForElementByCss('#something-same-query')
const queryTwo = JSON.parse(
await browser.elementByCss('#router-query').text()
)
expect(queryTwo.something).toBe('hello')
await browser.back().waitForElementByCss('#something-hello')
const queryThree = JSON.parse(
await browser.elementByCss('#router-query').text()
)
expect(queryThree.something).toBe('hello')
await browser
.elementByCss('#else')
.click()
.waitForElementByCss('#something-else')
await browser
.elementByCss('#hello2')
.click()
.waitForElementByCss('#nav-as-path-pushstate')
await browser.back().waitForElementByCss('#something-else')
const queryFour = JSON.parse(
await browser.elementByCss('#router-query').text()
)
expect(queryFour.something).toBe(undefined)
})
it('should detect asPath query changes correctly', async () => {
const browser = await next.browser('/nav/as-path-query')
await browser
.elementByCss('#hello')
.click()
.waitForElementByCss('#something-hello-something-hello')
const queryOne = JSON.parse(
await browser.elementByCss('#router-query').text()
)
expect(queryOne.something).toBe('hello')
await browser
.elementByCss('#hello2')
.click()
.waitForElementByCss('#something-hello-something-else')
const queryTwo = JSON.parse(
await browser.elementByCss('#router-query').text()
)
expect(queryTwo.something).toBe('else')
})
})
})
/* eslint-env jest */
import { fetchViaHTTP, waitFor } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation on error pages', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
it('should not reload when visiting /_error directly', async () => {
const { status } = await fetchViaHTTP(next.appPort, '/_error')
const browser = await next.browser('/_error')
await browser.eval('window.hello = true')
// wait on-demand-entries timeout since it can trigger
// reloading non-stop
for (let i = 0; i < 15; i++) {
expect(await browser.eval('window.hello')).toBe(true)
await waitFor(1000)
}
const html = await browser.eval('document.documentElement.innerHTML')
expect(status).toBe(404)
expect(html).toContain('This page could not be found')
expect(html).toContain('404')
})
describe('with 404 pages', () => {
it('should 404 on not existent page', async () => {
const browser = await next.browser('/non-existent')
expect(await browser.elementByCss('h1').text()).toBe('404')
expect(await browser.elementByCss('h2').text()).toBe(
'This page could not be found.'
)
await browser.close()
})
it('should 404 on wrong casing', async () => {
const browser = await next.browser('/nAv/AbOuT')
expect(await browser.elementByCss('h1').text()).toBe('404')
expect(await browser.elementByCss('h2').text()).toBe(
'This page could not be found.'
)
await browser.close()
})
it('should get url dynamic param', async () => {
const browser = await next.browser('/dynamic/dynamic-part/route')
expect(await browser.elementByCss('p').text()).toBe('dynamic-part')
await browser.close()
})
it('should 404 on wrong casing of url dynamic param', async () => {
const browser = await next.browser('/dynamic/dynamic-part/RoUtE')
expect(await browser.elementByCss('h1').text()).toBe('404')
expect(await browser.elementByCss('h2').text()).toBe(
'This page could not be found.'
)
await browser.close()
})
it('should not 404 for <page>/', async () => {
const browser = await next.browser('/nav/about/')
const text = await browser.elementByCss('p').text()
expect(text).toBe('This is the about page.')
await browser.close()
})
it('should should not contain a page script in a 404 page', async () => {
const browser = await next.browser('/non-existent')
const scripts = await browser.elementsByCss('script[src]')
for (const script of scripts) {
const src = await script.getAttribute('src')
expect(src.includes('/non-existent')).toBeFalsy()
}
await browser.close()
})
})
})
/* eslint-env jest */
import { assertNoRedbox, waitFor } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation with foreign history manipulation', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
it('should ignore history state without options', async () => {
const browser = await next.browser('/nav')
// push history object without options
await browser.eval(
'window.history.pushState({ url: "/whatever" }, "", "/whatever")'
)
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await waitFor(1000)
await assertNoRedbox(browser)
})
it('should ignore history state with an invalid url', async () => {
const browser = await next.browser('/nav')
// push history object wit invalid url (not relative)
await browser.eval(
'window.history.pushState({ url: "http://google.com" }, "", "/whatever")'
)
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await waitFor(1000)
await assertNoRedbox(browser)
})
it('should ignore foreign history state with missing properties', async () => {
const browser = await next.browser('/nav')
// push empty history state
await browser.eval('window.history.pushState({}, "", "/whatever")')
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await waitFor(1000)
await assertNoRedbox(browser)
})
})
/* eslint-env jest */
import { waitFor } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
const isReact18 = parseInt(process.env.NEXT_TEST_REACT_VERSION) === 18
describe.each([[false], [true]])(
'updating <Head /> with strictNextHead=%s while client routing',
(strictNextHead) => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(strictNextHead),
},
})
it.each([true, false])(
'should handle boolean async prop in next/head client-side: %s',
async (bool) => {
const browser = await next.browser('/head')
const value = await browser.eval(
`document.querySelector('script[src="/test-async-${JSON.stringify(
bool
)}.js"]').async`
)
expect(value).toBe(bool)
}
)
it('should only execute async and defer scripts once', async () => {
const browser = await next.browser('/head')
await browser.waitForElementByCss('h1')
await waitFor(2000)
expect(Number(await browser.eval('window.__test_async_executions'))).toBe(
strictNextHead || isReact18
? 1
: // <meta name="next-head-count" /> is floated before <script />.
// head-manager thinks it needs to add these again resulting in another execution.
2
)
expect(Number(await browser.eval('window.__test_defer_executions'))).toBe(
strictNextHead || isReact18
? 1
: // <meta name="next-head-count" /> is floated before <script defer />.
// head-manager thinks it needs to add these again resulting in another execution.
2
)
await browser.elementByCss('#reverseScriptOrder').click()
await waitFor(2000)
expect(Number(await browser.eval('window.__test_async_executions'))).toBe(
strictNextHead || isReact18 ? 1 : 2
)
expect(Number(await browser.eval('window.__test_defer_executions'))).toBe(
strictNextHead || isReact18 ? 1 : 2
)
await browser.elementByCss('#toggleScript').click()
await waitFor(2000)
expect(Number(await browser.eval('window.__test_async_executions'))).toBe(
strictNextHead || isReact18 ? 1 : 2
)
expect(Number(await browser.eval('window.__test_defer_executions'))).toBe(
strictNextHead || isReact18 ? 1 : 2
)
})
it('should warn when stylesheets or scripts are in head', async () => {
const browser = await next.browser('/head')
await browser.waitForElementByCss('h1')
await waitFor(1000)
const browserLogs = await browser.log()
let foundStyles = false
let foundScripts = false
const logs = []
browserLogs.forEach(({ message }) => {
if (message.includes('Do not add stylesheets using next/head')) {
foundStyles = true
logs.push(message)
}
if (message.includes('Do not add <script> tags using next/head')) {
foundScripts = true
logs.push(message)
}
})
expect(foundStyles).toEqual(true)
expect(foundScripts).toEqual(true)
// Warnings are unique
expect(logs.length).toEqual(new Set(logs).size)
})
it('should warn when scripts are in head', async () => {
const browser = await next.browser('/head')
await browser.waitForElementByCss('h1')
await waitFor(1000)
const browserLogs = await browser.log()
let found = false
browserLogs.forEach((log) => {
if (log.message.includes('Use next/script instead')) {
found = true
}
})
expect(found).toEqual(true)
})
it('should not warn when application/ld+json scripts are in head', async () => {
const browser = await next.browser('/head-with-json-ld-snippet')
await browser.waitForElementByCss('h1')
await waitFor(1000)
const browserLogs = await browser.log()
let found = false
browserLogs.forEach((log) => {
if (log.message.includes('Use next/script instead')) {
found = true
}
})
expect(found).toEqual(false)
})
it('should update head during client routing', async () => {
const browser = await next.browser('/nav/head-1')
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head One')
await browser
.elementByCss('#to-head-2')
.click()
.waitForElementByCss('#head-2', 3000)
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head Two')
await browser
.elementByCss('#to-head-1')
.click()
.waitForElementByCss('#head-1', 3000)
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head One')
await browser
.elementByCss('#to-head-3')
.click()
.waitForElementByCss('#head-3', 3000)
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head Three')
expect(await browser.eval('document.title')).toBe('')
await browser
.elementByCss('#to-head-1')
.click()
.waitForElementByCss('#head-1', 3000)
expect(
await browser
.elementByCss('meta[name="description"]')
.getAttribute('content')
).toBe('Head One')
})
it('should update title during client routing', async () => {
const browser = await next.browser('/nav/head-1')
expect(await browser.eval('document.title')).toBe('this is head-1')
await browser
.elementByCss('#to-head-2')
.click()
.waitForElementByCss('#head-2', 3000)
expect(await browser.eval('document.title')).toBe('this is head-2')
await browser
.elementByCss('#to-head-1')
.click()
.waitForElementByCss('#head-1', 3000)
expect(await browser.eval('document.title')).toBe('this is head-1')
})
it('should update head when unmounting component', async () => {
const browser = await next.browser('/head-dynamic')
expect(await browser.eval('document.title')).toBe('B')
await browser.elementByCss('button').click()
expect(await browser.eval('document.title')).toBe('A')
await browser.elementByCss('button').click()
expect(await browser.eval('document.title')).toBe('B')
})
}
)
/* eslint-env jest */
import { assertNoRedbox, waitFor, check } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client Navigation with <Link/>', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
it('should navigate the page', async () => {
const browser = await next.browser('/nav')
const text = await browser
.elementByCss('#about-link')
.click()
.waitForElementByCss('.nav-about')
.elementByCss('p')
.text()
expect(text).toBe('This is the about page.')
await browser.close()
})
it('should not throw error when one number type child is provided', async () => {
const browser = await next.browser('/link-number-child')
await assertNoRedbox(browser)
if (browser) await browser.close()
})
it('should navigate back after reload', async () => {
const browser = await next.browser('/nav')
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.refresh()
await waitFor(3000)
await browser.back()
await waitFor(3000)
const text = await browser.elementByCss('#about-link').text()
if (browser) await browser.close()
expect(text).toMatch(/About/)
})
it('should navigate forwards after reload', async () => {
const browser = await next.browser('/nav')
await browser.elementByCss('#about-link').click()
await browser.waitForElementByCss('.nav-about')
await browser.back()
await browser.refresh()
await waitFor(3000)
await browser.forward()
await waitFor(3000)
const text = await browser.elementByCss('p').text()
if (browser) await browser.close()
expect(text).toMatch(/this is the about page/i)
})
it('should error when calling onClick without event', async () => {
const browser = await next.browser('/link-invalid-onclick')
expect(await browser.elementByCss('#errors').text()).toBe('0')
await browser.elementByCss('#custom-button').click()
expect(await browser.elementByCss('#errors').text()).toBe('1')
})
it('should navigate via the client side', async () => {
const browser = await next.browser('/nav')
const counterText = await browser
.elementByCss('#increase')
.click()
.elementByCss('#about-link')
.click()
.waitForElementByCss('.nav-about')
.elementByCss('#home-link')
.click()
.waitForElementByCss('.nav-home')
.elementByCss('#counter')
.text()
expect(counterText).toBe('Counter: 1')
await browser.close()
})
it('should navigate an absolute url', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
await browser.waitForElementByCss('#absolute-link').click()
await check(
() => browser.eval(() => window.location.origin),
'https://vercel.com'
)
})
it('should call mouse handlers with an absolute url', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
await browser.elementByCss('#absolute-link-mouse-events').moveTo()
expect(
await browser
.waitForElementByCss('#absolute-link-mouse-events')
.getAttribute('data-hover')
).toBe('true')
})
it('should navigate an absolute local url', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
// @ts-expect-error _didNotNavigate is set intentionally
await browser.eval(() => (window._didNotNavigate = true))
await browser.waitForElementByCss('#absolute-local-link').click()
const text = await browser
.waitForElementByCss('.nav-about')
.elementByCss('p')
.text()
expect(text).toBe('This is the about page.')
// @ts-expect-error _didNotNavigate is set intentionally
expect(await browser.eval(() => window._didNotNavigate)).toBe(true)
})
it('should navigate an absolute local url with as', async () => {
const browser = await next.browser(`/absolute-url?port=${next.appPort}`)
// @ts-expect-error _didNotNavigate is set intentionally
await browser.eval(() => (window._didNotNavigate = true))
await browser.waitForElementByCss('#absolute-local-dynamic-link').click()
expect(await browser.waitForElementByCss('#dynamic-page').text()).toBe(
'hello'
)
// @ts-expect-error _didNotNavigate is set intentionally
expect(await browser.eval(() => window._didNotNavigate)).toBe(true)
})
})
/* eslint-env jest */
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('Client navigation with onClick action', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
it('should reload the page and perform additional action', async () => {
const browser = await next.browser('/nav/on-click')
const defaultCountQuery = await browser.elementByCss('#query-count').text()
const defaultCountState = await browser.elementByCss('#state-count').text()
expect(defaultCountQuery).toBe('QUERY COUNT: 0')
expect(defaultCountState).toBe('STATE COUNT: 0')
await browser.elementByCss('#on-click-link').click()
await retry(async () => {
const countQueryAfterClicked = await browser
.elementByCss('#query-count')
.text()
const countStateAfterClicked = await browser
.elementByCss('#state-count')
.text()
expect(countQueryAfterClicked).toBe('QUERY COUNT: 1')
expect(countStateAfterClicked).toBe('STATE COUNT: 1')
})
})
it('should not reload if default was prevented', async () => {
const browser = await next.browser('/nav/on-click')
const defaultCountQuery = await browser.elementByCss('#query-count').text()
const defaultCountState = await browser.elementByCss('#state-count').text()
expect(defaultCountQuery).toBe('QUERY COUNT: 0')
expect(defaultCountState).toBe('STATE COUNT: 0')
await browser.elementByCss('#on-click-link-prevent-default').click()
await retry(async () => {
const countQueryAfterClicked = await browser
.elementByCss('#query-count')
.text()
const countStateAfterClicked = await browser
.elementByCss('#state-count')
.text()
expect(countQueryAfterClicked).toBe('QUERY COUNT: 0')
expect(countStateAfterClicked).toBe('STATE COUNT: 1')
})
await browser.elementByCss('#on-click-link').click()
await retry(async () => {
const countQueryAfterClickedAgain = await browser
.elementByCss('#query-count')
.text()
const countStateAfterClickedAgain = await browser
.elementByCss('#state-count')
.text()
expect(countQueryAfterClickedAgain).toBe('QUERY COUNT: 1')
expect(countStateAfterClickedAgain).toBe('STATE COUNT: 2')
})
})
it('should always replace the state and perform additional action', async () => {
const browser = await next.browser('/nav')
await browser.elementByCss('#on-click-link').click()
await retry(async () => {
const defaultCountQuery = await browser
.elementByCss('#query-count')
.text()
expect(defaultCountQuery).toBe('QUERY COUNT: 1')
})
await browser.elementByCss('#on-click-link').click()
await retry(async () => {
const countQueryAfterClicked = await browser
.elementByCss('#query-count')
.text()
const countStateAfterClicked = await browser
.elementByCss('#state-count')
.text()
expect(countQueryAfterClicked).toBe('QUERY COUNT: 2')
expect(countStateAfterClicked).toBe('STATE COUNT: 1')
})
// Since we replace the state, back button would simply go us back to /nav
await browser.back().waitForElementByCss('.nav-home')
})
})
/* eslint-env jest */
import { check } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation querystring', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
describe('with the same page but different querystring', () => {
it('should navigate the page', async () => {
const browser = await next.browser('/nav/querystring?id=1')
const text = await browser
.elementByCss('#next-id-link')
.click()
.waitForElementByCss('.nav-id-2')
.elementByCss('p')
.text()
expect(text).toBe('2')
await browser.close()
})
it('should remove querystring', async () => {
const browser = await next.browser('/nav/querystring?id=1')
const text = await browser
.elementByCss('#main-page')
.click()
.waitForElementByCss('.nav-id-0')
.elementByCss('p')
.text()
expect(text).toBe('0')
await browser.close()
})
})
describe('with querystring relative urls', () => {
it('should work with Link', async () => {
const browser = await next.browser('/nav/query-only')
await browser.elementByCss('#link').click()
await check(() => browser.waitForElementByCss('#prop').text(), 'foo')
})
it('should work with router.push', async () => {
const browser = await next.browser('/nav/query-only')
await browser.elementByCss('#router-push').click()
await check(() => browser.waitForElementByCss('#prop').text(), 'bar')
})
it('should work with router.replace', async () => {
const browser = await next.browser('/nav/query-only')
await browser.elementByCss('#router-replace').click()
await check(() => browser.waitForElementByCss('#prop').text(), 'baz')
})
it('router.replace with shallow=true shall not throw route cancelled errors', async () => {
const browser = await next.browser('/nav/query-only-shallow')
await browser.elementByCss('#router-replace').click()
// the error occurs on every replace() after the first one
await browser.elementByCss('#router-replace').click()
await check(
() => browser.waitForElementByCss('#routeState').text(),
'{"completed":2,"errors":0}'
)
})
})
})
/* eslint-env jest */
import { check } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation scroll', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
describe('resets scroll at the correct time', () => {
it('should reset scroll before the new page runs its lifecycles (<Link />)', async () => {
const browser = await next.browser('/nav/long-page-to-snap-scroll')
// Scrolls to item 400 on the page
await browser
.waitForElementByCss('#long-page-to-snap-scroll')
.elementByCss('#scroll-to-item-400')
.click()
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(7208)
// Go to snap scroll page
await browser
.elementByCss('#goto-snap-scroll-position')
.click()
.waitForElementByCss('#scroll-pos-y')
const snappedScrollPosition = await browser.eval(
'document.getElementById("scroll-pos-y").innerText'
)
expect(snappedScrollPosition).toBe('0')
})
it('should reset scroll before the new page runs its lifecycles (Router#push)', async () => {
const browser = await next.browser('/nav/long-page-to-snap-scroll')
// Scrolls to item 400 on the page
await browser
.waitForElementByCss('#long-page-to-snap-scroll')
.elementByCss('#scroll-to-item-400')
.click()
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(7208)
// Go to snap scroll page
await browser
.elementByCss('#goto-snap-scroll-position-imperative')
.click()
.waitForElementByCss('#scroll-pos-y')
const snappedScrollPosition = await browser.eval(
'document.getElementById("scroll-pos-y").innerText'
)
expect(snappedScrollPosition).toBe('0')
})
it('should intentionally not reset scroll before the new page runs its lifecycles (Router#push)', async () => {
const browser = await next.browser('/nav/long-page-to-snap-scroll')
// Scrolls to item 400 on the page
await browser
.waitForElementByCss('#long-page-to-snap-scroll')
.elementByCss('#scroll-to-item-400')
.click()
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(7208)
// Go to snap scroll page
await browser
.elementByCss('#goto-snap-scroll-position-imperative-noscroll')
.click()
.waitForElementByCss('#scroll-pos-y')
const snappedScrollPosition = await browser.eval(
'document.getElementById("scroll-pos-y").innerText'
)
expect(snappedScrollPosition).not.toBe('0')
expect(Number(snappedScrollPosition)).toBeGreaterThanOrEqual(7208)
})
})
it('should scroll to top when the scroll option is set to true', async () => {
const browser = await next.browser('/nav/shallow-routing')
await browser.eval(() =>
document.querySelector('#increaseWithScroll').scrollIntoView()
)
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBeGreaterThan(3000)
await browser.elementByCss('#increaseWithScroll').click()
await check(async () => {
const newScrollPosition = await browser.eval('window.pageYOffset')
return newScrollPosition === 0 ? 'success' : 'fail'
}, 'success')
})
})
/* eslint-env jest */
import { waitFor } from 'next-test-utils'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation with shallow routing', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
it('should update the url without running getInitialProps', async () => {
const browser = await next.browser('/nav/shallow-routing')
const counter = await browser
.elementByCss('#increase')
.click()
.elementByCss('#increase')
.click()
.elementByCss('#counter')
.text()
expect(counter).toBe('Counter: 2')
const getInitialPropsRunCount = await browser
.elementByCss('#get-initial-props-run-count')
.text()
expect(getInitialPropsRunCount).toBe('getInitialProps run count: 1')
await browser.close()
})
it('should handle the back button and should not run getInitialProps', async () => {
const browser = await next.browser('/nav/shallow-routing')
let counter = await browser
.elementByCss('#increase')
.click()
.elementByCss('#increase')
.click()
.elementByCss('#counter')
.text()
expect(counter).toBe('Counter: 2')
counter = await browser.back().elementByCss('#counter').text()
expect(counter).toBe('Counter: 1')
const getInitialPropsRunCount = await browser
.elementByCss('#get-initial-props-run-count')
.text()
expect(getInitialPropsRunCount).toBe('getInitialProps run count: 1')
await browser.close()
})
it('should run getInitialProps always when rending the page to the screen', async () => {
const browser = await next.browser('/nav/shallow-routing')
const counter = await browser
.elementByCss('#increase')
.click()
.elementByCss('#increase')
.click()
.elementByCss('#home-link')
.click()
.waitForElementByCss('.nav-home')
.back()
.waitForElementByCss('.shallow-routing')
.elementByCss('#counter')
.text()
expect(counter).toBe('Counter: 2')
const getInitialPropsRunCount = await browser
.elementByCss('#get-initial-props-run-count')
.text()
expect(getInitialPropsRunCount).toBe('getInitialProps run count: 2')
await browser.close()
})
it('should keep the scroll position on shallow routing', async () => {
const browser = await next.browser('/nav/shallow-routing')
await browser.eval(() =>
document.querySelector('#increase').scrollIntoView()
)
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBeGreaterThan(3000)
await browser.elementByCss('#increase').click()
await waitFor(500)
const newScrollPosition = await browser.eval('window.pageYOffset')
expect(newScrollPosition).toBe(scrollPosition)
await browser.elementByCss('#increase2').click()
await waitFor(500)
const newScrollPosition2 = await browser.eval('window.pageYOffset')
expect(newScrollPosition2).toBe(0)
await browser.eval(() =>
document.querySelector('#invalidShallow').scrollIntoView()
)
const scrollPositionDown = await browser.eval('window.pageYOffset')
expect(scrollPositionDown).toBeGreaterThan(3000)
await browser.elementByCss('#invalidShallow').click()
await waitFor(500)
const newScrollPosition3 = await browser.eval('window.pageYOffset')
expect(newScrollPosition3).toBe(0)
})
})
/* eslint-env jest */
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
describe('Client navigation with URL hash', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixture'),
env: {
TEST_STRICT_NEXT_HEAD: String(true),
},
})
describe('when hash changes', () => {
describe('check hydration mis-match', () => {
it('should not have hydration mis-match for hash link', async () => {
const browser = await next.browser('/nav/hash-changes')
const browserLogs = await browser.log()
let found = false
browserLogs.forEach((log) => {
console.log('log.message', log.message)
if (log.message.includes('Warning: Prop')) {
found = true
}
})
expect(found).toEqual(false)
})
})
describe('when hash change via Link', () => {
it('should not run getInitialProps', async () => {
const browser = await next.browser('/nav/hash-changes')
const counter = await browser
.elementByCss('#via-link')
.click()
.elementByCss('p')
.text()
expect(counter).toBe('COUNT: 0')
await browser.close()
})
it('should scroll to the specified position on the same page', async () => {
const browser = await next.browser('/nav/hash-changes')
// Scrolls to item 400 on the page
await browser.elementByCss('#scroll-to-item-400').click()
const scrollPositionBeforeEmptyHash =
await browser.eval('window.pageYOffset')
expect(scrollPositionBeforeEmptyHash).toBe(7258)
// Scrolls back to top when scrolling to `#` with no value.
await browser.elementByCss('#via-empty-hash').click()
const scrollPositionAfterEmptyHash =
await browser.eval('window.pageYOffset')
expect(scrollPositionAfterEmptyHash).toBe(0)
// Scrolls to item 400 on the page
await browser.elementByCss('#scroll-to-item-400').click()
const scrollPositionBeforeTopHash =
await browser.eval('window.pageYOffset')
expect(scrollPositionBeforeTopHash).toBe(7258)
// Scrolls back to top when clicking link with href `#top`.
await browser.elementByCss('#via-top-hash').click()
const scrollPositionAfterTopHash =
await browser.eval('window.pageYOffset')
expect(scrollPositionAfterTopHash).toBe(0)
// Scrolls to cjk anchor on the page
await browser.elementByCss('#scroll-to-cjk-anchor').click()
const scrollPositionCJKHash = await browser.eval('window.pageYOffset')
expect(scrollPositionCJKHash).toBe(17436)
})
it('should not scroll to hash when scroll={false} is set', async () => {
const browser = await next.browser('/nav/hash-changes')
const curScroll = await browser.eval(
'document.documentElement.scrollTop'
)
await browser.elementByCss('#scroll-to-name-item-400-no-scroll').click()
expect(curScroll).toBe(
await browser.eval('document.documentElement.scrollTop')
)
})
it('should scroll to the specified position on the same page with a name property', async () => {
const browser = await next.browser('/nav/hash-changes')
// Scrolls to item 400 with name="name-item-400" on the page
await browser.elementByCss('#scroll-to-name-item-400').click()
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(16258)
// Scrolls back to top when scrolling to `#` with no value.
await browser.elementByCss('#via-empty-hash').click()
const scrollPositionAfterEmptyHash =
await browser.eval('window.pageYOffset')
expect(scrollPositionAfterEmptyHash).toBe(0)
})
it('should scroll to the specified position to a new page', async () => {
const browser = await next.browser('/nav')
// Scrolls to item 400 on the page
await browser
.elementByCss('#scroll-to-hash')
.click()
.waitForElementByCss('#hash-changes-page')
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(7258)
})
it('should scroll to the specified CJK position to a new page', async () => {
const browser = await next.browser('/nav')
// Scrolls to CJK anchor on the page
await browser
.elementByCss('#scroll-to-cjk-hash')
.click()
.waitForElementByCss('#hash-changes-page')
const scrollPosition = await browser.eval('window.pageYOffset')
expect(scrollPosition).toBe(17436)
})
it('Should update asPath', async () => {
const browser = await next.browser('/nav/hash-changes')
await browser.elementByCss('#via-link').click()
const asPath = await browser.elementByCss('div#asPath').text()
expect(asPath).toBe('ASPATH: /nav/hash-changes#via-link')
})
})
describe('when hash change via A tag', () => {
it('should not run getInitialProps', async () => {
const browser = await next.browser('/nav/hash-changes')
const counter = await browser
.elementByCss('#via-a')
.click()
.elementByCss('p')
.text()
expect(counter).toBe('COUNT: 0')
await browser.close()
})
})
describe('when hash get removed', () => {
it('should not run getInitialProps', async () => {
const browser = await next.browser('/nav/hash-changes')
const counter = await browser
.elementByCss('#via-a')
.click()
.elementByCss('#page-url')
.click()
.elementByCss('p')
.text()
expect(counter).toBe('COUNT: 1')
await browser.close()
})
it('should not run getInitialProps when removing via back', async () => {
const browser = await next.browser('/nav/hash-changes')
const counter = await browser
.elementByCss('#scroll-to-item-400')
.click()
.back()
.elementByCss('p')
.text()
expect(counter).toBe('COUNT: 0')
await browser.close()
})
})
describe('when hash set to empty', () => {
it('should not run getInitialProps', async () => {
const browser = await next.browser('/nav/hash-changes')
const counter = await browser
.elementByCss('#via-a')
.click()
.elementByCss('#via-empty-hash')
.click()
.elementByCss('p')
.text()
expect(counter).toBe('COUNT: 0')
await browser.close()
})
})
})
describe('when hash changes with state', () => {
describe('when passing state via hash change', () => {
it('should increment the history state counter', async () => {
const browser = await next.browser('/nav/hash-changes-with-state#')
const historyCount = await browser
.elementByCss('#increment-history-count')
.click()
.elementByCss('#increment-history-count')
.click()
.elementByCss('div#history-count')
.text()
expect(historyCount).toBe('HISTORY COUNT: 2')
const counter = await browser.elementByCss('p').text()
// getInitialProps should not be called with only hash changes
expect(counter).toBe('COUNT: 0')
await browser.close()
})
it('should increment the shallow history state counter', async () => {
const browser = await next.browser('/nav/hash-changes-with-state#')
const historyCount = await browser
.elementByCss('#increment-shallow-history-count')
.click()
.elementByCss('#increment-shallow-history-count')
.click()
.elementByCss('div#shallow-history-count')
.text()
expect(historyCount).toBe('SHALLOW HISTORY COUNT: 2')
const counter = await browser.elementByCss('p').text()
expect(counter).toBe('COUNT: 0')
await browser.close()
})
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment