Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nova-react-test-utils): throw error from apollo #112

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "showcase error behavior",
"packageName": "@nova/examples",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "throw error from apollo in decorator",
"packageName": "@nova/react-test-utils",
"email": "[email protected]",
"dependentChangeType": "patch"
}
3 changes: 1 addition & 2 deletions packages/examples/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ export default {
...config,
transform: {
"\\.(gql|graphql)$": "@graphql-tools/jest-transform",
// Needed for packages/examples to load relay compiler generated artifacts.
// It would be better if it could be config local to examples package but haven't found a way to configure it with just
// Needed to load relay compiler generated artifacts.
".+[\\\\/]relay[\\\\/].+\\.tsx?$":
"@graphitation/embedded-document-artefact-loader/ts-jest",
"^.+\\.tsx?$": [
Expand Down
22 changes: 21 additions & 1 deletion packages/examples/src/apollo/Feedback/Feedback.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { graphql } from "@nova/react";
import {
EventingProvider,
getNovaDecorator,
getNovaEnvironmentForStory,
type WithNovaEnvironment,
type WithoutFragmentRefs,
} from "@nova/react-test-utils/apollo";
import type { Meta, StoryObj } from "@storybook/react";
import { expect, userEvent, within } from "@storybook/test";
import { expect, userEvent, waitFor, within } from "@storybook/test";
import { getSchema } from "../../testing-utils/getSchema";
import type { TypeMap } from "../../__generated__/schema.all.interface";
import { FeedbackComponent, Feedback_feedbackFragment } from "./Feedback";
Expand Down Expand Up @@ -84,6 +85,25 @@ export const Like: Story = {
},
};

export const ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError: Story =
{
parameters: {
novaEnvironment: {
enableQueuedMockResolvers: false,
},
} satisfies WithNovaEnvironment<FeedbackStoryQuery, TypeMap>,
play: async (context) => {
const {
graphql: { mock },
} = getNovaEnvironmentForStory(context);
await waitFor(async () => {
const operation = mock.getMostRecentOperation();
await expect(operation).toBeDefined();
});
await mock.rejectMostRecentOperation(new Error("Query failed"));
},
};

const FeedbackWithDeleteDialog = (props: Story["args"]) => {
const [open, setOpen] = React.useState(false);
const [text, setText] = React.useState("");
Expand Down
28 changes: 28 additions & 0 deletions packages/examples/src/apollo/Feedback/Feedback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { composeStories } from "@storybook/react";
import * as stories from "./Feedback.stories";
import { render } from "@testing-library/react";
import * as React from "react";
import "@testing-library/jest-dom";
import { prepareStoryContextForTest } from "@nova/react-test-utils/apollo";
import { executePlayFunction } from "../../testing-utils/executePlayFunction";

const { ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError } =
composeStories(stories);

describe("Feedback", () => {
it("throws an error when the developer makes a mistake", async () => {
const { container } = render(
<ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError />,
);
const context = prepareStoryContextForTest(
ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError,
container,
);
expect(async () => {
await executePlayFunction(
ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError,
context,
);
}).rejects.toThrowError("Query failed");
});
});
18 changes: 18 additions & 0 deletions packages/examples/src/apollo/Feedback/FeedbackContainer.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ export const LikeFailure: Story = {
},
};

export const QueryFailure: Story = {
parameters: {
novaEnvironment: {
enableQueuedMockResolvers: false,
},
} satisfies WithNovaEnvironment<UnknownOperation, TypeMap>,
play: async (context) => {
const {
graphql: { mock },
} = getNovaEnvironmentForStory(context);
await waitFor(async () => {
const operation = mock.getMostRecentOperation();
await expect(operation).toBeDefined();
});
await mock.rejectMostRecentOperation(new Error("Query failed"));
},
};

export const Loading: Story = {
parameters: {
novaEnvironment: {
Expand Down
19 changes: 19 additions & 0 deletions packages/examples/src/relay/Feedback/Feedback.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ export const Like: Story = {
},
};

export const ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError: Story =
{
parameters: {
novaEnvironment: {
enableQueuedMockResolvers: false,
},
} satisfies WithNovaEnvironment<FeedbackStoryQuery, TypeMap>,
play: async (context) => {
const {
graphql: { mock },
} = getNovaEnvironmentForStory(context);
await waitFor(async () => {
const operation = mock.getMostRecentOperation();
await expect(operation).toBeDefined();
});
await mock.rejectMostRecentOperation(new Error("Query failed"));
},
};

const FeedbackWithDeleteDialog = (props: Story["args"]) => {
const [open, setOpen] = React.useState(false);
const [text, setText] = React.useState("");
Expand Down
28 changes: 28 additions & 0 deletions packages/examples/src/relay/Feedback/Feedback.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { composeStories } from "@storybook/react";
import * as stories from "./Feedback.stories";
import { render } from "@testing-library/react";
import * as React from "react";
import "@testing-library/jest-dom";
import { executePlayFunction } from "../../testing-utils/executePlayFunction";
import { prepareStoryContextForTest } from "@nova/react-test-utils/relay";

const { ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError } =
composeStories(stories);

describe("Feedback", () => {
it("throws an error when the developer makes a mistake", async () => {
const { container } = render(
<ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError />,
);
const context = prepareStoryContextForTest(
ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError,
container,
);
expect(async () => {
await executePlayFunction(
ArtificialFailureToShowcaseDecoratorBehaviorInCaseOfADevCausedError,
context,
);
}).rejects.toThrowError("Query failed");
});
});
18 changes: 18 additions & 0 deletions packages/examples/src/relay/Feedback/FeedbackContainer.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ export const LikeFailure: Story = {
},
};

export const QueryFailure: Story = {
parameters: {
novaEnvironment: {
enableQueuedMockResolvers: false,
},
} satisfies WithNovaEnvironment<UnknownOperation, TypeMap>,
play: async (context) => {
const {
graphql: { mock },
} = getNovaEnvironmentForStory(context);
await waitFor(async () => {
const operation = mock.getMostRecentOperation();
await expect(operation).toBeDefined();
});
await mock.rejectMostRecentOperation(new Error("Query failed"));
},
};

export const Loading: Story = {
parameters: {
novaEnvironment: {
Expand Down
3 changes: 0 additions & 3 deletions packages/nova-react-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@
"@storybook/types": "^7.6.19",
"@testing-library/react": "^15.0.0",
"@types/jest": "^29.2.0",
"@types/react": "^18.3.1",
"@types/react-relay": "^16.0.0",
"@types/relay-runtime": "^17.0.0",
"@types/relay-test-utils": "^17.0.0",
"@types/react-test-renderer": "^18.3.0",
"monorepo-scripts": "*",
"react": "^18.3.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ export function getRenderer(
): React.FC<React.PropsWithChildren<unknown>> {
if (query) {
const Renderer: React.FC<unknown> = () => {
const { data } = useLazyLoadQuery(
const { data, error } = useLazyLoadQuery(
// There are no consequences of the cast, we do it only to make sure pure relay components can also leverage the decorator
query as GraphQLTaggedNode,
variables,
);

// apollo does not suspend, but returns undefined data
if (!data) {
if (error) {
// apollo returns an error, while Relay throws, let's align the behavior
throw error;
}
return <div>Loading...</div>;
}

Expand All @@ -100,7 +104,7 @@ export function getRenderer(
Object.assign(context.args, Object.fromEntries(entries));
return <>{getStory(context)}</>;
};
return Renderer;
return () => <Renderer />;
} else {
return () => {
return <>{getStory(context)}</>;
Expand Down
Loading