未验证 提交 08859275 编写于 作者: Z Zack Tanner 提交者: GitHub

Add tests for searchParams dev warning (#16798)

Fixes a warning being logged to the console when using `href` with a dynamic path. Fixes #16794
上级 764b1945
import { useRouter } from 'next/router'
const Page = () => {
const router = useRouter()
const { query } = router
return <p id="asdf">This is {query.id}</p>
}
export default Page
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
if (typeof window !== 'undefined') {
window.caughtWarns = []
const origWarn = window.console.warn
window.console.warn = function (...args) {
window.caughtWarns.push(args)
origWarn(...args)
}
}
const Page = () => { const Page = () => {
return ( return (
<div> <div>
...@@ -128,6 +137,9 @@ const Page = () => { ...@@ -128,6 +137,9 @@ const Page = () => {
<a id="nested-ssg-catch-all-multi">Nested Catch-all route (multi)</a> <a id="nested-ssg-catch-all-multi">Nested Catch-all route (multi)</a>
</Link> </Link>
<br /> <br />
<Link href="/d/dynamic-1">
<a id="dynamic-route-no-as">Dynamic route no as</a>
</Link>
<p id="query">{JSON.stringify(Object.keys(useRouter().query))}</p> <p id="query">{JSON.stringify(Object.keys(useRouter().query))}</p>
</div> </div>
) )
......
...@@ -84,6 +84,31 @@ function runTests(dev) { ...@@ -84,6 +84,31 @@ function runTests(dev) {
expect(url).toBe('?fromHome=true') expect(url).toBe('?fromHome=true')
}) })
if (dev) {
it('should not have any console warnings on initial load', async () => {
const browser = await webdriver(appPort, '/')
expect(await browser.eval('window.caughtWarns')).toEqual([])
})
it('should not have any console warnings when navigating to dynamic route', async () => {
let browser
try {
browser = await webdriver(appPort, '/')
await browser.eval('window.beforeNav = 1')
await browser.elementByCss('#dynamic-route-no-as').click()
await browser.waitForElementByCss('#asdf')
expect(await browser.eval('window.beforeNav')).toBe(1)
const text = await browser.elementByCss('#asdf').text()
expect(text).toMatch(/this is.*?dynamic-1/i)
expect(await browser.eval('window.caughtWarns')).toEqual([])
} finally {
if (browser) await browser.close()
}
})
}
it('should navigate to a dynamic page successfully', async () => { it('should navigate to a dynamic page successfully', async () => {
let browser let browser
try { try {
...@@ -893,6 +918,14 @@ function runTests(dev) { ...@@ -893,6 +918,14 @@ function runTests(dev) {
helloworld: 'hello-world', helloworld: 'hello-world',
}, },
}, },
{
namedRegex: '^/d/(?<id>[^/]+?)(?:/)?$',
page: '/d/[id]',
regex: normalizeRegEx('^\\/d\\/([^\\/]+?)(?:\\/)?$'),
routeKeys: {
id: 'id',
},
},
{ {
namedRegex: '^/dash/(?<helloworld>[^/]+?)(?:/)?$', namedRegex: '^/dash/(?<helloworld>[^/]+?)(?:/)?$',
page: '/dash/[hello-world]', page: '/dash/[hello-world]',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册