Skip to content

Commit

Permalink
Add tests for static
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes Bornö committed Oct 25, 2022
1 parent 28d6ee2 commit a577ff2
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
75 changes: 75 additions & 0 deletions test/e2e/app-dir/app-static.test.ts
Expand Up @@ -10,6 +10,8 @@ import webdriver from 'next-webdriver'
const glob = promisify(globOrig)

describe('app-dir static/dynamic handling', () => {
const isDev = (global as any).isNextDev

if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
Expand Down Expand Up @@ -56,6 +58,8 @@ describe('app-dir static/dynamic handling', () => {
'blog/tim/first-post.rsc',
'dynamic-no-gen-params-ssr/[slug]/page.js',
'dynamic-no-gen-params/[slug]/page.js',
'hooks/use-pathname/[slug]/page.js',
'hooks/use-search-params/[slug]/page.js',
'ssr-auto/cache-no-store/page.js',
'ssr-auto/fetch-revalidate-zero/page.js',
'ssr-forced/page.js',
Expand Down Expand Up @@ -358,4 +362,75 @@ describe('app-dir static/dynamic handling', () => {

expect(secondDate).not.toBe(initialDate)
})

describe('hooks', () => {
describe('useSearchParams', () => {
if (isDev) {
it('should bail out to client rendering during SSG', async () => {
const res = await fetchViaHTTP(
next.url,
'/hooks/use-search-params/slug'
)
const html = await res.text()
expect(html).toInclude('<html id="__next_error__">')
})
}

it('should have the correct values', async () => {
const browser = await webdriver(
next.url,
'/hooks/use-search-params/slug?first=value&second=other&third'
)

expect(await browser.elementByCss('#params-first').text()).toBe('value')
expect(await browser.elementByCss('#params-second').text()).toBe(
'other'
)
expect(await browser.elementByCss('#params-third').text()).toBe('')
expect(await browser.elementByCss('#params-not-real').text()).toBe(
'N/A'
)
})

it('should have values from canonical url on rewrite', async () => {
const browser = await webdriver(
next.url,
'/rewritten-use-search-params?first=a&second=b&third=c'
)

expect(await browser.elementByCss('#params-first').text()).toBe('a')
expect(await browser.elementByCss('#params-second').text()).toBe('b')
expect(await browser.elementByCss('#params-third').text()).toBe('c')
expect(await browser.elementByCss('#params-not-real').text()).toBe(
'N/A'
)
})
})

describe('usePathname', () => {
if (isDev) {
it('should bail out to client rendering during SSG', async () => {
const res = await fetchViaHTTP(next.url, '/hooks/use-pathname/slug')
const html = await res.text()
expect(html).toInclude('<html id="__next_error__">')
})
}

it('should have the correct values', async () => {
const browser = await webdriver(next.url, '/hooks/use-pathname/slug')

expect(await browser.elementByCss('#pathname').text()).toBe(
'/hooks/use-pathname/slug'
)
})

it('should have values from canonical url on rewrite', async () => {
const browser = await webdriver(next.url, '/rewritten-use-pathname')

expect(await browser.elementByCss('#pathname').text()).toBe(
'/rewritten-use-pathname'
)
})
})
})
})
@@ -0,0 +1,7 @@
export default function Layout({ children }) {
return children
}

export function generateStaticParams() {
return [{ slug: 'slug' }]
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/app-static/app/hooks/use-pathname/[slug]/page.js
@@ -0,0 +1,12 @@
'use client'
import { usePathname } from 'next/navigation'

export const config = {
dynamicParams: false,
}

export default function Page() {
const pathname = usePathname()

return <p id="pathname">{pathname}</p>
}
@@ -0,0 +1,7 @@
export default function Layout({ children }) {
return children
}

export function generateStaticParams() {
return [{ slug: 'slug' }]
}
@@ -0,0 +1,19 @@
'use client'
import { useSearchParams } from 'next/navigation'

export const config = {
dynamicParams: false,
}

export default function Page() {
const params = useSearchParams()

return (
<>
<p id="params-first">{params.get('first') ?? 'N/A'}</p>
<p id="params-second">{params.get('second') ?? 'N/A'}</p>
<p id="params-third">{params.get('third') ?? 'N/A'}</p>
<p id="params-not-real">{params.get('notReal') ?? 'N/A'}</p>
</>
)
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/app-static/next.config.js
Expand Up @@ -11,6 +11,15 @@ module.exports = {
source: '/rewritten-to-dashboard',
destination: '/dashboard',
},
{
source: '/rewritten-use-search-params',
destination:
'/hooks/use-search-params/slug?first=value&second=other%20value&third',
},
{
source: '/rewritten-use-pathname',
destination: '/hooks/use-pathname/slug',
},
],
}
},
Expand Down

0 comments on commit a577ff2

Please sign in to comment.