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): fix issue with store being shared between stories #118

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": "patch",
"comment": "use custom store is some stories",
"packageName": "@nova/examples",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "fix issue with store being shared between stories",
"packageName": "@nova/react-test-utils",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 3 additions & 1 deletion packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"graphql": "^15.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-relay": "^17.0.0"
"react-relay": "^17.0.0",
"relay-runtime": "^17.0.0"
},
"devDependencies": {
"@babel/core": "^7.20.2",
Expand Down Expand Up @@ -53,6 +54,7 @@
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"@types/react-relay": "^16.0.0",
"@types/relay-runtime": "^17.0.0",
"esbuild-loader": "^3.0.1",
"monorepo-scripts": "*",
"prop-types": "15.8.1",
Expand Down
9 changes: 8 additions & 1 deletion packages/examples/src/relay/Feedback/Feedback.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import type { FeedbackStoryQuery } from "./__generated__/FeedbackStoryQuery.grap
import { getSchema } from "../../testing-utils/getSchema";
import * as React from "react";
import type { events } from "../../events/events";
import { RecordSource, Store } from "relay-runtime";

const schema = getSchema();

const MockPayloadGenerator = new PayloadGenerator(schema);

const novaDecorator = getNovaDecorator(schema, {
getEnvironmentOptions: () => ({
store: new Store(new RecordSource()),
}),
});
sjwilczynski marked this conversation as resolved.
Show resolved Hide resolved

const meta = {
component: FeedbackComponent,
decorators: [getNovaDecorator(schema)],
decorators: [novaDecorator],
parameters: {
novaEnvironment: {
query: graphql`
Expand Down
9 changes: 8 additions & 1 deletion packages/examples/src/relay/pure-relay/Feedback.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ import type { FeedbackStoryRelayQuery } from "./__generated__/FeedbackStoryRelay
import { getSchema } from "../../testing-utils/getSchema";
import * as React from "react";
import type { events } from "../../events/events";
import { RecordSource, Store } from "relay-runtime";

const schema = getSchema();

const MockPayloadGenerator = new PayloadGenerator(schema);

const novaDecorator = getNovaDecorator(schema, {
getEnvironmentOptions: () => ({
store: new Store(new RecordSource()),
}),
});

const meta = {
component: FeedbackComponent,
decorators: [getNovaDecorator(schema)],
decorators: [novaDecorator],
parameters: {
novaEnvironment: {
query: graphql`
Expand Down
8 changes: 5 additions & 3 deletions packages/nova-react-test-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const meta = {
} satisfies Meta<typeof FeedbackContainer>;
```

#### I need to configure Relay store to support some custom setup I have in my repository. Is it possible?
#### I need to configure Relay environment to support some custom setup I have in my repository. Is it possible?

Similarly as with configuring Apollo cache, one can pass options to `createMockEnvironment`:

Expand All @@ -262,14 +262,16 @@ const environment = createMockEnvironment(schema, {
});
```

and if you are using through storybook decorator you can pass options to `getNovaDecorator`:
and if you are using through storybook decorator you can pass options to `getNovaDecorator`. However, in case of storybook environment we need to make sure that for example `store` is unique for each story, so we need to pass a function that will return the options:

```tsx
const meta = {
component: FeedbackContainer,
decorators: [
getNovaDecorator(schema, {
store: myCustomStoreConfig,
getEnvironmentOptions: () => ({
store: myCustomStoreConfig,
}),
}),
],
} satisfies Meta<typeof FeedbackContainer>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import { createMockEnvironment } from "relay-test-utils";
import type { GraphQLSchema } from "graphql";
import type { ReactRenderer } from "@storybook/react";
import type { PlayFunctionContext } from "@storybook/types";
import type { EnvironmentConfig } from "relay-runtime";

type RelayEnvironmentOptions = Parameters<typeof createMockEnvironment>[0];

type Options = RelayEnvironmentOptions & {
type Options = { getEnvironmentOptions?: () => Partial<EnvironmentConfig> } & {
generateFunction?: typeof RelayMockPayloadGenerator.prototype.generate;
};

export const getNovaRelayDecorator: (
schema: GraphQLSchema,
options?: Options,
) => MakeDecoratorResult = (schema, { generateFunction, ...rest } = {}) => {
const createEnvironment = () => createNovaRelayEnvironment(rest);
) => MakeDecoratorResult = (
schema,
{ generateFunction, getEnvironmentOptions } = {},
) => {
const createEnvironment = () =>
createNovaRelayEnvironment(getEnvironmentOptions?.());
const relayMockPayloadGenerator = new RelayMockPayloadGenerator(schema);
const initializeGenerator = (
parameters: WithNovaEnvironment["novaEnvironment"],
Expand All @@ -45,7 +48,7 @@ export const getNovaRelayDecorator: (
};

function createNovaRelayEnvironment(
options?: RelayEnvironmentOptions,
options?: Partial<EnvironmentConfig>,
): NovaMockEnvironment<"relay", "storybook"> {
const relayEnvironment = createMockEnvironment(options);
const env: NovaMockEnvironment<"relay", "storybook"> = {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3856,7 +3856,7 @@
"@types/prop-types" "*"
csstype "^3.0.2"

"@types/relay-runtime@*", "@types/relay-runtime@>16":
"@types/relay-runtime@*", "@types/relay-runtime@>16", "@types/relay-runtime@^17.0.0":
version "17.0.4"
resolved "https://registry.yarnpkg.com/@types/relay-runtime/-/relay-runtime-17.0.4.tgz#428526fc3e6dfb6e0a5730c38ad521cb1eea189b"
integrity sha512-fB77br4lXlBYM/HpI6VI6KCrj5pw0LiAnkZOkffjirNYso+dzXGWkeIm0G0MGszD8WY1et+r1Uj2TA6rscBXNQ==
Expand Down
Loading