-
Notifications
You must be signed in to change notification settings - Fork 561
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
Add tests for '@reach/utils' and compatibility check with react 18 #960
Open
dartess
wants to merge
15
commits into
reach:dev
Choose a base branch
from
dartess:utils-tests
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ea06735
add utils for run tests of hooks; fix usage render from @reach-intern…
dartess fd852db
add tests for useConstant
dartess d58d027
add tests for useControlledState
dartess e62e04f
add tests for useEventListener
dartess 030ce69
add tests for useFocusChange and fix errors
dartess 7111180
add tests for useForceUpdate
dartess 51cac7f
add tests for useLazyRef
dartess 6ec6bec
add tests for usePrevious
dartess 40bd40b
add tests for useStableCallback and useStableLayoutCallback
dartess 79ef7dc
add tests for useStatefulRefValue
dartess 8931cd7
add tests for useUpdateEffect
dartess 3ccb63c
remove unused `only`
dartess 1ebec3e
adopt useUpdateEffect for react@18 StrictMode
dartess 81db498
adopt useStableCallback tests for react@18 StrictMode
dartess 9af9a61
add react@18 to peerDependencies of utils
dartess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,26 @@ | ||
import { afterEach, describe, expect, it } from "vitest"; | ||
import { renderHook, cleanupHooks } from "@reach-internal/test/utils"; | ||
import { useConstant } from "@reach/utils"; | ||
|
||
afterEach(cleanupHooks); | ||
|
||
describe("useConstant", () => { | ||
const renderUseConstant = () => | ||
renderHook(() => useConstant(() => ({ foo: "bar" }))); | ||
|
||
it("should return value from callback", () => { | ||
const render = renderUseConstant(); | ||
|
||
const firstRenderedObject = render.result.current; | ||
expect(firstRenderedObject).toEqual({ foo: "bar" }); | ||
}); | ||
|
||
it("should return the same value after rerender", () => { | ||
const render = renderUseConstant(); | ||
const resultFirst = render.result.current; | ||
render.rerender(); | ||
const resultSecond = render.result.current; | ||
|
||
expect(resultFirst).toBe(resultSecond); | ||
}); | ||
}); |
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,50 @@ | ||
import { afterEach, describe, expect, it } from "vitest"; | ||
import { renderHook, cleanupHooks, actHooks } from "@reach-internal/test/utils"; | ||
import { useControlledState } from "@reach/utils"; | ||
|
||
afterEach(cleanupHooks); | ||
|
||
describe("useControlledState", () => { | ||
const DEFAULT_VALUE = 10; | ||
const CONTROLLED_VALUE = 42; | ||
|
||
it("should return value and setter", () => { | ||
const { result } = renderHook(() => | ||
useControlledState({ | ||
defaultValue: DEFAULT_VALUE, | ||
controlledValue: undefined, | ||
}) | ||
); | ||
|
||
expect(result.current[0]).toBe(DEFAULT_VALUE); | ||
expect(typeof result.current[1]).toBe("function"); | ||
}); | ||
|
||
it("should work as uncontrolled", () => { | ||
const { result } = renderHook(() => | ||
useControlledState({ | ||
defaultValue: DEFAULT_VALUE, | ||
controlledValue: undefined, | ||
}) | ||
); | ||
expect(result.current[0]).toBe(DEFAULT_VALUE); | ||
actHooks(() => { | ||
result.current[1](17); | ||
}); | ||
expect(result.current[0]).toBe(17); | ||
}); | ||
|
||
it("should work as controlled", () => { | ||
const { result } = renderHook(() => | ||
useControlledState({ | ||
defaultValue: DEFAULT_VALUE, | ||
controlledValue: CONTROLLED_VALUE, | ||
}) | ||
); | ||
expect(result.current[0]).toBe(CONTROLLED_VALUE); | ||
actHooks(() => { | ||
result.current[1](17); | ||
}); | ||
expect(result.current[0]).toBe(CONTROLLED_VALUE); | ||
}); | ||
}); |
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,33 @@ | ||
import * as React from "react"; | ||
import { render, fireEvent, cleanup } from "@reach-internal/test/utils"; | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
import { useEventListener } from "@reach/utils"; | ||
|
||
afterEach(cleanup); | ||
|
||
describe("useEventListener", () => { | ||
const Test = ({ onBodyClick }: { onBodyClick: () => void }) => { | ||
useEventListener("click", onBodyClick, document.body); | ||
return null; | ||
}; | ||
|
||
it("should call event listener when it's need", () => { | ||
const handleBodyClick = vi.fn(); | ||
render(<Test onBodyClick={handleBodyClick} />); | ||
fireEvent.click(document.body); | ||
expect(handleBodyClick).toHaveBeenCalledTimes(1); | ||
fireEvent.click(document.body); | ||
expect(handleBodyClick).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should can change event listener from args", () => { | ||
const handleBodyClick1 = vi.fn(); | ||
const handleBodyClick2 = vi.fn(); | ||
const { rerender } = render(<Test onBodyClick={handleBodyClick1} />); | ||
fireEvent.click(document.body); | ||
rerender(<Test onBodyClick={handleBodyClick2} />); | ||
fireEvent.click(document.body); | ||
expect(handleBodyClick1).toHaveBeenCalledOnce(); | ||
expect(handleBodyClick2).toHaveBeenCalledOnce(); | ||
}); | ||
}); |
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,106 @@ | ||
import * as React from "react"; | ||
import { render, cleanup, userEvent } from "@reach-internal/test/utils"; | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
import { useFocusChange } from "@reach/utils"; | ||
|
||
afterEach(cleanup); | ||
|
||
describe("useFocusChange", () => { | ||
const Test = ({ | ||
onChange, | ||
when, | ||
}: { | ||
onChange: () => void; | ||
when?: "focus" | "blur"; | ||
}) => { | ||
useFocusChange(onChange, when); | ||
return ( | ||
<> | ||
<input type="text" placeholder="first" /> | ||
<input type="text" placeholder="second" /> | ||
<div>just div</div> | ||
</> | ||
); | ||
}; | ||
|
||
const renderTest = (when?: "focus" | "blur") => { | ||
const handleChange = vi.fn(); | ||
const { getByPlaceholderText, getByText } = render( | ||
<Test onChange={handleChange} when={when} /> | ||
); | ||
const firstInput = getByPlaceholderText("first"); | ||
const secondInput = getByPlaceholderText("second"); | ||
const div = getByText("just div"); | ||
return { | ||
firstInput, | ||
secondInput, | ||
div, | ||
handleChange, | ||
}; | ||
}; | ||
|
||
/** | ||
* WARNING: The order of the tests is important: | ||
* the blur test should come first. | ||
* If this is not the case, the activeElement will be dirty | ||
* and the blur event will fire when the input is clicked. | ||
*/ | ||
|
||
it("should call handler on blur", async () => { | ||
const { | ||
firstInput, | ||
secondInput, | ||
div, | ||
handleChange: handleBlur, | ||
} = renderTest("blur"); | ||
|
||
await userEvent.click(firstInput); | ||
expect(handleBlur).not.toHaveBeenCalled(); | ||
|
||
await userEvent.click(secondInput); | ||
expect(handleBlur).toHaveBeenCalledTimes(1); | ||
expect(handleBlur).toHaveBeenCalledWith( | ||
document.body, | ||
document.body, | ||
expect.any(FocusEvent) | ||
); | ||
|
||
await userEvent.click(div); | ||
expect(handleBlur).toHaveBeenCalledTimes(2); | ||
expect(handleBlur).toHaveBeenCalledWith( | ||
document.body, | ||
document.body, | ||
expect.any(FocusEvent) | ||
); | ||
}); | ||
|
||
it("should call handler on focus", async () => { | ||
const { firstInput, secondInput, handleChange: handleFocus } = renderTest(); | ||
|
||
await userEvent.click(firstInput); | ||
expect(handleFocus).toHaveBeenCalledTimes(1); | ||
expect(handleFocus).toHaveBeenCalledWith( | ||
firstInput, | ||
document.body, | ||
expect.any(FocusEvent) | ||
); | ||
|
||
await userEvent.click(secondInput); | ||
expect(handleFocus).toHaveBeenCalledTimes(2); | ||
expect(handleFocus).toHaveBeenCalledWith( | ||
secondInput, | ||
firstInput, | ||
expect.any(FocusEvent) | ||
); | ||
}); | ||
|
||
it("should do not call handler on focus at the same node", async () => { | ||
const { firstInput, handleChange: handleFocus } = renderTest(); | ||
|
||
await userEvent.click(firstInput); | ||
expect(handleFocus).toHaveBeenCalledOnce(); | ||
|
||
await userEvent.click(firstInput); | ||
expect(handleFocus).toHaveBeenCalledOnce(); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -22,20 +22,24 @@ export function useFocusChange( | |
lastActiveElement.current = ownerDocument.activeElement; | ||
|
||
function onChange(event: FocusEvent) { | ||
if (lastActiveElement.current !== ownerDocument.activeElement) { | ||
handleChange( | ||
ownerDocument.activeElement, | ||
lastActiveElement.current, | ||
event | ||
); | ||
lastActiveElement.current = ownerDocument.activeElement; | ||
if ( | ||
when === "focus" && | ||
lastActiveElement.current === ownerDocument.activeElement | ||
) { | ||
return; | ||
} | ||
handleChange( | ||
ownerDocument.activeElement, | ||
lastActiveElement.current, | ||
event | ||
); | ||
lastActiveElement.current = ownerDocument.activeElement; | ||
} | ||
|
||
ownerDocument.addEventListener(when, onChange, true); | ||
|
||
return () => { | ||
ownerDocument.removeEventListener(when, onChange); | ||
ownerDocument.removeEventListener(when, onChange, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing the handler didn't work before. |
||
}; | ||
}, [when, handleChange, ownerDocument]); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If do not check it and try use this hook with
blur
, handleChange will never be called, becauseownerDocument.activeElement
will be alwaysownerDocument.body
.