diff --git a/src/testing/core/__tests__/createProxiedSchema.test.tsx b/src/testing/core/__tests__/createProxiedSchema.test.tsx index b10b0121f0e..7128b69825b 100644 --- a/src/testing/core/__tests__/createProxiedSchema.test.tsx +++ b/src/testing/core/__tests__/createProxiedSchema.test.tsx @@ -861,7 +861,6 @@ describe("schema proxy", () => { }, Book: { __resolveType: (obj) => { - console.log(obj); if ("text" in obj) { return "TextBook"; } @@ -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(); + + 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 = gql` + query { + viewer { + id + name + age + book { + id + title + publishedAt + ... on ColoringBook { + colors + } + } + } + } + `; + + const Fallback = () => { + useTrackRenders(); + return
Loading...
; + }; + + const App = () => { + return ( + }> + + + ); + }; + + const Child = () => { + const result = useSuspenseQuery(query); + + useTrackRenders(); + + Profiler.mergeSnapshot({ + result, + } as Partial<{}>); + + return
Hello
; + }; + + const { unmount } = renderWithClient(, { + 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(); + + resetTestSchema.reset(); + + using _fetch = createMockFetch(resetTestSchema); + + const client = new ApolloClient({ + cache: new InMemoryCache(), + uri, + }); + + const query: TypedDocumentNode = gql` + query { + viewer { + id + name + age + book { + id + title + publishedAt + ... on ColoringBook { + colors + } + } + } + } + `; + + const Fallback = () => { + useTrackRenders(); + return
Loading...
; + }; + + const App = () => { + return ( + }> + + + ); + }; + + const Child = () => { + const result = useSuspenseQuery(query); + + useTrackRenders(); + + Profiler.mergeSnapshot({ + result, + } as Partial<{}>); + + return
Hello
; + }; + + const { unmount } = renderWithClient(, { + 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(); + }); + }); }); diff --git a/src/testing/core/createProxiedSchema.ts b/src/testing/core/createProxiedSchema.ts index 092a7eb6b1f..26b792532d7 100644 --- a/src/testing/core/createProxiedSchema.ts +++ b/src/testing/core/createProxiedSchema.ts @@ -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 @@ -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({ @@ -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, @@ -77,11 +67,7 @@ const createProxiedSchema = ( }, fork: ({ resolvers: newResolvers } = {}) => { - return createProxiedSchema( - targetSchema, - newResolvers ?? targetResolvers, - options - ); + return createProxiedSchema(targetSchema, newResolvers ?? targetResolvers); }, reset: () => {