diff --git a/config/jest.config.js b/config/jest.config.js index 1e0d714b4ac..29323d3da7d 100644 --- a/config/jest.config.js +++ b/config/jest.config.js @@ -29,7 +29,10 @@ const defaults = { const ignoreTSFiles = ".ts$"; const ignoreTSXFiles = ".tsx$"; -const react19TestFileIgnoreList = [ignoreTSFiles]; +const react19TestFileIgnoreList = [ + ignoreTSFiles, + "src/react/ssr/__tests__/getDataFromTree.test.tsx", +]; const react17TestFileIgnoreList = [ ignoreTSFiles, diff --git a/src/react/ssr/__tests__/getDataFromTree.test.tsx b/src/react/ssr/__tests__/getDataFromTree.test.tsx new file mode 100644 index 00000000000..54bc53b4ac3 --- /dev/null +++ b/src/react/ssr/__tests__/getDataFromTree.test.tsx @@ -0,0 +1,128 @@ +import React from "react"; +import gql from "graphql-tag"; +import { DocumentNode } from "graphql"; + +import { ApolloClient, TypedDocumentNode } from "../../../core"; +import { InMemoryCache as Cache } from "../../../cache"; +import { getDataFromTree } from "../getDataFromTree"; +import { mockSingleLink } from "../../../testing"; +import { useQuery } from "../../hooks"; +import { ApolloProvider, getApolloContext } from "../../context"; + +describe("SSR", () => { + describe("`getDataFromTree`", () => { + it("should support passing a root context", async () => { + type CustomContext = { text: string }; + const ApolloContext = getApolloContext(); + + function App() { + return ( + + {(context) =>
{(context as CustomContext).text}
} +
+ ); + } + + const html = await getDataFromTree(, { + text: "oyez", + }); + + expect(html).toEqual("
oyez
"); + }); + + it("should run through all of the queries (also defined via Query component) that want SSR", async () => { + const query: TypedDocumentNode = gql` + { + currentUser { + firstName + } + } + `; + const data1 = { currentUser: { firstName: "James" } }; + const link = mockSingleLink({ + request: { query }, + result: { data: data1 }, + delay: 50, + }); + const apolloClient = new ApolloClient({ + link, + cache: new Cache({ addTypename: false }), + }); + + interface Data { + currentUser?: { + firstName: string; + }; + } + + function App() { + const { data, loading } = useQuery(query); + + return ( +
+ {loading || !data ? "loading" : data.currentUser!.firstName} +
+ ); + } + + const app = ( + + + + ); + + const markup = await getDataFromTree(app); + + expect(markup).toMatch(/James/); + }); + + it('should pass any GraphQL errors in props along with data during a SSR when errorPolicy="all"', async () => { + expect.assertions(3); + const query: DocumentNode = gql` + query people { + allPeople { + people { + name + } + } + } + `; + const link = mockSingleLink({ + request: { query }, + result: { + data: { + allPeople: { + people: null, + }, + }, + errors: [new Error("this is an error")], + }, + }); + + const client = new ApolloClient({ + link, + cache: new Cache({ addTypename: false }), + }); + + function App() { + const { data, loading, error } = useQuery(query, { + errorPolicy: "all", + }); + + if (!loading) { + expect(data).toMatchObject({ allPeople: { people: null } }); + expect(error).toBeDefined(); + expect(error?.graphQLErrors[0].message).toEqual("this is an error"); + } + + return null; + } + + await getDataFromTree( + + + + ); + }); + }); +});