Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update data masking tests in React hooks to account for changes in render stream library #12166

Merged
merged 11 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/react/hooks/__tests__/useBackgroundQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4219,7 +4219,8 @@ it("masks queries when dataMasking is `true`", async () => {
);
}

renderStream.render(<App />, {
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, {
wrapper: createClientWrapper(client),
});

Expand Down Expand Up @@ -4307,7 +4308,8 @@ it("does not mask query when dataMasking is `false`", async () => {
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

// loading
await renderStream.takeRender();
Expand Down Expand Up @@ -4391,7 +4393,8 @@ it("does not mask query by default", async () => {
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

// loading
await renderStream.takeRender();
Expand Down Expand Up @@ -4476,7 +4479,8 @@ it("masks queries updated by the cache", async () => {
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

// loading
await renderStream.takeRender();
Expand Down Expand Up @@ -4592,7 +4596,8 @@ it("does not rerender when updating field in named fragment", async () => {
);
}

renderStream.render(<App />, {
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, {
wrapper: createClientWrapper(client),
});

Expand Down Expand Up @@ -4719,7 +4724,8 @@ it("masks result from cache when using with cache-first fetch policy", async ()
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

const { snapshot } = await renderStream.takeRender();

Expand Down Expand Up @@ -4815,7 +4821,8 @@ it("masks cache and network result when using cache-and-network fetch policy", a
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

{
const { snapshot } = await renderStream.takeRender();
Expand Down Expand Up @@ -4931,7 +4938,8 @@ it("masks partial cache data when returnPartialData is `true`", async () => {
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

{
const { snapshot } = await renderStream.takeRender();
Expand Down Expand Up @@ -5031,7 +5039,8 @@ it("masks partial data returned from data on errors with errorPolicy `all`", asy
);
}

renderStream.render(<App />, { wrapper: createClientWrapper(client) });
using _disabledAct = disableActEnvironment();
await renderStream.render(<App />, { wrapper: createClientWrapper(client) });

// loading
await renderStream.takeRender();
Expand Down
29 changes: 17 additions & 12 deletions src/react/hooks/__tests__/useFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,8 @@ describe("useFragment", () => {

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

const { takeSnapshot } = renderHookToSnapshotStream(
using _disabledAct = disableActEnvironment();
const { takeSnapshot } = await renderHookToSnapshotStream(
() => useFragment({ fragment, from: { __typename: "User" } }),
{
wrapper: ({ children }) => (
Expand All @@ -1634,19 +1635,19 @@ describe("useFragment", () => {
}
);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
"Could not identify object passed to `from` for '%s' fragment, either because the object is non-normalized or the key fields are missing. If you are masking this object, please ensure the key fields are requested by the parent object.",
"UserFields"
);

{
const { data, complete } = await takeSnapshot();

expect(data).toEqual({});
// TODO: Update when https://github.com/apollographql/apollo-client/issues/12003 is fixed
expect(complete).toBe(true);
}

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
"Could not identify object passed to `from` for '%s' fragment, either because the object is non-normalized or the key fields are missing. If you are masking this object, please ensure the key fields are requested by the parent object.",
"UserFields"
);
});

it("allows `null` as valid `from` value without warning", async () => {
Expand All @@ -1664,7 +1665,8 @@ describe("useFragment", () => {

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

const { takeSnapshot } = renderHookToSnapshotStream(
using _disabledAct = disableActEnvironment();
const { takeSnapshot } = await renderHookToSnapshotStream(
() => useFragment({ fragment, from: null }),
{
wrapper: ({ children }) => (
Expand Down Expand Up @@ -1711,7 +1713,8 @@ describe("useFragment", () => {
},
});

const { takeSnapshot, rerender } = renderHookToSnapshotStream(
using _disabledAct = disableActEnvironment();
const { takeSnapshot, rerender } = await renderHookToSnapshotStream(
({ from }) => useFragment({ fragment, from }),
{
initialProps: { from: null as UseFragmentOptions<any, never>["from"] },
Expand All @@ -1728,7 +1731,7 @@ describe("useFragment", () => {
expect(complete).toBe(false);
}

rerender({ from: { __typename: "User", id: "1" } });
await rerender({ from: { __typename: "User", id: "1" } });

{
const { data, complete } = await takeSnapshot();
Expand Down Expand Up @@ -1972,7 +1975,8 @@ describe("data masking", () => {
},
});

const { takeSnapshot } = renderHookToSnapshotStream(
using _disabledAct = disableActEnvironment();
const { takeSnapshot } = await renderHookToSnapshotStream(
() =>
useFragment({
fragment,
Expand Down Expand Up @@ -2089,7 +2093,8 @@ describe("data masking", () => {
return null;
}

renderStream.render(<Parent />, {
using _disabledAct = disableActEnvironment();
await renderStream.render(<Parent />, {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
Expand Down
50 changes: 23 additions & 27 deletions src/react/hooks/__tests__/useLazyQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2075,14 +2075,13 @@ describe("useLazyQuery Hook", () => {
link: new MockLink(mocks),
});

const { takeSnapshot, getCurrentSnapshot } = renderHookToSnapshotStream(
() => useLazyQuery(query),
{
using _disabledAct = disableActEnvironment();
const { takeSnapshot, getCurrentSnapshot } =
await renderHookToSnapshotStream(() => useLazyQuery(query), {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
}
);
});

// initial render
await takeSnapshot();
Expand Down Expand Up @@ -2164,14 +2163,13 @@ describe("useLazyQuery Hook", () => {
link: new MockLink(mocks),
});

const { takeSnapshot, getCurrentSnapshot } = renderHookToSnapshotStream(
() => useLazyQuery(query),
{
using _disabledAct = disableActEnvironment();
const { takeSnapshot, getCurrentSnapshot } =
await renderHookToSnapshotStream(() => useLazyQuery(query), {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
}
);
});

// initial render
await takeSnapshot();
Expand Down Expand Up @@ -2254,14 +2252,13 @@ describe("useLazyQuery Hook", () => {
link: new MockLink(mocks),
});

const { takeSnapshot, getCurrentSnapshot } = renderHookToSnapshotStream(
() => useLazyQuery(query),
{
using _disabledAct = disableActEnvironment();
const { takeSnapshot, getCurrentSnapshot } =
await renderHookToSnapshotStream(() => useLazyQuery(query), {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
}
);
});

// initial render
await takeSnapshot();
Expand Down Expand Up @@ -2345,20 +2342,19 @@ describe("useLazyQuery Hook", () => {
link: new MockLink(mocks),
});

const { takeSnapshot, getCurrentSnapshot } = renderHookToSnapshotStream(
() => useLazyQuery(query),
{
using _disabledAct = disableActEnvironment();
const { takeSnapshot, getCurrentSnapshot } =
await renderHookToSnapshotStream(() => useLazyQuery(query), {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
}
);
});

// initial render
await takeSnapshot();

const [execute] = getCurrentSnapshot();
execute();
await execute();

// Loading
await takeSnapshot();
Expand Down Expand Up @@ -2444,6 +2440,7 @@ describe("useLazyQuery Hook", () => {
},
},
},
delay: 20,
},
];

Expand All @@ -2453,20 +2450,19 @@ describe("useLazyQuery Hook", () => {
link: new MockLink(mocks),
});

const { takeSnapshot, getCurrentSnapshot } = renderHookToSnapshotStream(
() => useLazyQuery(query),
{
using _disabledAct = disableActEnvironment();
const { takeSnapshot, getCurrentSnapshot } =
await renderHookToSnapshotStream(() => useLazyQuery(query), {
wrapper: ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
),
}
);
});

// initial render
await takeSnapshot();

const [execute] = getCurrentSnapshot();
execute();
await execute();

// Loading
await takeSnapshot();
Expand Down
Loading
Loading