Skip to content

Commit a043653

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

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
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 cachedQuery = cache.build(queryClient, defaultedOptions);
102+
// Set manual data so react-query will not try to refetch.
103+
cachedQuery.setData(undefined, { manual: true });
104+
cachedQuery.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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ export const LoadingGroup: Story = {
5151
},
5252
};
5353

54-
// Will show a 404 because the query is not mocked.
5554
export const GroupError: Story = {
5655
parameters: {
57-
queries: [permissionsQuery({})],
56+
queries: [groupQuery(new Error("test group error")), permissionsQuery({})],
5857
},
5958
};
6059

@@ -86,12 +85,12 @@ export const EveryoneGroup: Story = {
8685
},
8786
};
8887

89-
// Will show a 404 because the query is not mocked.
9088
export const MembersError: Story = {
9189
parameters: {
9290
queries: [
9391
groupQuery(MockGroup),
9492
permissionsQuery({ canUpdateGroup: true }),
93+
membersQuery(new Error("test members error")),
9594
],
9695
},
9796
play: async ({ canvasElement }) => {

0 commit comments

Comments
 (0)