-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.ts
128 lines (104 loc) · 3.85 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { renderHook } from '@testing-library/react-hooks';
import { useScrollToElement } from './';
describe('useScrollToElement custom hook', () => {
const ELEMENT_NAMES = [
'Question 1',
'Question 2',
'Question 3',
'Question 4',
'Question 5',
'Question 6',
'Question 7',
];
test('should return refs based on category input', () => {
const { result } = renderHook(() => useScrollToElement(ELEMENT_NAMES));
expect(result.current.getScrollToElementRef(ELEMENT_NAMES[0], 0)).not.toBe(null);
expect(result.current.getScrollToElementRef(ELEMENT_NAMES[5], 5)).not.toBe(null);
});
test('should return same elementRef if re-rendered with same list of elementNames', () => {
const { result, rerender } = renderHook(
({ initialValue }) => useScrollToElement(initialValue),
{
initialProps: {
initialValue: ELEMENT_NAMES,
},
},
);
const function1A = result.current.getScrollToElementRef;
const function2A = result.current.scrollToElementClickHandler;
rerender({
initialValue: ELEMENT_NAMES,
});
const function1B = result.current.getScrollToElementRef;
const function2B = result.current.scrollToElementClickHandler;
expect(function1A).toBe(function1B);
expect(function2A).toBe(function2B);
});
test('should return same scrollToElementClickHandler if re-rendered with same list of elementNames and scrollIntoViewOptions', () => {
const scrollOptions = {
behavior: 'auto',
} as ScrollIntoViewOptions;
const { result, rerender } = renderHook(
({ initialValue, initialScrollIntoViewOptions }) =>
useScrollToElement(initialValue, initialScrollIntoViewOptions),
{
initialProps: {
initialValue: ELEMENT_NAMES,
initialScrollIntoViewOptions: scrollOptions,
},
},
);
const function1A = result.current.getScrollToElementRef;
const function2A = result.current.scrollToElementClickHandler;
rerender({
initialValue: ELEMENT_NAMES,
initialScrollIntoViewOptions: scrollOptions,
});
const function1B = result.current.getScrollToElementRef;
const function2B = result.current.scrollToElementClickHandler;
expect(function1A).toBe(function1B);
expect(function2A).toBe(function2B);
});
test('should return different elementRef if re-rendered with different list of elementNames', () => {
const { result, rerender } = renderHook(
({ initialValue }) => useScrollToElement(initialValue),
{
initialProps: {
initialValue: ELEMENT_NAMES,
},
},
);
const function1A = result.current.getScrollToElementRef;
const function2A = result.current.scrollToElementClickHandler;
rerender({
initialValue: ELEMENT_NAMES.slice().splice(1),
});
const function1B = result.current.getScrollToElementRef;
const function2B = result.current.scrollToElementClickHandler;
expect(function1A).not.toBe(function1B);
expect(function2A).not.toBe(function2B);
});
test('should return different scrollToElementClickHandler if re-rendered with different scrollIntoViewOptions', () => {
const { result, rerender } = renderHook(
({ initialValue, initialScrollIntoViewOptions }) =>
useScrollToElement(initialValue, initialScrollIntoViewOptions),
{
initialProps: {
initialValue: ELEMENT_NAMES,
initialScrollIntoViewOptions: {
behavior: 'smooth',
} as ScrollIntoViewOptions,
},
},
);
const function2A = result.current.scrollToElementClickHandler;
rerender({
initialValue: ELEMENT_NAMES,
initialScrollIntoViewOptions: {
behavior: 'auto',
} as ScrollIntoViewOptions,
});
const function2B = result.current.scrollToElementClickHandler;
expect(function2A).not.toBe(function2B);
});
});