forked from vonovak/react-navigation-header-buttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeaderButtons.test.tsx
130 lines (120 loc) · 4.06 KB
/
HeaderButtons.test.tsx
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
/* eslint-env jest */
import { Item } from '../HeaderItems';
import { HeaderButtons } from '../HeaderButtons';
import { HeaderButton, HeaderButtonProps } from '../HeaderButton';
import { render, fireEvent } from '@testing-library/react-native';
import { Text, TextProps } from 'react-native';
import React from 'react';
beforeEach(() => {
jest.spyOn(global, 'requestAnimationFrame').mockImplementation((cb) => {
cb(0);
return 0;
});
});
afterEach(() => {
// @ts-ignore
global.requestAnimationFrame.mockRestore();
});
describe('HeaderButtons', () => {
it('renders button with pressable labels when Item is a direct or indirect child', () => {
const searchOnPress = jest.fn();
const deleteOnPress = jest.fn();
const WrappedItem = () => <Item title="Delete" onPress={deleteOnPress} />;
const { getByText } = render(
<HeaderButtons>
<Item title="Search" onPress={searchOnPress} />
<WrappedItem />
</HeaderButtons>
);
fireEvent.press(getByText('Search'));
expect(searchOnPress).toHaveBeenCalled();
fireEvent.press(getByText('Delete'));
expect(deleteOnPress).toHaveBeenCalled();
});
it('renders totally custom elements when provided', () => {
const searchOnPress = jest.fn();
const deleteOnPress = jest.fn();
const WrappedItem = () => <Text onPress={deleteOnPress}>Delete</Text>;
const { getByText, UNSAFE_queryAllByType } = render(
<HeaderButtons>
<Text onPress={searchOnPress}>Search</Text>
<WrappedItem />
</HeaderButtons>
);
expect(UNSAFE_queryAllByType(Item)).toHaveLength(0);
fireEvent.press(getByText('Search'));
expect(searchOnPress).toHaveBeenCalled();
fireEvent.press(getByText('Delete'));
expect(deleteOnPress).toHaveBeenCalled();
});
it('renders button with pressable Icons when Item is a direct or indirect child', () => {
const searchOnPress = jest.fn();
const deleteOnPress = jest.fn();
const WrappedItem = () => {
return (
<Item
title="delete"
iconName="ios-delete"
onPress={deleteOnPress}
accessibilityHint="delete"
/>
);
};
const IoniconsHeaderButton = (props: HeaderButtonProps) => (
<HeaderButton
{...props}
IconComponent={({ name }) => <Text>{name}</Text>}
iconSize={23}
color="blue"
/>
);
const { queryAllByText, getByAccessibilityHint } = render(
<HeaderButtons HeaderButtonComponent={IoniconsHeaderButton}>
<Item
iconName="ios-search"
title="search"
onPress={searchOnPress}
accessibilityHint="search"
/>
<WrappedItem />
</HeaderButtons>
);
expect(queryAllByText('Search')).toHaveLength(0);
fireEvent.press(getByAccessibilityHint('search'));
expect(searchOnPress).toHaveBeenCalled();
expect(queryAllByText('Delete')).toHaveLength(0);
fireEvent.press(getByAccessibilityHint('delete'));
expect(deleteOnPress).toHaveBeenCalled();
});
it('props passed to HeaderButtonComponent or Item are passed through to HeaderButton and thus to the rendered result', () => {
const searchOnPress = jest.fn();
const CustomIconComponent = (props: TextProps) => <Text {...props} />;
const IoniconsHeaderButton = (props: HeaderButtonProps) => (
<HeaderButton
IconComponent={CustomIconComponent}
iconSize={23}
color="blue"
{...props}
/>
);
const { UNSAFE_getAllByType } = render(
<HeaderButtons HeaderButtonComponent={IoniconsHeaderButton}>
<Item
iconName="1"
title="search"
onPress={searchOnPress}
iconSize={30}
/>
</HeaderButtons>
);
const [item] = UNSAFE_getAllByType(HeaderButton);
const { color, iconName, iconSize, title, IconComponent, onPress } =
item.props;
expect(color).toBe('blue');
expect(iconName).toBe('1');
expect(iconSize).toBe(30);
expect(title).toBe('search');
expect(onPress).toBe(searchOnPress);
expect(IconComponent).toBe(CustomIconComponent);
});
});