|
1 | | -import { expect, fireEvent, userEvent, waitFor, within } from '@storybook/test' |
| 1 | +import { expect, userEvent, waitFor, within } from '@storybook/test' |
2 | 2 | import { Meta, StoryObj } from '@storybook/react' |
3 | 3 | import { cookies } from '@storybook/nextjs/headers.mock' |
4 | 4 | import Page from './page' |
5 | | -import { deleteNote, saveNote } from '#app/actions.mock' |
6 | 5 | import { db } from '#lib/db' |
7 | 6 | import { createUserCookie, userCookieKey } from '#lib/session' |
8 | 7 | import { PageDecorator } from '#.storybook/decorators' |
| 8 | +import { getRouter } from '@storybook/nextjs/navigation.mock' |
9 | 9 |
|
10 | 10 | const meta = { |
11 | 11 | component: Page, |
@@ -49,43 +49,55 @@ export const UnknownId: Story = { |
49 | 49 | args: { params: { id: '999' } }, |
50 | 50 | } |
51 | 51 |
|
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 = { |
54 | 56 | play: async ({ canvasElement, step }) => { |
55 | 57 | 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 | + ) |
60 | 64 |
|
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') |
65 | 69 |
|
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() |
70 | 74 |
|
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 | +} |
82 | 83 |
|
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) |
90 | 102 | }, |
91 | 103 | } |
0 commit comments