Skip to content

Commit

Permalink
expect Redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed May 16, 2024
1 parent 5edb531 commit ba6728b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions app/note/[id]/page.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { createUserCookie, userCookieKey } from '#lib/session'
import { PageDecorator } from '#.storybook/decorators'
import { login } from '#app/actions.mock'
import * as auth from '#app/auth/route'
import { expectRedirect } from '#lib/test-utils'
import { getRouter } from '@storybook/nextjs/navigation.mock'

const meta = {
component: Page,
Expand Down Expand Up @@ -84,9 +86,8 @@ export const LoginShouldGetOAuthTokenAndSetCookie: Story = {
await userEvent.click(
await canvas.findByRole('menuitem', { name: /login to add/i }),
)
await waitFor(async () => {
await expect(cookies().get(userCookieKey)?.value).toContain('storybookjs')
})
await expectRedirect('/')
await expect(cookies().get(userCookieKey)?.value).toContain('storybookjs')
},
}

Expand All @@ -98,6 +99,7 @@ export const LogoutShouldDeleteCookie: Story = {
const canvas = within(canvasElement)
await expect(cookies().get(userCookieKey)?.value).toContain('storybookjs')
await userEvent.click(await canvas.findByRole('button', { name: 'logout' }))
await expectRedirect('/')
await expect(cookies().get(userCookieKey)).toBeUndefined()
},
}
Expand Down
5 changes: 3 additions & 2 deletions lib/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect, waitFor } from '@storybook/test'
import { getRouter } from '@storybook/nextjs/navigation.mock'

export const expectRedirect = (url: string) =>
waitFor(() =>
export const expectRedirect = async (url: string) => {
await waitFor(() =>
expect(getRouter().push).toHaveBeenLastCalledWith(url, expect.anything()),
)
}

0 comments on commit ba6728b

Please sign in to comment.