Skip to content

Commit

Permalink
test: added tests for supporting app client hooks in pages
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Oct 27, 2022
1 parent 88806a0 commit 4674bf5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/e2e/app-dir/app/components/router-hooks-fixtures.js
@@ -0,0 +1,26 @@
import { useRouter } from 'next/router'
import { usePathname, useSearchParams } from 'next/navigation'
import { useState } from 'react'
import { useEffect } from 'react'

export const RouterHooksFixtures = () => {
const router = useRouter()
const searchParams = useSearchParams()
const pathname = usePathname()

const [value, setValue] = useState(null)
useEffect(() => {
if (!router.isReady) {
return
}

setValue(searchParams.get('key'))
}, [router.isReady, searchParams])

return (
<div id={router.isReady ? 'router-ready' : 'router-not-ready'}>
<div id="key-value">{value}</div>
<div id="pathname">{pathname}</div>
</div>
)
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app/pages/adapter-hooks/[id].js
@@ -0,0 +1,5 @@
import { RouterHooksFixtures } from '../../components/router-hooks-fixtures'

export default function Page() {
return <RouterHooksFixtures />
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/app/pages/adapter-hooks/[id]/account.js
@@ -0,0 +1,13 @@
import { RouterHooksFixtures } from '../../../components/router-hooks-fixtures'

export default function Page() {
return <RouterHooksFixtures />
}

export const getStaticProps = () => {
return { props: {} }
}

export const getStaticPaths = () => {
return { fallback: 'blocking', paths: [{ params: { id: '1' } }] }
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/app/pages/adapter-hooks/static.js
@@ -0,0 +1,9 @@
import { RouterHooksFixtures } from '../../components/router-hooks-fixtures'

export default function Page() {
return <RouterHooksFixtures />
}

export const getStaticProps = () => {
return { props: {} }
}
33 changes: 33 additions & 0 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -1125,6 +1125,39 @@ describe('app dir', () => {

describe('client components', () => {
describe('hooks', () => {
describe('from pages', () => {
it.each([
{ pathname: '/adapter-hooks/static' },
{ pathname: '/adapter-hooks/1' },
{ pathname: '/adapter-hooks/2' },
{ pathname: '/adapter-hooks/1/account' },
{ pathname: '/adapter-hooks/static', keyValue: 'value' },
{ pathname: '/adapter-hooks/1', keyValue: 'value' },
{ pathname: '/adapter-hooks/2', keyValue: 'value' },
{ pathname: '/adapter-hooks/1/account', keyValue: 'value' },
])(
'should have the correct hooks',
async ({ pathname, keyValue = '' }) => {
const browser = await webdriver(
next.url,
pathname + (keyValue ? `?key=${keyValue}` : '')
)

try {
await browser.waitForElementByCss('#router-ready')
expect(await browser.elementById('key-value').text()).toBe(
keyValue
)
expect(await browser.elementById('pathname').text()).toBe(
pathname
)
} finally {
await browser.close()
}
}
)
})

describe('usePathname', () => {
it('should have the correct pathname', async () => {
const html = await renderViaHTTP(next.url, '/hooks/use-pathname')
Expand Down

0 comments on commit 4674bf5

Please sign in to comment.