Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add ability to mock react-query errors
  • Loading branch information
code-asher committed Aug 23, 2024
commit a04365393a1291fcee9ca6b3eb061a8347a153f5
15 changes: 13 additions & 2 deletions site/.storybook/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
import { DecoratorHelpers } from "@storybook/addon-themes";
import { withRouter } from "storybook-addon-remix-react-router";
import { StrictMode } from "react";
import { QueryClient, QueryClientProvider } from "react-query";
import { parseQueryArgs, QueryClient, QueryClientProvider } from "react-query";
import { HelmetProvider } from "react-helmet-async";
import themes from "theme";
import "theme/globalFonts";
Expand Down Expand Up @@ -93,7 +93,18 @@ function withQuery(Story, { parameters }) {

if (parameters.queries) {
parameters.queries.forEach((query) => {
queryClient.setQueryData(query.key, query.data);
if (query.data instanceof Error) {
// This is copied from setQueryData() but sets the error.
const cache = queryClient.getQueryCache();
const parsedOptions = parseQueryArgs(query.key)
const defaultedOptions = queryClient.defaultQueryOptions(parsedOptions)
const cachedQuery = cache.build(queryClient, defaultedOptions);
// Set manual data so react-query will not try to refetch.
cachedQuery.setData(undefined, { manual: true });
cachedQuery.setState({ error: query.data });
} else {
queryClient.setQueryData(query.key, query.data);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ export const LoadingGroup: Story = {
},
};

// Will show a 404 because the query is not mocked.
export const GroupError: Story = {
parameters: {
queries: [permissionsQuery({})],
queries: [groupQuery(new Error("test group error")), permissionsQuery({})],
},
};

Expand Down Expand Up @@ -86,12 +85,12 @@ export const EveryoneGroup: Story = {
},
};

// Will show a 404 because the query is not mocked.
export const MembersError: Story = {
parameters: {
queries: [
groupQuery(MockGroup),
permissionsQuery({ canUpdateGroup: true }),
membersQuery(new Error("test members error")),
],
},
play: async ({ canvasElement }) => {
Expand Down
Loading