-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.tsx
112 lines (91 loc) · 2.92 KB
/
index.test.tsx
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
107
108
109
110
111
112
import {
act,
fireEvent,
render,
renderHook,
screen,
} from "@testing-library/react";
import { useClsState } from "./index";
describe("useClsState", () => {
function renderUseClsStateHook<T>(initialState?: T) {
return renderHook(() => useClsState(initialState));
}
it("should be able to use undefined as state", () => {
const { result: hook } = renderUseClsStateHook(undefined);
expect(hook.current[0]).toBe(undefined);
act(() => {
hook.current[1](undefined);
});
expect(hook.current[0]).toBe(undefined);
});
it("should use undefined as state when initial state is absent", () => {
const { result: hook } = renderUseClsStateHook();
expect(hook.current[0]).toBe(undefined);
});
it("should be able to use null as state", () => {
const { result: hook } = renderUseClsStateHook(null);
expect(hook.current[0]).toBe(null);
act(() => {
hook.current[1](null);
});
expect(hook.current[0]).toBe(null);
});
it("should be able to use object as state", () => {
const { result: hook } = renderUseClsStateHook({ a: 123, b: "abc" });
expect(hook.current[0]).toEqual({ a: 123, b: "abc" });
act(() => {
hook.current[1]({ a: 456, b: "def" });
});
expect(hook.current[0]).toEqual({ a: 456, b: "def" });
});
class State {
public a: number;
private b: string;
public constructor(data: { a: number; b: string }) {
this.a = data.a;
this.b = data.b;
}
public increment() {
this.a = this.a + 1;
}
public equal(state: State) {
return this.a === state.a && this.b === state.b;
}
}
it("should be able to use class as state", () => {
const { result: hook } = renderUseClsStateHook(
new State({ a: 123, b: "abc" }),
);
expect(hook.current[0]).toBeInstanceOf(State);
expect(hook.current[0].equal(new State({ a: 123, b: "abc" })));
act(() => {
hook.current[1](new State({ a: 456, b: "def" }));
});
expect(hook.current[0]).toBeInstanceOf(State);
expect(hook.current[0].equal(new State({ a: 456, b: "def" })));
});
it("should only trigger re-render when class state is updated by hook", () => {
function Page(props: { initialState: State }) {
const [state, setState] = useClsState(props.initialState);
return (
<div>
<p>{state.a}</p>
<button onClick={() => state.increment()}>Update class</button>
<button
onClick={() => {
setState(state);
}}
>
Update state
</button>
</div>
);
}
render(<Page initialState={new State({ a: 123, b: "abc" })} />);
expect(screen.queryByText(123)).not.toBe(null);
fireEvent.click(screen.getByText(/update class/i));
expect(screen.queryByText(123)).not.toBe(null);
fireEvent.click(screen.getByText(/update state/i));
expect(screen.queryByText(124)).not.toBe(null);
});
});