From 85b9d00d754f35ed7d60b455f1159685a74a504e Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Sun, 8 Oct 2023 23:19:35 +0200 Subject: [PATCH] add failing test case for subscriptions with @client directive --- .../__tests__/client/Subscription.test.tsx | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/react/components/__tests__/client/Subscription.test.tsx b/src/react/components/__tests__/client/Subscription.test.tsx index 4584913a30d..486ad6241a3 100644 --- a/src/react/components/__tests__/client/Subscription.test.tsx +++ b/src/react/components/__tests__/client/Subscription.test.tsx @@ -38,6 +38,85 @@ const client = new ApolloClient({ cache, }); +itAsync( + "executes the subscription with @client directive data", + (resolve, reject) => { + let renderCount = 0; + + const cacheWithLocal = new Cache({ + typePolicies: { + User: { + fields: { + test: { + read() { + return "asdf"; + }, + }, + }, + }, + }, + }); + + const clientWithLocal = new ApolloClient({ + link, + cache: cacheWithLocal, + }); + + const withLocalResults = ["Luke Skywalker", "Han Solo"].map((name) => ({ + result: { data: { user: { __typename: "User", name, test: "asdf" } } }, + })); + + const subscriptionWithLocal = gql` + subscription UserInfo { + user { + name + test @client + } + } + `; + + const Component = () => ( + + {(result: any) => { + const { loading, data, error } = result; + switch (renderCount) { + case 0: + expect(loading).toBe(true); + expect(error).toBeUndefined(); + expect(data).toBeUndefined(); + break; + case 1: + expect(loading).toBe(false); + expect(data).toEqual(withLocalResults[0].result.data); + break; + case 2: + expect(loading).toBe(false); + expect(data).toEqual(withLocalResults[1].result.data); + break; + default: + } + + setTimeout(() => { + renderCount <= withLocalResults.length && + link.simulateResult(results[renderCount - 1]); + }); + + renderCount += 1; + return null; + }} + + ); + + render( + + + + ); + + waitFor(() => expect(renderCount).toBe(3)).then(resolve, reject); + } +); + itAsync("executes the subscription", (resolve, reject) => { let renderCount = 0; const Component = () => (