forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useCounter.test.ts
208 lines (153 loc) Β· 4.99 KB
/
useCounter.test.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { act, renderHook } from '@testing-library/react-hooks';
import useCounter from '../src/useCounter';
const setUp = (initialValue?: number, max: number | null = null, min: number | null = null) =>
renderHook(() => useCounter(initialValue, max, min));
it('should init counter and utils', () => {
const { result } = setUp(5);
expect(result.current[0]).toBe(5);
expect(result.current[1]).toStrictEqual({
inc: expect.any(Function),
dec: expect.any(Function),
get: expect.any(Function),
set: expect.any(Function),
reset: expect.any(Function),
});
});
it('should init counter to 0 if not initial value received', () => {
const { result } = setUp();
expect(result.current[0]).toBe(0);
});
it('should init counter to negative number', () => {
const { result } = setUp(-2);
expect(result.current[0]).toBe(-2);
});
it('should get current counter', () => {
const { result } = setUp(5);
const { get } = result.current[1];
expect(get()).toBe(5);
});
it('should increment by 1 if not value received', () => {
const { result } = setUp(5);
const { get, inc } = result.current[1];
act(() => inc());
expect(result.current[0]).toBe(6);
expect(get()).toBe(6);
});
it('should increment by value received', () => {
const { result } = setUp(5);
const { get, inc } = result.current[1];
act(() => inc(9));
expect(result.current[0]).toBe(14);
expect(get()).toBe(14);
});
it('should decrement by 1 if not value received', () => {
const { result } = setUp(5);
const { get, dec } = result.current[1];
act(() => dec());
expect(result.current[0]).toBe(4);
expect(get()).toBe(4);
});
it('should decrement by value received', () => {
const { result } = setUp(5);
const { get, dec } = result.current[1];
act(() => dec(9));
expect(result.current[0]).toBe(-4);
expect(get()).toBe(-4);
});
it('should set to value received', () => {
const { result } = setUp(5);
const { get, set } = result.current[1];
act(() => set(17));
expect(result.current[0]).toBe(17);
expect(get()).toBe(17);
});
it('should reset to original value', () => {
const { result } = setUp(5);
const { get, set, reset } = result.current[1];
// set different value than initial one...
act(() => set(17));
expect(result.current[0]).toBe(17);
// ... and reset it to initial one
act(() => reset());
expect(result.current[0]).toBe(5);
expect(get()).toBe(5);
});
it('should reset and set new original value', () => {
const { result } = setUp(5);
const { get, set, reset } = result.current[1];
// set different value than initial one...
act(() => set(17));
expect(result.current[0]).toBe(17);
// ... now reset and set it to different than initial one...
act(() => reset(8));
expect(result.current[0]).toBe(8);
// ... and set different value than initial one again...
act(() => set(32));
expect(result.current[0]).toBe(32);
// ... and reset it to new initial value
act(() => reset());
expect(result.current[0]).toBe(8);
expect(get()).toBe(8);
});
it('should not exceed max value', () => {
const { result } = setUp(10, 5);
expect(result.current[0]).toBe(5);
const { get, inc, reset } = result.current[1];
act(() => reset(10));
expect(get()).toBe(5);
act(() => reset(4));
expect(get()).toBe(4);
act(() => inc());
expect(get()).toBe(5);
act(() => inc());
expect(get()).toBe(5);
});
it('should not exceed min value', () => {
const { result } = setUp(3, null, 5);
expect(result.current[0]).toBe(5);
const { get, dec, reset } = result.current[1];
act(() => reset(4));
expect(get()).toBe(5);
act(() => reset(6));
expect(get()).toBe(6);
act(() => dec());
expect(get()).toBe(5);
act(() => dec());
expect(get()).toBe(5);
});
describe('should `console.error` on unexpected inputs', () => {
it('on any of call parameters', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
// @ts-ignore
setUp(false);
expect(spy.mock.calls[0][0]).toBe('initialValue has to be a number, got boolean');
// @ts-ignore
setUp(10, false);
expect(spy.mock.calls[1][0]).toBe('max has to be a number, got boolean');
// @ts-ignore
setUp(10, 5, {});
expect(spy.mock.calls[2][0]).toBe('min has to be a number, got object');
spy.mockRestore();
});
it('on any of returned methods has unexpected input', () => {
const { result } = setUp(10);
const { inc, dec, reset } = result.current[1];
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
// @ts-ignore
act(() => inc(false));
expect(spy.mock.calls[0][0]).toBe(
'delta has to be a number or function returning a number, got boolean'
);
// @ts-ignore
act(() => dec(false));
expect(spy.mock.calls[1][0]).toBe(
'delta has to be a number or function returning a number, got boolean'
);
// @ts-ignore
act(() => reset({}));
expect(spy.mock.calls[2][0]).toBe(
'value has to be a number or function returning a number, got object'
);
spy.mockRestore();
});
});