Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for providing correct params to layouts #43775

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 99 additions & 0 deletions test/e2e/app-dir/layout-params.test.ts
@@ -0,0 +1,99 @@
import path from 'path'
import { renderViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import cheerio from 'cheerio'

describe('app dir - layout params', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, './layout-params')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
})
})
afterAll(() => next.destroy())

const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'

if (isReact17) {
it('should skip tests for react 17', () => {})
return
}

describe('basic params', () => {
it('check layout without params get no params', async () => {
const html = await renderViaHTTP(next.url, '/base/something/another')

const $ = cheerio.load(html)

const ids = ['#root-layout', '#lvl1-layout']
ids.forEach((divId) => {
const params = $(`${divId} > div`)
expect(params.length).toBe(0)
})
})

it("check layout renders just it's params", async () => {
const html = await renderViaHTTP(next.url, '/base/something/another')

const $ = cheerio.load(html)

expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-param1').text()).toBe('"something"')
})

it('check topmost layout renders all params', async () => {
const html = await renderViaHTTP(next.url, '/base/something/another')

const $ = cheerio.load(html)

expect($('#lvl3-layout > div').length).toBe(2)
expect($('#lvl3-param1').text()).toBe('"something"')
expect($('#lvl3-param2').text()).toBe('"another"')
})
})

describe('catchall params', () => {
it('should give catchall params just to last layout', async () => {
const html = await renderViaHTTP(next.url, '/catchall/something/another')

const $ = cheerio.load(html)

expect($(`#root-layout > div`).length).toBe(0)

expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-params').text()).toBe('["something","another"]')
})

it('should give optional catchall params just to last layout', async () => {
const html = await renderViaHTTP(
next.url,
'/optional-catchall/something/another'
)

const $ = cheerio.load(html)

expect($(`#root-layout > div`).length).toBe(0)

expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-params').text()).toBe('["something","another"]')
})

it("should give empty optional catchall params won't give params to any layout", async () => {
const html = await renderViaHTTP(next.url, '/optional-catchall')

const $ = cheerio.load(html)

expect($(`#root-layout > div`).length).toBe(0)
expect($('#lvl2-layout > div').length).toBe(0)
})
})
})
@@ -0,0 +1,17 @@
import React from 'react'
import ShowParams from '../../../show-params'

export default function Layout({
children,
params,
}: {
children: React.ReactNode
params: {}
}) {
return (
<div>
<ShowParams prefix="lvl3" params={params} />
{children}
</div>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
17 changes: 17 additions & 0 deletions test/e2e/app-dir/layout-params/app/base/[param1]/layout.tsx
@@ -0,0 +1,17 @@
import React from 'react'
import ShowParams from '../../show-params'

export default function Layout({
children,
params,
}: {
children: React.ReactNode
params: {}
}) {
return (
<div>
<ShowParams prefix="lvl2" params={params} />
{children}
</div>
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/layout-params/app/base/[param1]/page.tsx
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
17 changes: 17 additions & 0 deletions test/e2e/app-dir/layout-params/app/base/layout.tsx
@@ -0,0 +1,17 @@
import React from 'react'
import ShowParams from '../show-params'

export default function Layout({
children,
params,
}: {
children: React.ReactNode
params: {}
}) {
return (
<div>
<ShowParams prefix="lvl1" params={params} />
{children}
</div>
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/layout-params/app/base/page.tsx
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
@@ -0,0 +1,17 @@
import React from 'react'
import ShowParams from '../../show-params'

export default function Layout({
children,
params,
}: {
children: React.ReactNode
params: {}
}) {
return (
<div>
<ShowParams prefix="lvl2" params={params} />
{children}
</div>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
22 changes: 22 additions & 0 deletions test/e2e/app-dir/layout-params/app/layout.tsx
@@ -0,0 +1,22 @@
import React from 'react'
import ShowParams from './show-params'

export default function Layout({
children,
params,
}: {
children: React.ReactNode
params: {}
}) {
return (
<html>
<head></head>
<body>
<div>
<ShowParams prefix="root" params={params} />
{children}
</div>
</body>
</html>
)
}
@@ -0,0 +1,17 @@
import React from 'react'
import ShowParams from '../../show-params'

export default function Layout({
children,
params,
}: {
children: React.ReactNode
params: {}
}) {
return (
<div>
<ShowParams prefix="lvl2" params={params} />
{children}
</div>
)
}
@@ -0,0 +1,3 @@
export default function Page() {
return null
}
19 changes: 19 additions & 0 deletions test/e2e/app-dir/layout-params/app/show-params.tsx
@@ -0,0 +1,19 @@
import React from 'react'

export default function ShowParams({
prefix,
params,
}: {
prefix: string
params: Record<string, unknown>
}) {
return (
<div id={`${prefix}-layout`}>
{Object.entries(params).map(([key, val]) => (
<div key={key} id={`${prefix}-${key}`}>
{JSON.stringify(val)}
</div>
))}
</div>
)
}
@@ -1,6 +1,5 @@
module.exports = {
experimental: {
appDir: true,
fontLoaders: [],
jankaifer marked this conversation as resolved.
Show resolved Hide resolved
},
}