rsnext/test/e2e/app-dir/use-selected-layout-segment-s/use-selected-layout-segment-s.test.ts
Tim Neutkens 68abad58ed
Fix useSelectedLayoutSegments including __PAGE__ (#47492)
### What?

Excludes `__PAGE__` elements from `useSelectedLayoutSegment` as it's not
a public api.
This ensures the exclude still applies when there are searchParams part
of the key.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
2023-03-24 16:13:22 +01:00

146 lines
4.6 KiB
TypeScript

import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import webdriver from 'next-webdriver'
import { check } from 'next-test-utils'
describe('useSelectedLayoutSegment(s)', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(__dirname),
})
})
afterAll(() => next.destroy())
let browser: Awaited<ReturnType<typeof webdriver>>
beforeEach(async () => {
browser = await webdriver(
next.url,
'/segment-name/value1/segment-name2/value2/value3/value4'
)
})
it('should return correct values for root layout', async () => {
expect(
await browser.elementByCss('#root > .segments').text()
).toMatchInlineSnapshot(
`"[\\"segment-name\\",\\"value1\\",\\"segment-name2\\",\\"value2\\",\\"value3/value4\\"]"`
)
expect(
await browser.elementByCss('#root > .segment').text()
).toMatchInlineSnapshot(`"\\"segment-name\\""`)
})
it('should return correct values in layout before static segment', async () => {
expect(
await browser.elementByCss('#before-static > .segments').text()
).toMatchInlineSnapshot(
`"[\\"segment-name2\\",\\"value2\\",\\"value3/value4\\"]"`
)
expect(
await browser.elementByCss('#before-static > .segment').text()
).toMatchInlineSnapshot(`"\\"segment-name2\\""`)
})
it('should return correct values in layout before param segment', async () => {
expect(
await browser.elementByCss('#before-param > .segments').text()
).toMatchInlineSnapshot(`"[\\"value2\\",\\"value3/value4\\"]"`)
expect(
await browser.elementByCss('#before-param > .segment').text()
).toMatchInlineSnapshot(`"\\"value2\\""`)
})
it('should return correct values in layout before catchall segment', async () => {
expect(
await browser.elementByCss('#before-catchall > .segments').text()
).toMatchInlineSnapshot(`"[\\"value3/value4\\"]"`)
expect(
await browser.elementByCss('#before-catchall > .segment').text()
).toMatchInlineSnapshot(`"\\"value3/value4\\""`)
})
it('should return correct values in layout after last segment', async () => {
expect(
await browser.elementByCss('#final > .segments').text()
).toMatchInlineSnapshot(`"[]"`)
expect(
await browser.elementByCss('#final > .segment').text()
).toMatchInlineSnapshot(`"null"`)
})
it('should correctly update when changing static segment', async () => {
browser.elementById('change-static').click()
await check(
() => browser.eval('window.location.pathname'),
'/segment-name/param1/different-segment'
)
expect(
await browser.elementByCss('#root > .segments').text()
).toMatchInlineSnapshot(
`"[\\"segment-name\\",\\"param1\\",\\"different-segment\\"]"`
)
expect(
await browser.elementByCss('#before-static > .segments').text()
).toMatchInlineSnapshot(`"[\\"different-segment\\"]"`)
expect(
await browser.elementByCss('#before-static > .segment').text()
).toMatchInlineSnapshot(`"\\"different-segment\\""`)
})
it('should correctly update when changing param segment', async () => {
browser.elementById('change-param').click()
await check(
() => browser.eval('window.location.pathname'),
'/segment-name/param1/segment-name2/different-value/value3/value4'
)
expect(
await browser.elementByCss('#root > .segments').text()
).toMatchInlineSnapshot(
`"[\\"segment-name\\",\\"param1\\",\\"segment-name2\\",\\"different-value\\",\\"value3/value4\\"]"`
)
expect(
await browser.elementByCss('#before-param > .segments').text()
).toMatchInlineSnapshot(`"[\\"different-value\\",\\"value3/value4\\"]"`)
expect(
await browser.elementByCss('#before-param > .segment').text()
).toMatchInlineSnapshot(`"\\"different-value\\""`)
})
it('should correctly update when changing catchall segment', async () => {
browser.elementById('change-catchall').click()
await check(
() => browser.eval('window.location.pathname'),
'/segment-name/param1/segment-name2/value2/different/random/paths'
)
expect(
await browser.elementByCss('#root > .segments').text()
).toMatchInlineSnapshot(
`"[\\"segment-name\\",\\"param1\\",\\"segment-name2\\",\\"value2\\",\\"different/random/paths\\"]"`
)
expect(
await browser.elementByCss('#before-catchall > .segments').text()
).toMatchInlineSnapshot(`"[\\"different/random/paths\\"]"`)
expect(
await browser.elementByCss('#before-catchall > .segment').text()
).toMatchInlineSnapshot(`"\\"different/random/paths\\""`)
})
})