Skip to content

Commit

Permalink
Add test to validate @nonreactive works with masking
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 23, 2024
1 parent 373d77a commit 4d1f29c
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/__tests__/dataMasking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,120 @@ test("triggers update to child watched fragment when updating field in named fra
});
});

test("does not trigger update to watched fragments when updating field in named fragment with @nonreactive", async () => {
interface UserFieldsFragment {
__typename: "User";
id: number;
lastUpdatedAt: string;
}

interface ProfileFieldsFragment {
__typename: "User";
lastUpdatedAt: string;
}

const profileFieldsFragment: TypedDocumentNode<ProfileFieldsFragment, never> =
gql`
fragment ProfileFields on User {
age
lastUpdatedAt @nonreactive
}
`;

const userFieldsFragment: TypedDocumentNode<UserFieldsFragment, never> = gql`
fragment UserFields on User {
id
lastUpdatedAt @nonreactive
...ProfileFields
}
${profileFieldsFragment}
`;

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

client.writeFragment({
fragment: userFieldsFragment,
fragmentName: "UserFields",
data: {
__typename: "User",
id: 1,
lastUpdatedAt: "2024-01-01",
// @ts-ignore TODO: Determine how to handle cache writes with masking
age: 30,
},
});

const userFieldsObservable = client.watchFragment({
fragment: userFieldsFragment,
fragmentName: "UserFields",
from: { __typename: "User", id: 1 },
});

const profileFieldsObservable = client.watchFragment({
fragment: profileFieldsFragment,
from: { __typename: "User", id: 1 },
});

const userFieldsStream = new ObservableStream(userFieldsObservable);
const profileFieldsStream = new ObservableStream(profileFieldsObservable);

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

expect(data).toEqual({
__typename: "User",
id: 1,
lastUpdatedAt: "2024-01-01",
});
}

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

expect(data).toEqual({
__typename: "User",
age: 30,
lastUpdatedAt: "2024-01-01",
});
}

client.writeFragment({
fragment: userFieldsFragment,
fragmentName: "UserFields",
data: {
__typename: "User",
id: 1,
lastUpdatedAt: "2024-01-02",
// @ts-ignore TODO: Determine how to handle cache writes with masking
age: 30,
},
});

await expect(userFieldsStream.takeNext()).rejects.toThrow(
new Error("Timeout waiting for next event")
);
await expect(profileFieldsStream.takeNext()).rejects.toThrow(
new Error("Timeout waiting for next event")
);

expect(
client.readFragment({
fragment: userFieldsFragment,
fragmentName: "UserFields",
id: "User:1",
})
).toEqual({
__typename: "User",
id: 1,
lastUpdatedAt: "2024-01-02",
age: 30,
});
});

test("warns when accessing an unmasked field on a watched fragment while using @unmask with mode: 'migrate'", async () => {
using consoleSpy = spyOnConsole("warn");

Expand Down

0 comments on commit 4d1f29c

Please sign in to comment.