-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
104 lines (92 loc) · 3.33 KB
/
index.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
/**
* @jest-environment jsdom
*/
import { act, renderHook } from '@testing-library/react';
import { describe, expect, jest, test } from '@jest/globals';
import { useRecapture, useEventEffect } from './index';
describe('useRecapture', () => {
test('should not throw if callback is nullish', () => {
const { result, rerender } = renderHook(() => useRecapture());
const state = {
get callback() {
return result.current[0];
},
get recapture() {
return result.current[1];
},
};
expect(() => act(() => state.callback())).not.toThrow();
// eslint-disable-next-line @typescript-eslint/no-empty-function
act(() => state.recapture(() => {}));
rerender();
expect(() => act(() => state.callback())).not.toThrow();
});
test('should maintain stable reference after recapture', () => {
const version1 = jest.fn((i: number | string) =>
console.log(`Version 1 called with "${i}"`),
);
const version2 = jest.fn((i: number | string) =>
console.log(`Version 2 called with "${i}"`),
);
const { result, rerender } = renderHook(() =>
useRecapture<[i: number | string], void>(version1),
);
const state = {
get callback() {
return result.current[0];
},
get recapture() {
return result.current[1];
},
};
const stableReference = {
callback: state.callback,
recapture: state.recapture,
};
expect(version1).not.toHaveBeenCalled();
act(() => state.callback('test 1'));
expect(state.callback).toBe(stableReference.callback);
expect(state.recapture).toBe(stableReference.recapture);
expect(version1).toHaveBeenCalledTimes(1);
expect(version1).toHaveBeenCalledWith('test 1');
act(() => state.recapture(version2));
rerender();
expect(state.callback).toBe(stableReference.callback);
expect(state.recapture).toBe(stableReference.recapture);
expect(version1).toHaveBeenCalledTimes(1);
act(() => state.callback('test 2'));
expect(state.callback).toBe(stableReference.callback);
expect(state.recapture).toBe(stableReference.recapture);
expect(version1).toHaveBeenCalledTimes(1);
expect(version2).toHaveBeenCalledTimes(1);
expect(version2).toHaveBeenCalledWith('test 2');
});
});
describe('useEventEffect', () => {
test('should maintain stable reference after recapture', () => {
const version1 = jest.fn((i: number | string) =>
console.log(`Version 1 called with "${i}"`),
);
const version2 = jest.fn((i: number | string) =>
console.log(`Version 2 called with "${i}"`),
);
const { result, rerender } = renderHook(
(effect) => useEventEffect(effect),
{ initialProps: version1 },
);
const stableReference = result.current;
expect(version1).not.toHaveBeenCalled();
act(() => result.current('test 1'));
expect(result.current).toBe(stableReference);
expect(version1).toHaveBeenCalledTimes(1);
expect(version1).toHaveBeenCalledWith('test 1');
rerender(version2);
expect(result.current).toBe(stableReference);
expect(version1).toHaveBeenCalledTimes(1);
act(() => result.current('test 2'));
expect(result.current).toBe(stableReference);
expect(version1).toHaveBeenCalledTimes(1);
expect(version2).toHaveBeenCalledTimes(1);
expect(version2).toHaveBeenCalledWith('test 2');
});
});