-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathHeaderWithTitleLeftScrollable.test.tsx
More file actions
306 lines (264 loc) · 8.75 KB
/
HeaderWithTitleLeftScrollable.test.tsx
File metadata and controls
306 lines (264 loc) · 8.75 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Third party dependencies.
import React from 'react';
import { render, renderHook } from '@testing-library/react-native';
import { useSharedValue, SharedValue } from 'react-native-reanimated';
import { Text } from 'react-native';
// External dependencies.
import { IconName } from '@metamask/design-system-react-native';
// Internal dependencies.
import HeaderWithTitleLeftScrollable from './HeaderWithTitleLeftScrollable';
import useHeaderWithTitleLeftScrollable from './useHeaderWithTitleLeftScrollable';
// Mock react-native-reanimated
jest.mock('react-native-reanimated', () => {
const Reanimated = jest.requireActual('react-native-reanimated/mock');
Reanimated.useSharedValue = jest.fn((initial) => ({
value: initial,
}));
Reanimated.useAnimatedStyle = jest.fn((fn) => fn());
Reanimated.interpolate = jest.fn(
(_value, _inputRange, outputRange) => outputRange[0],
);
Reanimated.Extrapolation = { CLAMP: 'clamp' };
return Reanimated;
});
// Mock react-native-safe-area-context
jest.mock('react-native-safe-area-context', () => ({
useSafeAreaInsets: () => ({ top: 44, bottom: 34, left: 0, right: 0 }),
}));
// Test wrapper component that provides scrollY
const TestWrapper: React.FC<{
children: (scrollYValue: SharedValue<number>) => React.ReactNode;
}> = ({ children }) => {
const scrollYValue = useSharedValue(0);
return <>{children(scrollYValue)}</>;
};
describe('HeaderWithTitleLeftScrollable', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('rendering', () => {
it('renders with title', () => {
const { getAllByText } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test Title"
scrollY={scrollYValue}
/>
)}
</TestWrapper>,
);
expect(getAllByText('Test Title').length).toBeGreaterThan(0);
});
it('renders container with testID when provided', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
testID="test-container"
/>
)}
</TestWrapper>,
);
expect(getByTestId('test-container')).toBeOnTheScreen();
});
});
describe('back button', () => {
it('renders back button when onBack provided', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
onBack={jest.fn()}
backButtonProps={{ testID: 'test-back-button' }}
/>
)}
</TestWrapper>,
);
expect(getByTestId('test-back-button')).toBeOnTheScreen();
});
it('renders back button when backButtonProps provided', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
backButtonProps={{
onPress: jest.fn(),
testID: 'test-back-button',
}}
/>
)}
</TestWrapper>,
);
expect(getByTestId('test-back-button')).toBeOnTheScreen();
});
});
describe('close button', () => {
it('renders close button when onClose provided', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
onClose={jest.fn()}
closeButtonProps={{ testID: 'test-close-button' }}
/>
)}
</TestWrapper>,
);
expect(getByTestId('test-close-button')).toBeOnTheScreen();
});
it('renders close button when closeButtonProps provided', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
closeButtonProps={{
onPress: jest.fn(),
testID: 'test-close-button',
}}
/>
)}
</TestWrapper>,
);
expect(getByTestId('test-close-button')).toBeOnTheScreen();
});
});
describe('endButtonIconProps', () => {
it('renders endButtonIconProps', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
endButtonIconProps={[
{
iconName: IconName.Close,
onPress: jest.fn(),
testID: 'end-button',
},
]}
/>
)}
</TestWrapper>,
);
expect(getByTestId('end-button')).toBeOnTheScreen();
});
});
describe('isInsideSafeAreaView', () => {
it('positions header at top 0 when isInsideSafeAreaView is false', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
isInsideSafeAreaView={false}
testID="test-container"
/>
)}
</TestWrapper>,
);
const container = getByTestId('test-container');
const flattenedStyle = Array.isArray(container.props.style)
? Object.assign({}, ...container.props.style)
: container.props.style;
expect(flattenedStyle.top).toBe(0);
});
it('positions header at insets.top when isInsideSafeAreaView is true', () => {
const { getByTestId } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
isInsideSafeAreaView
testID="test-container"
/>
)}
</TestWrapper>,
);
const container = getByTestId('test-container');
const flattenedStyle = Array.isArray(container.props.style)
? Object.assign({}, ...container.props.style)
: container.props.style;
expect(flattenedStyle.top).toBe(44);
});
});
describe('titleLeft and titleLeftProps', () => {
it('renders custom titleLeft node', () => {
const { getByText } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
titleLeft={<Text>Custom Content</Text>}
/>
)}
</TestWrapper>,
);
expect(getByText('Custom Content')).toBeOnTheScreen();
});
it('titleLeft takes priority over titleLeftProps', () => {
const { getByText, queryByText } = render(
<TestWrapper>
{(scrollYValue) => (
<HeaderWithTitleLeftScrollable
title="Test"
scrollY={scrollYValue}
titleLeft={<Text>Custom Node</Text>}
titleLeftProps={{ title: 'Props Title' }}
/>
)}
</TestWrapper>,
);
expect(getByText('Custom Node')).toBeOnTheScreen();
expect(queryByText('Props Title')).toBeNull();
});
});
});
describe('useHeaderWithTitleLeftScrollable', () => {
it('returns onScroll handler', () => {
const { result } = renderHook(() => useHeaderWithTitleLeftScrollable());
expect(typeof result.current.onScroll).toBe('function');
});
it('returns scrollY shared value with initial value of 0', () => {
const { result } = renderHook(() => useHeaderWithTitleLeftScrollable());
expect(result.current.scrollY.value).toBe(0);
});
it('returns expandedHeight', () => {
const { result } = renderHook(() => useHeaderWithTitleLeftScrollable());
expect(result.current.expandedHeight).toBe(140);
});
it('returns scrollTriggerPosition defaulting to expandedHeight', () => {
const { result } = renderHook(() => useHeaderWithTitleLeftScrollable());
expect(result.current.scrollTriggerPosition).toBe(
result.current.expandedHeight,
);
});
it('uses custom scrollTriggerPosition when provided', () => {
const { result } = renderHook(() =>
useHeaderWithTitleLeftScrollable({
scrollTriggerPosition: 80,
}),
);
expect(result.current.scrollTriggerPosition).toBe(80);
});
it('uses custom expandedHeight when provided', () => {
const { result } = renderHook(() =>
useHeaderWithTitleLeftScrollable({ expandedHeight: 200 }),
);
expect(result.current.expandedHeight).toBe(200);
});
});