Skip to content

Commit

Permalink
Wait for redirect to have happened
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed May 2, 2024
1 parent a4b05ec commit a79bebd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions code/frameworks/nextjs/src/export-mocks/navigation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export * from 'next/dist/client/components/navigation';

// mock utilities/overrides (as of Next v14.2.0)
export const redirect = fn(
(url: string, type: actual.RedirectType = actual.RedirectType.replace): never => {
(url: string, type: actual.RedirectType = actual.RedirectType.push): never => {
throw getRedirectError(url, type, RedirectStatusCode.SeeOther);
}
).mockName('next/navigation::redirect');

export const permanentRedirect = fn(
(url: string, type: actual.RedirectType = actual.RedirectType.replace): never => {
(url: string, type: actual.RedirectType = actual.RedirectType.push): never => {
throw getRedirectError(url, type, RedirectStatusCode.SeeOther);
}
).mockName('next/navigation::permanentRedirect');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import { expect, within, userEvent } from '@storybook/test';
import { expect, within, userEvent, waitFor } from '@storybook/test';
import { cookies } from '@storybook/nextjs/headers.mock';
import { revalidatePath } from '@storybook/nextjs/cache.mock';
import { redirect } from '@storybook/nextjs/navigation.mock';
import { redirect, getRouter } from '@storybook/nextjs/navigation.mock';

import { accessRoute, login, logout } from './server-actions';

Expand Down Expand Up @@ -63,6 +63,8 @@ export const ProtectedWhileLoggedOut: StoryObj<typeof Component> = {

await expect(cookies().get).toHaveBeenCalledWith('user');
await expect(redirect).toHaveBeenCalledWith('/');

await waitFor(() => expect(getRouter().push).toHaveBeenCalled());
},
};

Expand All @@ -77,6 +79,8 @@ export const ProtectedWhileLoggedIn: StoryObj<typeof Component> = {
await expect(cookies().get).toHaveBeenLastCalledWith('user');
await expect(revalidatePath).toHaveBeenLastCalledWith('/');
await expect(redirect).toHaveBeenLastCalledWith('/protected');

await waitFor(() => expect(getRouter().push).toHaveBeenCalled());
},
};

Expand All @@ -91,6 +95,8 @@ export const Logout: StoryObj<typeof Component> = {
await expect(cookies().delete).toHaveBeenCalled();
await expect(revalidatePath).toHaveBeenCalledWith('/');
await expect(redirect).toHaveBeenCalledWith('/');

await waitFor(() => expect(getRouter().push).toHaveBeenCalled());
},
};

Expand All @@ -102,5 +108,7 @@ export const Login: StoryObj<typeof Component> = {
await expect(cookies().set).toHaveBeenCalledWith('user', 'storybookjs');
await expect(revalidatePath).toHaveBeenCalledWith('/');
await expect(redirect).toHaveBeenCalledWith('/');

await waitFor(() => expect(getRouter().push).toHaveBeenCalled());
},
};

0 comments on commit a79bebd

Please sign in to comment.