Skip to content

Commit 380a034

Browse files
committed
02/03: reuse navigate fixture
1 parent d9e583b commit 380a034

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

exercises/02.test-setup/03.problem.authentication/tests/e2e/notes.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ test('creates a new note', async ({
1515
//
1616
// 🐨 Navigate to the new note route in the app.
1717
// Reference the username from the authenticated "user" object.
18-
// 💰 `/users/${username}/notes/new`
19-
// 💰 await page.goto(ROUTE)
18+
// 💰 `/users/:username/notes/new`
19+
// 💰 await navigate(ROUTE, PARAMS)
2020
//
2121
// 🐨 Interact with the "New note" form to fill in the note's title and content.
2222
// 💰 await page.getByLabel('Title').fill(NOTE_TITLE)

exercises/02.test-setup/03.problem.authentication/tests/test-extend.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@ import { test as testBase, expect } from '@playwright/test'
33
// from the `playwright-persona` package
44
// 💰 import { one, two, three } from 'playwright-persona'
55

6+
import { href, type Register } from 'react-router'
67
import { getPasswordHash } from '#app/utils/auth.server.ts'
78
import { prisma } from '#app/utils/db.server.ts'
89
import { createUser } from '#tests/db-utils'
910

11+
interface Fixtures {
12+
navigate: <T extends keyof Register['pages']>(
13+
...args: Parameters<typeof href<T>>
14+
) => Promise<void>
15+
16+
// 🐨 Annotate the custom authentication fixture by defining a key
17+
// called "authenticate" and its type as "AuthenticateFunction" type
18+
// you've imported earlier. Provide the list of persona types as the type
19+
// argument to the "AuthenticateFunction" type.
20+
// 💰 authenticate: AuthenticateFunction<[typeof personaOne, typeof personaTwo]>
21+
}
22+
1023
// 🐨 Declare a new variable called `user` and assign it the result of
1124
// calling the `definePersona` function.
1225
// 💰 const user = definePersona()
@@ -78,13 +91,13 @@ import { createUser } from '#tests/db-utils'
7891
// 💰 await destroySession({ session }) {}
7992
// 💰 await prisma.user.deleteMany({ where: { id: session.user.id } })
8093

81-
export const test = testBase.extend<{
82-
// 🐨 Annotate the custom authentication fixture by defining a key
83-
// called "authenticate" and its type as "AuthenticateFunction" type
84-
// you've imported earlier. Provide the list of persona types as the type
85-
// argument to the "AuthenticateFunction" type.
86-
// 💰 authenticate: AuthenticateFunction<[typeof personaOne, typeof personaTwo]>
87-
}>({
94+
export const test = testBase.extend<Fixtures>({
95+
async navigate({ page }, use) {
96+
await use(async (...args) => {
97+
await page.goto(href(...args))
98+
})
99+
},
100+
88101
// 🐨 Declare a new fixture called "authenticate".
89102
// As the value of this fixture, provide it the result of calling
90103
// the "combinePersonas" function you've imported earlier.

exercises/02.test-setup/03.solution.authentication/tests/e2e/notes.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { test, expect } from '#tests/test-extend.ts'
22

3-
test('creates a new note', async ({ authenticate, page }) => {
3+
test('creates a new note', async ({ navigate, authenticate, page }) => {
44
const { user } = await authenticate({ as: 'user' })
55

6-
await page.goto(`/users/${user.username}/notes/new`)
6+
await navigate('/users/:username/notes/new', { username: user.username })
77

88
await page.getByLabel('Title').fill('My new note')
99
await page.getByLabel('Content').fill('Hello world')

exercises/02.test-setup/03.solution.authentication/tests/test-extend.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@ import {
44
combinePersonas,
55
type AuthenticateFunction,
66
} from 'playwright-persona'
7+
import { href, type Register } from 'react-router'
78
import { getPasswordHash } from '#app/utils/auth.server.ts'
89
import { prisma } from '#app/utils/db.server.ts'
910
import { createUser } from '#tests/db-utils'
1011

12+
interface Fixtures {
13+
navigate: <T extends keyof Register['pages']>(
14+
...args: Parameters<typeof href<T>>
15+
) => Promise<void>
16+
17+
authenticate: AuthenticateFunction<[typeof user]>
18+
}
19+
1120
const user = definePersona('user', {
1221
async createSession({ page }) {
1322
const user = await prisma.user.create({
@@ -35,9 +44,12 @@ const user = definePersona('user', {
3544
},
3645
})
3746

38-
export const test = testBase.extend<{
39-
authenticate: AuthenticateFunction<[typeof user]>
40-
}>({
47+
export const test = testBase.extend<Fixtures>({
48+
async navigate({ page }, use) {
49+
await use(async (...args) => {
50+
await page.goto(href(...args))
51+
})
52+
},
4153
authenticate: combinePersonas(user),
4254
})
4355

0 commit comments

Comments
 (0)