Skip to content

Commit

Permalink
Add tests for searchParams dev warning (#16798)
Browse files Browse the repository at this point in the history
Fixes a warning being logged to the console when using `href` with a dynamic path. Fixes #16794
  • Loading branch information
ztanner authored Sep 3, 2020
1 parent 764b194 commit 0885927
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/integration/dynamic-routing/pages/d/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
12 changes: 12 additions & 0 deletions test/integration/dynamic-routing/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import Link from 'next/link'
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 = () => {
return (
<div>
Expand Down Expand Up @@ -128,6 +137,9 @@ const Page = () => {
<a id="nested-ssg-catch-all-multi">Nested Catch-all route (multi)</a>
</Link>
<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>
</div>
)
Expand Down
33 changes: 33 additions & 0 deletions test/integration/dynamic-routing/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ function runTests(dev) {
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 () => {
let browser
try {
Expand Down Expand Up @@ -893,6 +918,14 @@ function runTests(dev) {
helloworld: 'hello-world',
},
},
{
namedRegex: '^/d/(?<id>[^/]+?)(?:/)?$',
page: '/d/[id]',
regex: normalizeRegEx('^\\/d\\/([^\\/]+?)(?:\\/)?$'),
routeKeys: {
id: 'id',
},
},
{
namedRegex: '^/dash/(?<helloworld>[^/]+?)(?:/)?$',
page: '/dash/[hello-world]',
Expand Down

0 comments on commit 0885927

Please sign in to comment.