Skip to content

Commit

Permalink
Create expectRedirect test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed May 15, 2024
1 parent c777b40 commit a3353a6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
9 changes: 3 additions & 6 deletions app/note/edit/[id]/page.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Page from './page'
import { db } from '#lib/db'
import { createUserCookie, userCookieKey } from '#lib/session'
import { PageDecorator } from '#.storybook/decorators'
import { getRouter } from '@storybook/nextjs/navigation.mock'
import { expectRedirect } from '#lib/test-utils'

const meta = {
component: Page,
Expand Down Expand Up @@ -49,9 +49,6 @@ export const UnknownId: Story = {
args: { params: { id: '999' } },
}

const waitForRedirect = () =>
waitFor(() => expect(getRouter().push).toHaveBeenCalled())

export const SavingExistingNoteShouldUpdateDBAndRedirect: Story = {
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
Expand All @@ -70,7 +67,7 @@ export const SavingExistingNoteShouldUpdateDBAndRedirect: Story = {
await userEvent.click(
await canvas.findByRole('menuitem', { name: /done/i }),
)
await waitForRedirect()
await expectRedirect()

await expect(await db.note.findUnique({ where: { id: 2 } })).toEqual(
expect.objectContaining({
Expand All @@ -93,7 +90,7 @@ export const DeleteNoteRemovesFromDBAndSidebar: Story = {
await userEvent.click(
await canvas.findByRole('menuitem', { name: /delete/i }),
)
await waitForRedirect()
await expectRedirect()

await expect(
await db.note.findMany({ where: { id: 2 } }),
Expand Down
9 changes: 3 additions & 6 deletions app/note/edit/page.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, userEvent, waitFor, within } from '@storybook/test'
import { expect, userEvent, within } from '@storybook/test'
import { Meta, StoryObj } from '@storybook/react'
import { cookies } from '@storybook/nextjs/headers.mock'
import Page from './page'
import { db } from '#lib/db'
import { createUserCookie, userCookieKey } from '#lib/session'
import { PageDecorator } from '#.storybook/decorators'
import { getRouter } from '@storybook/nextjs/navigation.mock'
import { expectRedirect } from '#lib/test-utils'

const meta = {
component: Page,
Expand Down Expand Up @@ -43,9 +43,6 @@ type Story = StoryObj<typeof meta>

export const EditNewNote: Story = {}

const waitForRedirect = () =>
waitFor(() => expect(getRouter().push).toHaveBeenCalled())

export const SaveNewNote: Story = {
play: async ({ canvasElement, step }) => {
const canvas = within(canvasElement)
Expand All @@ -63,7 +60,7 @@ export const SaveNewNote: Story = {
await canvas.findByRole('menuitem', { name: /done/i }),
)

await waitForRedirect()
await expectRedirect()

await expect(await db.note.findUnique({ where: { id: 3 } })).toEqual(
expect.objectContaining({
Expand Down
5 changes: 5 additions & 0 deletions lib/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { expect, waitFor } from '@storybook/test'
import { getRouter } from '@storybook/nextjs/navigation.mock'

export const expectRedirect = () =>
waitFor(() => expect(getRouter().push).toHaveBeenCalled())

0 comments on commit a3353a6

Please sign in to comment.