Skip to content

Commit 9ed099a

Browse files
committed
Fix flaky test
1 parent 38d635c commit 9ed099a

File tree

2 files changed

+65
-49
lines changed

2 files changed

+65
-49
lines changed

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

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { expect, fireEvent, userEvent, waitFor, within } from '@storybook/test'
1+
import { expect, userEvent, waitFor, within } from '@storybook/test'
22
import { Meta, StoryObj } from '@storybook/react'
33
import { cookies } from '@storybook/nextjs/headers.mock'
44
import Page from './page'
5-
import { deleteNote, saveNote } from '#app/actions.mock'
65
import { db } from '#lib/db'
76
import { createUserCookie, userCookieKey } from '#lib/session'
87
import { PageDecorator } from '#.storybook/decorators'
8+
import { getRouter } from '@storybook/nextjs/navigation.mock'
99

1010
const meta = {
1111
component: Page,
@@ -49,43 +49,55 @@ export const UnknownId: Story = {
4949
args: { params: { id: '999' } },
5050
}
5151

52-
export const Save: Story = {
53-
name: 'Save and Delete Flow ▶',
52+
const waitForRedirect = () =>
53+
waitFor(() => expect(getRouter().push).toHaveBeenCalled())
54+
55+
export const SavingExistingNoteShouldUpdateDBAndRedirect: Story = {
5456
play: async ({ canvasElement, step }) => {
5557
const canvas = within(canvasElement)
56-
const titleInput = await canvas.findByRole('textbox', {
57-
name: /Enter a title for your note/i,
58-
})
59-
const bodyInput = canvas.getByRole('textbox', { name: /body/i })
58+
const titleInput = await canvas.findByLabelText(
59+
'Enter a title for your note',
60+
)
61+
const bodyInput = await canvas.findByLabelText(
62+
'Enter the body for your note',
63+
)
6064

61-
await step('Clear inputs', async () => {
62-
await userEvent.clear(titleInput)
63-
await userEvent.clear(bodyInput)
64-
})
65+
await userEvent.clear(titleInput)
66+
await userEvent.type(titleInput, 'Edited Title')
67+
await userEvent.clear(bodyInput)
68+
await userEvent.type(bodyInput, 'Edited Body')
6569

66-
await step('Edit inputs', async () => {
67-
await fireEvent.change(titleInput, { target: { value: 'Edited Title' } })
68-
await fireEvent.change(bodyInput, { target: { value: 'Edited Body' } })
69-
})
70+
await userEvent.click(
71+
await canvas.findByRole('menuitem', { name: /done/i }),
72+
)
73+
await waitForRedirect()
7074

71-
await step('Save', async () => {
72-
await userEvent.click(
73-
await canvas.findByRole('menuitem', { name: /done/i }),
74-
)
75-
await expect(saveNote).toHaveBeenCalledOnce()
76-
await expect(saveNote).toHaveBeenCalledWith(
77-
2,
78-
'Edited Title',
79-
'Edited Body',
80-
)
81-
})
75+
await expect(await db.note.findUnique({ where: { id: 2 } })).toEqual(
76+
expect.objectContaining({
77+
title: 'Edited Title',
78+
body: 'Edited Body',
79+
}),
80+
)
81+
},
82+
}
8283

83-
await step('Delete', async () => {
84-
await userEvent.click(
85-
await canvas.findByRole('menuitem', { name: /delete/i }),
86-
)
87-
await waitFor(() => expect(deleteNote).toHaveBeenCalledOnce())
88-
await expect(deleteNote).toHaveBeenCalledWith(2)
89-
})
84+
export const DeleteNoteRemovesFromDBAndSidebar: Story = {
85+
play: async ({ canvasElement, step }) => {
86+
const canvas = within(canvasElement)
87+
88+
await expect(
89+
await db.note.findMany({ where: { id: 2 } }),
90+
'Note with id 2 does exist',
91+
).toHaveLength(1)
92+
93+
await userEvent.click(
94+
await canvas.findByRole('menuitem', { name: /delete/i }),
95+
)
96+
await waitForRedirect()
97+
98+
await expect(
99+
await db.note.findMany({ where: { id: 2 } }),
100+
'Note with id 2 does not exist anymore',
101+
).toHaveLength(0)
90102
},
91103
}

components/note-ui.stories.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const meta = {
1010
component: NoteUI,
1111
async beforeEach() {
1212
cookies().set(userCookieKey, await createUserCookie('storybookjs'))
13-
}
13+
},
1414
} satisfies Meta<typeof NoteUI>
1515

1616
export default meta
@@ -35,28 +35,32 @@ export const EditModeFlow: Story = {
3535
},
3636
play: async ({ canvasElement, step }) => {
3737
const canvas = within(canvasElement)
38-
const titleInput = canvas.getByRole('textbox', { name: /title/i })
39-
const bodyInput = canvas.getByRole('textbox', { name: /body/i })
40-
41-
await step('Clear inputs', async () => {
42-
await userEvent.clear(titleInput)
43-
await userEvent.clear(bodyInput)
44-
})
45-
46-
await step('Edit inputs', async () => {
47-
await fireEvent.change(titleInput, { target: { value: 'Edited Title' } })
48-
await fireEvent.change(bodyInput, { target: { value: 'Edited Body' } })
49-
})
38+
const titleInput = await canvas.findByLabelText(
39+
'Enter a title for your note',
40+
)
41+
const bodyInput = await canvas.findByLabelText(
42+
'Enter the body for your note',
43+
)
44+
await userEvent.clear(titleInput)
45+
await userEvent.type(titleInput, 'Edited Title')
46+
await userEvent.clear(bodyInput)
47+
await userEvent.type(bodyInput, 'Edited Body')
5048

5149
await step('Save flow', async () => {
5250
const saveButton = await canvas.findByRole('menuitem', { name: /done/i })
5351
await userEvent.click(saveButton)
5452
await expect(saveNote).toHaveBeenCalledOnce()
55-
await expect(saveNote).toHaveBeenCalledWith(1, 'Edited Title', 'Edited Body')
53+
await expect(saveNote).toHaveBeenCalledWith(
54+
1,
55+
'Edited Title',
56+
'Edited Body',
57+
)
5658
})
5759

5860
await step('Delete flow', async () => {
59-
const deleteButton = await canvas.findByRole('menuitem', { name: /delete/i })
61+
const deleteButton = await canvas.findByRole('menuitem', {
62+
name: /delete/i,
63+
})
6064
await userEvent.click(deleteButton)
6165
await expect(deleteNote).toHaveBeenCalledOnce()
6266
await expect(deleteNote).toHaveBeenCalledWith(1)

0 commit comments

Comments
 (0)