Skip to content

Commit 0bfabe0

Browse files
committed
Add ability to mock react-query errors
1 parent fd02c23 commit 0bfabe0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

site/.storybook/preview.jsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
77
import { DecoratorHelpers } from "@storybook/addon-themes";
88
import { withRouter } from "storybook-addon-remix-react-router";
99
import { StrictMode } from "react";
10-
import { QueryClient, QueryClientProvider } from "react-query";
10+
import { parseQueryArgs, QueryClient, QueryClientProvider } from "react-query";
1111
import { HelmetProvider } from "react-helmet-async";
1212
import themes from "theme";
1313
import "theme/globalFonts";
@@ -93,7 +93,18 @@ function withQuery(Story, { parameters }) {
9393

9494
if (parameters.queries) {
9595
parameters.queries.forEach((query) => {
96-
queryClient.setQueryData(query.key, query.data);
96+
if (query.data instanceof Error) {
97+
// This is copied from setQueryData() but sets the error.
98+
const cache = queryClient.getQueryCache();
99+
const parsedOptions = parseQueryArgs(query.key)
100+
const defaultedOptions = queryClient.defaultQueryOptions(parsedOptions)
101+
const test = cache.build(queryClient, defaultedOptions);
102+
// Set manual data so react-query will not try to refetch.
103+
test.setData(undefined, { manual: true });
104+
test.setState({ error: query.data });
105+
} else {
106+
queryClient.setQueryData(query.key, query.data);
107+
}
97108
});
98109
}
99110

site/src/pages/ManagementSettingsPage/GroupsPage/GroupPage.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const LoadingGroup: Story = {
5454
// Will show a 404 because the query is not mocked.
5555
export const GroupError: Story = {
5656
parameters: {
57-
queries: [permissionsQuery({})],
57+
queries: [groupQuery(new Error("test group error")), permissionsQuery({})],
5858
},
5959
};
6060

@@ -86,12 +86,12 @@ export const EveryoneGroup: Story = {
8686
},
8787
};
8888

89-
// Will show a 404 because the query is not mocked.
9089
export const MembersError: Story = {
9190
parameters: {
9291
queries: [
9392
groupQuery(MockGroup),
9493
permissionsQuery({ canUpdateGroup: true }),
94+
membersQuery(new Error("test members error")),
9595
],
9696
},
9797
play: async ({ canvasElement }) => {

0 commit comments

Comments
 (0)