Skip to content

Commit

Permalink
Add additional tests for warnings with no-cache on client.subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Nov 13, 2024
1 parent 5a3b9e5 commit d3aad6d
Showing 1 changed file with 178 additions and 0 deletions.
178 changes: 178 additions & 0 deletions src/__tests__/dataMasking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4518,6 +4518,184 @@ describe("client.subscribe", () => {
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(NO_CACHE_WARNING);
});

test("does not warn on no-cache queries when data masking is disabled", async () => {
using _ = spyOnConsole("warn");
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 InMemoryCache(),
link,
});

const observable = client.subscribe({
query: subscription,
fetchPolicy: "no-cache",
});
const stream = new ObservableStream(observable);

link.simulateResult({
result: {
data: {
addedComment: {
__typename: "Comment",
id: 1,
comment: "Test comment",
author: "Test User",
},
},
},
});

const { data } = await stream.takeNext();

expect(data).toEqual({
addedComment: {
__typename: "Comment",
id: 1,
comment: "Test comment",
author: "Test User",
},
});

expect(console.warn).not.toHaveBeenCalled();
});

test("does not warn on no-cache queries when all fragments use `@unmask`", async () => {
using _ = spyOnConsole("warn");
const subscription = gql`
subscription NewCommentSubscription {
addedComment {
id
...CommentFields @unmask
}
}
fragment CommentFields on Comment {
comment
author
}
`;

const link = new MockSubscriptionLink();

const client = new ApolloClient({
dataMasking: true,
cache: new InMemoryCache(),
link,
});

const observable = client.subscribe({
query: subscription,
fetchPolicy: "no-cache",
});
const stream = new ObservableStream(observable);

link.simulateResult({
result: {
data: {
addedComment: {
__typename: "Comment",
id: 1,
comment: "Test comment",
author: "Test User",
},
},
},
});

const { data } = await stream.takeNext();

expect(data).toEqual({
addedComment: {
__typename: "Comment",
id: 1,
comment: "Test comment",
author: "Test User",
},
});

expect(console.warn).not.toHaveBeenCalled();
});

test("warns on no-cache queries when at least one fragment does not use `@unmask`", async () => {
using _ = spyOnConsole("warn");
const subscription = gql`
subscription NewCommentSubscription {
addedComment {
id
...CommentFields @unmask
}
}
fragment CommentFields on Comment {
comment
author {
...AuthorFields
}
}
fragment AuthorFields on User {
name
}
`;

const link = new MockSubscriptionLink();

const client = new ApolloClient({
dataMasking: true,
cache: new InMemoryCache(),
link,
});

const observable = client.subscribe({
query: subscription,
fetchPolicy: "no-cache",
});
const stream = new ObservableStream(observable);

link.simulateResult({
result: {
data: {
addedComment: {
__typename: "Comment",
id: 1,
comment: "Test comment",
author: { __typename: "User", name: "Test User" },
},
},
},
});

const { data } = await stream.takeNext();

expect(data).toEqual({
addedComment: {
__typename: "Comment",
id: 1,
comment: "Test comment",
author: { __typename: "User" },
},
});

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(NO_CACHE_WARNING);
});
});

describe("observableQuery.subscribeToMore", () => {
Expand Down

0 comments on commit d3aad6d

Please sign in to comment.