Skip to content

Commit

Permalink
Add data masking tests to useSubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 27, 2024
1 parent 72b174a commit 88bc5cd
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions src/react/hooks/__tests__/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,144 @@ describe("ignoreResults", () => {
});
});

describe("data masking", () => {
test("masks data returned 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 ProfiledHook = profileHook(() => useSubscription(subscription));

render(<ProfiledHook />, {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
});

{
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();
}

await expect(ProfiledHook).not.toRerender();
});

test("does not mask data returned from subscriptions when dataMasking is `false`", async () => {
const subscription = gql`
subscription NewCommentSubscription {
addedComment {
id
...CommentFields
}
}
fragment CommentFields on Comment {
comment
author
}
`;

const link = new MockSubscriptionLink();
const client = new ApolloClient({
dataMasking: false,
cache: new Cache(),
link,
});

const ProfiledHook = profileHook(() => useSubscription(subscription));

render(<ProfiledHook />, {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
});

{
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,
comment: "Test comment",
author: "Test User",
},
});
expect(error).toBeUndefined();
}

await expect(ProfiledHook).not.toRerender();
});
});

describe.skip("Type Tests", () => {
test("NoInfer prevents adding arbitrary additional variables", () => {
const typedNode = {} as TypedDocumentNode<{ foo: string }, { bar: number }>;
Expand Down

0 comments on commit 88bc5cd

Please sign in to comment.