-
Notifications
You must be signed in to change notification settings - Fork 560
Expand file tree
/
Copy pathuse-focus-change.test.tsx
More file actions
106 lines (92 loc) · 2.59 KB
/
use-focus-change.test.tsx
File metadata and controls
106 lines (92 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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();
});
});