Skip to content

Commit

Permalink
chore: add reset test, remove option
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Apr 10, 2024
1 parent 12a5f36 commit 0fb94cc
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 21 deletions.
209 changes: 205 additions & 4 deletions src/testing/core/__tests__/createProxiedSchema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,6 @@ describe("schema proxy", () => {
},
Book: {
__resolveType: (obj) => {
console.log(obj);
if ("text" in obj) {
return "TextBook";
}
Expand Down Expand Up @@ -1028,7 +1027,209 @@ describe("schema proxy", () => {

unmount();
});
it.todo("add test for reset");
it.todo("add test for mergeResolvers: false");
it.todo("add test for mergeResolvers: true");

describe("schema.reset", () => {
const resetTestSchema = createProxiedSchema(schemaWithMocks, {
Query: {
viewer: () => ({
book: {
text: "Hello World",
title: "Orlando: A Biography",
},
}),
},
Book: {
__resolveType: (obj) => {
if ("text" in obj) {
return "TextBook";
}
if ("colors" in obj) {
return "ColoringBook";
}
throw new Error("Could not resolve type");
},
},
});
it("setup test where we add resolvers to schema", async () => {
const Profiler = createDefaultProfiler<ViewerQueryData>();

resetTestSchema.add({
resolvers: {
Query: {
viewer: () => ({
book: {
text: "Hello World",
title: "The Waves",
},
}),
},
},
});

using _fetch = createMockFetch(resetTestSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
uri,
});

const query: TypedDocumentNode<ViewerQueryData> = gql`
query {
viewer {
id
name
age
book {
id
title
publishedAt
... on ColoringBook {
colors
}
}
}
}
`;

const Fallback = () => {
useTrackRenders();
return <div>Loading...</div>;
};

const App = () => {
return (
<React.Suspense fallback={<Fallback />}>
<Child />
</React.Suspense>
);
};

const Child = () => {
const result = useSuspenseQuery(query);

useTrackRenders();

Profiler.mergeSnapshot({
result,
} as Partial<{}>);

return <div>Hello</div>;
};

const { unmount } = renderWithClient(<App />, {
client,
wrapper: Profiler,
});

// initial suspended render
await Profiler.takeRender();

{
const { snapshot } = await Profiler.takeRender();

expect(snapshot.result?.data).toEqual({
viewer: {
__typename: "User",
age: 42,
id: "1",
name: "String",
book: {
__typename: "TextBook",
id: "1",
publishedAt: "2024-01-01",
// value set in this test with .add
title: "The Waves",
},
},
});
}

unmount();
});
it("resets the schema to the original", async () => {
const Profiler = createDefaultProfiler<ViewerQueryData>();

resetTestSchema.reset();

using _fetch = createMockFetch(resetTestSchema);

const client = new ApolloClient({
cache: new InMemoryCache(),
uri,
});

const query: TypedDocumentNode<ViewerQueryData> = gql`
query {
viewer {
id
name
age
book {
id
title
publishedAt
... on ColoringBook {
colors
}
}
}
}
`;

const Fallback = () => {
useTrackRenders();
return <div>Loading...</div>;
};

const App = () => {
return (
<React.Suspense fallback={<Fallback />}>
<Child />
</React.Suspense>
);
};

const Child = () => {
const result = useSuspenseQuery(query);

useTrackRenders();

Profiler.mergeSnapshot({
result,
} as Partial<{}>);

return <div>Hello</div>;
};

const { unmount } = renderWithClient(<App />, {
client,
wrapper: Profiler,
});

// initial suspended render
await Profiler.takeRender();

{
const { snapshot } = await Profiler.takeRender();

expect(snapshot.result?.data).toEqual({
viewer: {
__typename: "User",
age: 42,
id: "1",
name: "String",
book: {
__typename: "TextBook",
id: "1",
publishedAt: "2024-01-01",
// original value
title: "Orlando: A Biography",
},
},
});
}

unmount();
});
});
});
20 changes: 3 additions & 17 deletions src/testing/core/createProxiedSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ interface ProxiedSchemaFns {
reset: () => void;
}

type CreateProxiedSchemaOptions = {
mergeResolvers: boolean;
};

/**
* A function that creates a [Proxy object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
* around a given `schema` with `resolvers`. This proxied schema can be used to
Expand Down Expand Up @@ -49,10 +45,7 @@ type CreateProxiedSchemaOptions = {
*/
const createProxiedSchema = (
schemaWithMocks: GraphQLSchema,
resolvers: Resolvers,
options: CreateProxiedSchemaOptions = {
mergeResolvers: true,
}
resolvers: Resolvers
): ProxiedSchema => {
let targetResolvers = { ...resolvers };
let targetSchema = addResolversToSchema({
Expand All @@ -63,10 +56,7 @@ const createProxiedSchema = (
const fns: ProxiedSchemaFns = {
add: ({ resolvers: newResolvers }) => {
// @ts-ignore TODO(fixme): IResolvers type does not play well with our Resolvers
targetResolvers =
options.mergeResolvers ?
mergeResolvers([targetResolvers, newResolvers])
: { ...targetResolvers, ...newResolvers };
targetResolvers = mergeResolvers([targetResolvers, newResolvers]);

targetSchema = addResolversToSchema({
schema: targetSchema,
Expand All @@ -77,11 +67,7 @@ const createProxiedSchema = (
},

fork: ({ resolvers: newResolvers } = {}) => {
return createProxiedSchema(
targetSchema,
newResolvers ?? targetResolvers,
options
);
return createProxiedSchema(targetSchema, newResolvers ?? targetResolvers);
},

reset: () => {
Expand Down

0 comments on commit 0fb94cc

Please sign in to comment.