diff --git a/src/react/hooks/__tests__/useSubscription.test.tsx b/src/react/hooks/__tests__/useSubscription.test.tsx index f143bc8561e..3da19628b3f 100644 --- a/src/react/hooks/__tests__/useSubscription.test.tsx +++ b/src/react/hooks/__tests__/useSubscription.test.tsx @@ -2196,6 +2196,87 @@ describe("data masking", () => { await expect(ProfiledHook).not.toRerender(); }); + + test("masks data passed to onData callback when dataMasking is `true`", async () => { + const subscription = gql` + subscription NewCommentSubscription { + addedComment { + id + ...CommentFields + } + } + + fragment CommentFields on Comment { + comment + author + } + `; + + const link = new MockSubscriptionLink(); + const client = new ApolloClient({ + dataMasking: true, + cache: new Cache(), + link, + }); + + const onData = jest.fn(); + const ProfiledHook = profileHook(() => + useSubscription(subscription, { onData }) + ); + + render(, { + wrapper: ({ children }) => ( + {children} + ), + }); + + { + const { data, loading, error } = await ProfiledHook.takeSnapshot(); + + expect(loading).toBe(true); + expect(data).toBeUndefined(); + expect(error).toBeUndefined(); + } + + link.simulateResult({ + result: { + data: { + addedComment: { + __typename: "Comment", + id: 1, + comment: "Test comment", + author: "Test User", + }, + }, + }, + }); + + { + const { data, loading, error } = await ProfiledHook.takeSnapshot(); + + expect(loading).toBe(false); + expect(data).toEqual({ + addedComment: { + __typename: "Comment", + id: 1, + }, + }); + expect(error).toBeUndefined(); + + expect(onData).toHaveBeenCalledTimes(1); + expect(onData).toHaveBeenCalledWith({ + client: expect.anything(), + data: { + data: { addedComment: { __typename: "Comment", id: 1 } }, + loading: false, + error: undefined, + variables: undefined, + }, + }); + } + + await expect(ProfiledHook).not.toRerender(); + }); }); describe.skip("Type Tests", () => {