From a4b05ec06e212a5dc62a18652dd8b8bb2e2955d4 Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Thu, 2 May 2024 17:01:53 +0200 Subject: [PATCH] Cleanup tests --- .../ServerActions.stories.tsx | 78 +++++++++++-------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/code/frameworks/nextjs/template/stories_nextjs-default-ts/ServerActions.stories.tsx b/code/frameworks/nextjs/template/stories_nextjs-default-ts/ServerActions.stories.tsx index 8bfea13b0498..f330b2af3b9b 100644 --- a/code/frameworks/nextjs/template/stories_nextjs-default-ts/ServerActions.stories.tsx +++ b/code/frameworks/nextjs/template/stories_nextjs-default-ts/ServerActions.stories.tsx @@ -32,6 +32,12 @@ function Component() { export default { component: Component, parameters: { + nextjs: { + appDirectory: true, + navigation: { + pathname: '/', + }, + }, test: { // This is needed until Next will update to the React 19 beta: https://github.com/vercel/next.js/pull/65058 // In the React 19 beta ErrorBoundary errors (such as redirect) are only logged, and not thrown. @@ -50,43 +56,51 @@ export default { }, } as Meta; -export const Default: StoryObj = { - play: async ({ canvasElement, step }) => { +export const ProtectedWhileLoggedOut: StoryObj = { + play: async ({ canvasElement }) => { + const canvas = within(canvasElement); + await userEvent.click(canvas.getByText('Access protected route')); + + await expect(cookies().get).toHaveBeenCalledWith('user'); + await expect(redirect).toHaveBeenCalledWith('/'); + }, +}; + +export const ProtectedWhileLoggedIn: StoryObj = { + beforeEach() { + cookies().set('user', 'storybookjs'); + }, + play: async ({ canvasElement }) => { const canvas = within(canvasElement); + await userEvent.click(canvas.getByText('Access protected route')); - const loginBtn = canvas.getByText('Login'); - const logoutBtn = canvas.getByText('Logout'); - const accessRouteBtn = canvas.getByText('Access protected route'); + await expect(cookies().get).toHaveBeenLastCalledWith('user'); + await expect(revalidatePath).toHaveBeenLastCalledWith('/'); + await expect(redirect).toHaveBeenLastCalledWith('/protected'); + }, +}; - await step('accessRoute flow - logged out', async () => { - await userEvent.click(accessRouteBtn); - await expect(cookies().get).toHaveBeenCalledWith('user'); - await expect(redirect).toHaveBeenCalledWith('/'); - }); +export const Logout: StoryObj = { + beforeEach() { + cookies().set('user', 'storybookjs'); + }, + play: async ({ canvasElement }) => { + const canvas = within(canvasElement); - await step('accessRoute flow - logged', async () => { - cookies.mockRestore(); - cookies().set('user', 'storybookjs'); - await userEvent.click(accessRouteBtn); - await expect(cookies().get).toHaveBeenCalledWith('user'); - await expect(revalidatePath).toHaveBeenCalledWith('/'); - await expect(redirect).toHaveBeenCalledWith('/protected'); - }); + await userEvent.click(canvas.getByText('Logout')); + await expect(cookies().delete).toHaveBeenCalled(); + await expect(revalidatePath).toHaveBeenCalledWith('/'); + await expect(redirect).toHaveBeenCalledWith('/'); + }, +}; - await step('logout flow', async () => { - cookies.mockRestore(); - await userEvent.click(logoutBtn); - await expect(cookies().delete).toHaveBeenCalled(); - await expect(revalidatePath).toHaveBeenCalledWith('/'); - await expect(redirect).toHaveBeenCalledWith('/'); - }); +export const Login: StoryObj = { + play: async ({ canvasElement }) => { + const canvas = within(canvasElement); + await userEvent.click(canvas.getByText('Login')); - await step('login flow', async () => { - cookies.mockRestore(); - await userEvent.click(loginBtn); - await expect(cookies().set).toHaveBeenCalledWith('user', 'storybookjs'); - await expect(revalidatePath).toHaveBeenCalledWith('/'); - await expect(redirect).toHaveBeenCalledWith('/'); - }); + await expect(cookies().set).toHaveBeenCalledWith('user', 'storybookjs'); + await expect(revalidatePath).toHaveBeenCalledWith('/'); + await expect(redirect).toHaveBeenCalledWith('/'); }, };