-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathHeaderBackButton.test.tsx
More file actions
243 lines (207 loc) · 8.18 KB
/
Copy pathHeaderBackButton.test.tsx
File metadata and controls
243 lines (207 loc) · 8.18 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fireEvent, render } from '@testing-library/react-native';
import { PixelRatio } from 'react-native';
import { HeaderBackButton, ICON_MARGIN, ICON_SIZE } from './HeaderBackButton';
describe('HeaderBackButton', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('Rendering', () => {
it('should render without crashing', () => {
const { getByTestId } = render(<HeaderBackButton testID="back-button" />);
expect(getByTestId('back-button')).toBeTruthy();
});
it('should render with default back arrow image', () => {
const { UNSAFE_getByType } = render(<HeaderBackButton />);
const image = UNSAFE_getByType('Image' as any);
expect(image).toBeTruthy();
expect(image.props.source).toMatchObject({
uri: expect.stringContaining('data:image/png;base64'),
width: PixelRatio.getPixelSizeForLayoutSize(ICON_SIZE),
height: PixelRatio.getPixelSizeForLayoutSize(ICON_SIZE),
});
});
it('should render without label by default', () => {
const { queryByText } = render(<HeaderBackButton />);
expect(queryByText(/./)).toBeNull();
});
it('should render with label when provided', () => {
const label = 'Back';
const { getByText } = render(<HeaderBackButton label={label} />);
expect(getByText(label)).toBeTruthy();
});
it('should render with custom label text', () => {
const customLabel = 'Go Back to Home';
const { getByText } = render(<HeaderBackButton label={customLabel} />);
expect(getByText(customLabel)).toBeTruthy();
});
});
describe('Custom Image Props', () => {
it('should render with custom imageUri', () => {
const customUri = 'https://example.com/custom-back-icon.png';
const { UNSAFE_getByType } = render(
<HeaderBackButton imageUri={customUri} />
);
const image = UNSAFE_getByType('Image' as any);
expect(image.props.source).toMatchObject({
uri: customUri,
});
});
it('should render with custom imageSource', () => {
const customSource = { uri: 'https://example.com/icon.png' };
const { UNSAFE_getByType } = render(
<HeaderBackButton imageSource={customSource} />
);
const image = UNSAFE_getByType('Image' as any);
expect(image.props.source).toEqual(customSource);
});
it('should prioritize imageSource over imageUri when both are provided', () => {
const customUri = 'https://example.com/custom-back-icon.png';
const customSource = { uri: 'https://example.com/icon.png' };
const { UNSAFE_getByType } = render(
<HeaderBackButton imageUri={customUri} imageSource={customSource} />
);
const image = UNSAFE_getByType('Image' as any);
expect(image.props.source).toEqual(customSource);
});
});
describe('Image Properties', () => {
it('should render image with correct properties', () => {
const { UNSAFE_getByType } = render(<HeaderBackButton />);
const image = UNSAFE_getByType('Image' as any);
expect(image.props.resizeMode).toBe('contain');
expect(image.props.fadeDuration).toBe(0);
expect(image.props.height).toBe(ICON_SIZE);
expect(image.props.width).toBe(ICON_SIZE);
expect(image.props.resizeMethod).toBe('scale');
expect(image.props.tintColor).toBeTruthy();
});
it('should apply correct style to image', () => {
const { UNSAFE_getByType } = render(<HeaderBackButton />);
const image = UNSAFE_getByType('Image' as any);
expect(image.props.style).toMatchObject({
height: ICON_SIZE,
margin: ICON_MARGIN,
width: ICON_SIZE,
});
});
});
describe('Touch Interaction', () => {
it('should call onPress when button is pressed', () => {
const onPressMock = jest.fn();
const { getByTestId } = render(
<HeaderBackButton testID="back-button" onPress={onPressMock} />
);
fireEvent.press(getByTestId('back-button'));
expect(onPressMock).toHaveBeenCalledTimes(1);
});
it('should call onPressIn when touch starts', () => {
const onPressInMock = jest.fn();
const { getByTestId } = render(
<HeaderBackButton testID="back-button" onPressIn={onPressInMock} />
);
fireEvent(getByTestId('back-button'), 'pressIn');
expect(onPressInMock).toHaveBeenCalledTimes(1);
});
it('should call onPressOut when touch ends', () => {
const onPressOutMock = jest.fn();
const { getByTestId } = render(
<HeaderBackButton testID="back-button" onPressOut={onPressOutMock} />
);
fireEvent(getByTestId('back-button'), 'pressOut');
expect(onPressOutMock).toHaveBeenCalledTimes(1);
});
it('should not trigger onPress when disabled', () => {
const onPressMock = jest.fn();
const { getByTestId } = render(
<HeaderBackButton
testID="back-button"
onPress={onPressMock}
disabled={true}
/>
);
fireEvent.press(getByTestId('back-button'));
expect(onPressMock).not.toHaveBeenCalled();
});
});
describe('Platform-specific behavior', () => {
it('should export correct icon size constant', () => {
// ICON_SIZE is evaluated at module load time based on Platform.OS
expect(ICON_SIZE).toBeDefined();
expect([21, 24]).toContain(ICON_SIZE);
});
it('should export correct icon margin constant', () => {
// ICON_MARGIN is evaluated at module load time based on Platform.OS
expect(ICON_MARGIN).toBeDefined();
expect([3, 8]).toContain(ICON_MARGIN);
});
it('should use consistent icon size in image props', () => {
const { UNSAFE_getByType } = render(<HeaderBackButton />);
const image = UNSAFE_getByType('Image' as any);
expect(image.props.height).toBe(ICON_SIZE);
expect(image.props.width).toBe(ICON_SIZE);
});
});
describe('Accessibility', () => {
it('should accept accessibility props', () => {
const { getByTestId } = render(
<HeaderBackButton
testID="back-button"
accessible={true}
accessibilityLabel="Navigate back"
accessibilityHint="Returns to previous screen"
/>
);
const button = getByTestId('back-button');
expect(button.props.accessible).toBe(true);
expect(button.props.accessibilityLabel).toBe('Navigate back');
expect(button.props.accessibilityHint).toBe('Returns to previous screen');
});
});
describe('Component Structure', () => {
it('should render View with correct flex direction', () => {
const { UNSAFE_getAllByType } = render(<HeaderBackButton label="Back" />);
const views = UNSAFE_getAllByType('View' as any);
// Find the view with returnButton style
const returnButtonView = views.find(
(view) =>
view.props.style?.flexDirection === 'row' &&
view.props.style?.alignItems === 'center'
);
expect(returnButtonView).toBeTruthy();
});
it('should render label text with correct style when provided', () => {
const { getByText } = render(<HeaderBackButton label="Back" />);
const labelElement = getByText('Back');
expect(labelElement.props.style).toMatchObject({
fontSize: 20,
});
});
});
describe('Edge Cases', () => {
it('should handle empty string label', () => {
const { queryByText } = render(<HeaderBackButton label="" />);
// Empty string should not render text
expect(queryByText('')).toBeNull();
});
it('should handle multiple props correctly', () => {
const onPressMock = jest.fn();
const label = 'Custom Back';
const customUri = 'https://example.com/icon.png';
const { getByText, getByTestId } = render(
<HeaderBackButton
testID="back-button"
label={label}
imageUri={customUri}
onPress={onPressMock}
accessible={true}
accessibilityLabel="Go back"
/>
);
expect(getByText(label)).toBeTruthy();
expect(getByTestId('back-button').props.accessible).toBe(true);
fireEvent.press(getByTestId('back-button'));
expect(onPressMock).toHaveBeenCalledTimes(1);
});
});
});