Skip to content

Commit d58d027

Browse files
committed
add tests for useControlledState
1 parent fd852db commit d58d027

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

internal/test/utils.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import * as React from "react";
2-
import { act } from "react-dom/test-utils";
32
import type { MatcherFunction } from "@testing-library/react";
43
import { render as tlRender, fireEvent } from "@testing-library/react";
54
import { renderHook as tlRenderHook } from "@testing-library/react-hooks";
6-
import { fireEvent as fireDomEvent } from "@testing-library/dom";
7-
import userEvent from "@testing-library/user-event";
85
import type {
96
RenderHookOptions,
107
RenderHookResult,
@@ -142,7 +139,10 @@ export function simulateEnterKeyClick(
142139

143140
type Query = (f: MatcherFunction) => HTMLElement | null;
144141

145-
export { cleanup as cleanupHooks } from "@testing-library/react-hooks";
146-
export { cleanup, fireEvent, screen } from "@testing-library/react";
147-
export { act, userEvent, fireDomEvent };
142+
export {
143+
cleanup as cleanupHooks,
144+
act as actHooks,
145+
} from "@testing-library/react-hooks";
146+
export { cleanup, fireEvent, screen, act } from "@testing-library/react";
147+
export * as userEvent from "@testing-library/user-event";
148148
export type { RenderOptions, RenderResult };
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { renderHook, cleanupHooks, actHooks } from "@reach-internal/test/utils";
3+
import { useControlledState } from "@reach/utils";
4+
5+
afterEach(cleanupHooks);
6+
7+
describe("useControlledState", () => {
8+
const DEFAULT_VALUE = 10;
9+
const CONTROLLED_VALUE = 42;
10+
11+
it("should return value and setter", () => {
12+
const { result } = renderHook(() =>
13+
useControlledState({
14+
defaultValue: DEFAULT_VALUE,
15+
controlledValue: undefined,
16+
})
17+
);
18+
19+
expect(result.current[0]).toBe(DEFAULT_VALUE);
20+
expect(typeof result.current[1]).toBe("function");
21+
});
22+
23+
it("should work as uncontrolled", () => {
24+
const { result } = renderHook(() =>
25+
useControlledState({
26+
defaultValue: DEFAULT_VALUE,
27+
controlledValue: undefined,
28+
})
29+
);
30+
expect(result.current[0]).toBe(DEFAULT_VALUE);
31+
actHooks(() => {
32+
result.current[1](17);
33+
});
34+
expect(result.current[0]).toBe(17);
35+
});
36+
37+
it("should work as controlled", () => {
38+
const { result } = renderHook(() =>
39+
useControlledState({
40+
defaultValue: DEFAULT_VALUE,
41+
controlledValue: CONTROLLED_VALUE,
42+
})
43+
);
44+
expect(result.current[0]).toBe(CONTROLLED_VALUE);
45+
actHooks(() => {
46+
result.current[1](17);
47+
});
48+
expect(result.current[0]).toBe(CONTROLLED_VALUE);
49+
});
50+
});

0 commit comments

Comments
 (0)