-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
useRenderGuard
more resilient to changes in React internals. (#…
…11659) * Make `useRenderGuard` more resilient to changes in React internals. * uhhh... confusing, but okay
- Loading branch information
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Make `useRenderGuard` more resilient to changes in React internals. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"dist/apollo-client.min.cjs": 39245, | ||
"dist/apollo-client.min.cjs": 39247, | ||
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32630 | ||
} |
76 changes: 76 additions & 0 deletions
76
src/react/hooks/internal/__tests__/useRenderGuard.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable testing-library/render-result-naming-convention */ | ||
import React, { useEffect } from "react"; | ||
import { useRenderGuard } from "../useRenderGuard"; | ||
import { render, waitFor } from "@testing-library/react"; | ||
import { withCleanup } from "../../../../testing/internal"; | ||
|
||
const UNDEF = {}; | ||
|
||
it("returns a function that returns `true` if called during render", () => { | ||
let result: boolean | typeof UNDEF = UNDEF; | ||
function TestComponent() { | ||
const calledDuringRender = useRenderGuard(); | ||
result = calledDuringRender(); | ||
return <>Test</>; | ||
} | ||
render(<TestComponent />); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("returns a function that returns `false` if called after render", async () => { | ||
let result: boolean | typeof UNDEF = UNDEF; | ||
function TestComponent() { | ||
const calledDuringRender = useRenderGuard(); | ||
useEffect(() => { | ||
result = calledDuringRender(); | ||
}); | ||
return <>Test</>; | ||
} | ||
render(<TestComponent />); | ||
await waitFor(() => { | ||
expect(result).not.toBe(UNDEF); | ||
}); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
function breakReactInternalsTemporarily() { | ||
const R = React as unknown as { | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: any; | ||
}; | ||
const orig = R.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; | ||
|
||
R.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = {}; | ||
return withCleanup({}, () => { | ||
R.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = orig; | ||
}); | ||
} | ||
|
||
it("results in false negatives if React internals change", () => { | ||
let result: boolean | typeof UNDEF = UNDEF; | ||
function TestComponent() { | ||
using _ = breakReactInternalsTemporarily(); | ||
const calledDuringRender = useRenderGuard(); | ||
result = calledDuringRender(); | ||
return <>Test</>; | ||
} | ||
render(<TestComponent />); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("does not result in false positives if React internals change", async () => { | ||
let result: boolean | typeof UNDEF = UNDEF; | ||
function TestComponent() { | ||
using _ = breakReactInternalsTemporarily(); | ||
const calledDuringRender = useRenderGuard(); | ||
useEffect(() => { | ||
using _ = breakReactInternalsTemporarily(); | ||
result = calledDuringRender(); | ||
}); | ||
return <>Test</>; | ||
} | ||
render(<TestComponent />); | ||
await waitFor(() => { | ||
expect(result).not.toBe(UNDEF); | ||
}); | ||
expect(result).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters