Skip to content

Commit 28228b4

Browse files
committed
Use .test syntax in RSC demo
1 parent a0eac53 commit 28228b4

File tree

4 files changed

+71
-74
lines changed

4 files changed

+71
-74
lines changed

.storybook/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineMain({
1414
},
1515
features: {
1616
experimentalRSC: true,
17+
experimentalTestSyntax: true,
1718
},
1819
staticDirs: ['../public'],
1920
async viteFinal(config) {

app/note/[id]/page.stories.tsx

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,54 +40,47 @@ export const LoggedIn = meta.story({
4040
},
4141
})
4242

43+
LoggedIn.test('log out should delete cookie', async ({ canvas, userEvent }) => {
44+
await expect((await cookies()).get(userCookieKey)?.value).toContain('storybookjs')
45+
await userEvent.click(await canvas.findByRole('button', { name: 'logout' }))
46+
await expectToHaveBeenNavigatedTo({ pathname: '/' })
47+
await expect((await cookies()).get(userCookieKey)).toBeUndefined()
48+
})
49+
4350
export const NotLoggedIn = meta.story()
4451

45-
export const LoginShouldGetOAuthTokenAndSetCookie = meta.story({
46-
parameters: {
47-
msw: {
48-
// Mock out OAUTH
49-
handlers: [
50-
http.post('https://github.com/login/oauth/access_token', async ({ request }) => {
51-
let json = (await request.json()) as any
52-
return Response.json({ access_token: json.code })
53-
}),
54-
http.get('https://api.github.com/user', async ({ request }) =>
55-
Response.json({
56-
login: request.headers.get('Authorization')?.replace('token ', ''),
52+
NotLoggedIn.test(
53+
'login should get oauth token and set cookie',
54+
{
55+
parameters: {
56+
msw: {
57+
// Mock out OAUTH
58+
handlers: [
59+
http.post('https://github.com/login/oauth/access_token', async ({ request }) => {
60+
let json = (await request.json()) as any
61+
return Response.json({ access_token: json.code })
5762
}),
58-
),
59-
],
63+
http.get('https://api.github.com/user', async ({ request }) =>
64+
Response.json({
65+
login: request.headers.get('Authorization')?.replace('token ', ''),
66+
}),
67+
),
68+
],
69+
},
70+
},
71+
async beforeEach() {
72+
mocked(login).mockImplementation(async () => {
73+
return await auth.GET(new Request('/auth?code=storybookjs'))
74+
})
6075
},
6176
},
62-
play: async ({ mount, userEvent }) => {
63-
// Point the login implementation to the endpoint github would have redirected too.
64-
mocked(login).mockImplementation(async () => {
65-
return await auth.GET(new Request('/auth?code=storybookjs'))
66-
})
67-
const canvas = await mount()
77+
async ({ canvas, userEvent }) => {
6878
await expect((await cookies()).get(userCookieKey)?.value).toBeUndefined()
6979
await userEvent.click(await canvas.findByRole('menuitem', { name: /login to add/i }))
7080
await expectToHaveBeenNavigatedTo({ pathname: '/' })
7181
await expect((await cookies()).get(userCookieKey)?.value).toContain('storybookjs')
7282
},
73-
})
74-
75-
export const LogoutShouldDeleteCookie = meta.story({
76-
play: async ({ mount, userEvent }) => {
77-
;(await cookies()).set(userCookieKey, await createUserCookie('storybookjs'))
78-
const canvas = await mount()
79-
await expect((await cookies()).get(userCookieKey)?.value).toContain('storybookjs')
80-
await userEvent.click(await canvas.findByRole('button', { name: 'logout' }))
81-
await expectToHaveBeenNavigatedTo({ pathname: '/' })
82-
await expect((await cookies()).get(userCookieKey)).toBeUndefined()
83-
},
84-
})
85-
86-
// export const SearchInputShouldFilterNotes = meta.story({
87-
// parameters: {
88-
// nextjs: { navigation: { query: { q: 'RSC' } } },
89-
// },
90-
// })
83+
)
9184

9285
export const EmptyState = meta.story({
9386
async beforeEach() {
@@ -98,3 +91,9 @@ export const EmptyState = meta.story({
9891
export const Loading = meta.story({
9992
render: () => <NoteSkeleton />,
10093
})
94+
95+
// export const SearchInputShouldFilterNotes = meta.story({
96+
// parameters: {
97+
// nextjs: { navigation: { query: { q: 'RSC' } } },
98+
// },
99+
// })

app/note/edit/[id]/page.stories.tsx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ const meta = preview.meta({
3333

3434
export const EditNote = meta.story()
3535

36-
export const UnknownId = meta.story({
37-
args: { params: { id: '999' } },
38-
})
39-
40-
export const SavingExistingNoteShouldUpdateDBAndRedirect = meta.story({
41-
play: async ({ userEvent, canvas }) => {
36+
EditNote.test(
37+
'saving existing note should update db and redirect',
38+
async ({ canvas, userEvent }) => {
4239
const titleInput = await canvas.findByLabelText('Enter a title for your note')
4340
const bodyInput = await canvas.findByLabelText('Enter the body for your note')
4441

@@ -56,20 +53,22 @@ export const SavingExistingNoteShouldUpdateDBAndRedirect = meta.story({
5653
}),
5754
)
5855
},
59-
})
56+
)
6057

61-
export const DeleteNoteRemovesFromDBAndSidebar = meta.story({
62-
play: async ({ canvas, userEvent }) => {
63-
await expect(
64-
await db.note.findMany({ where: { id: 2 } }),
65-
'Note with id 2 does exist',
66-
).toHaveLength(1)
58+
EditNote.test('delete note removes from db and sidebar', async ({ canvas, userEvent }) => {
59+
await expect(
60+
await db.note.findMany({ where: { id: 2 } }),
61+
'Note with id 2 does exist',
62+
).toHaveLength(1)
6763

68-
await userEvent.click(await canvas.findByRole('menuitem', { name: /delete/i }))
69-
await expectToHaveBeenNavigatedTo({ pathname: '/' })
70-
await expect(
71-
await db.note.findMany({ where: { id: 2 } }),
72-
'Note with id 2 does not exist anymore',
73-
).toHaveLength(0)
74-
},
64+
await userEvent.click(await canvas.findByRole('menuitem', { name: /delete/i }))
65+
await expectToHaveBeenNavigatedTo({ pathname: '/' })
66+
await expect(
67+
await db.note.findMany({ where: { id: 2 } }),
68+
'Note with id 2 does not exist anymore',
69+
).toHaveLength(0)
70+
})
71+
72+
export const UnknownId = meta.story({
73+
args: { params: { id: '999' } },
7574
})

app/note/edit/page.stories.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,22 @@ const meta = preview.meta({
3333

3434
export const EditNewNote = meta.story()
3535

36-
export const SaveNewNote = meta.story({
37-
play: async ({ canvas, userEvent }) => {
38-
const titleInput = await canvas.findByLabelText('Enter a title for your note')
39-
const bodyInput = await canvas.findByLabelText('Enter the body for your note')
40-
await userEvent.clear(titleInput)
41-
await userEvent.type(titleInput, 'New Note Title', {})
42-
await userEvent.type(bodyInput, 'New Note Body')
43-
await userEvent.click(await canvas.findByRole('menuitem', { name: /done/i }))
36+
EditNewNote.test('save should create new note and redirect', async ({ canvas, userEvent }) => {
37+
const titleInput = await canvas.findByLabelText('Enter a title for your note')
38+
const bodyInput = await canvas.findByLabelText('Enter the body for your note')
39+
await userEvent.clear(titleInput)
40+
await userEvent.type(titleInput, 'New Note Title', {})
41+
await userEvent.type(bodyInput, 'New Note Body')
42+
await userEvent.click(await canvas.findByRole('menuitem', { name: /done/i }))
4443

45-
await expectToHaveBeenNavigatedTo({ pathname: '/note/3' })
44+
await expectToHaveBeenNavigatedTo({ pathname: '/note/3' })
4645

47-
await expect(await db.note.findUnique({ where: { id: 3 } })).toEqual(
48-
expect.objectContaining({
49-
title: 'New Note Title',
50-
body: 'New Note Body',
51-
}),
52-
)
53-
},
46+
await expect(await db.note.findUnique({ where: { id: 3 } })).toEqual(
47+
expect.objectContaining({
48+
title: 'New Note Title',
49+
body: 'New Note Body',
50+
}),
51+
)
5452
})
5553

5654
export const Loading = meta.story({

0 commit comments

Comments
 (0)