-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathButtonBase.test.tsx
More file actions
201 lines (176 loc) · 4.81 KB
/
ButtonBase.test.tsx
File metadata and controls
201 lines (176 loc) · 4.81 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
// Third party dependencies.
import React from 'react';
import { shallow } from 'enzyme';
import { fireEvent, render } from '@testing-library/react-native';
// External dependencies.
import { IconName } from '../../../../Icons/Icon';
import { TextColor, TextVariant } from '../../../../Texts/Text';
// Internal dependencies.
import ButtonBase from './ButtonBase';
import { ButtonSize } from '../../Button.types';
describe('ButtonBase', () => {
it('renders with basic props', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('renders when disabled', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
isDisabled
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('calls onPress when button is pressed', () => {
const mockOnPress = jest.fn();
const { getByRole } = render(
<ButtonBase
label="Test Button"
onPress={mockOnPress}
size={ButtonSize.Md}
/>,
);
const button = getByRole('button');
fireEvent.press(button);
expect(mockOnPress).toHaveBeenCalledTimes(1);
});
it('renders disabled button with correct props', () => {
const mockOnPress = jest.fn();
const { getByRole } = render(
<ButtonBase
label="Test Button"
onPress={mockOnPress}
isDisabled
size={ButtonSize.Md}
/>,
);
const button = getByRole('button');
// Verify the button is marked as disabled
expect(button.props.disabled).toBe(true);
});
it('calls onPressIn when button is pressed in', () => {
const mockOnPressIn = jest.fn();
const { getByRole } = render(
<ButtonBase
label="Test Button"
onPress={() => null}
onPressIn={mockOnPressIn}
size={ButtonSize.Md}
/>,
);
const button = getByRole('button');
fireEvent(button, 'pressIn');
expect(mockOnPressIn).toHaveBeenCalledTimes(1);
});
it('renders disabled button with onPressIn prop', () => {
const mockOnPressIn = jest.fn();
const { getByRole } = render(
<ButtonBase
label="Test Button"
onPress={() => null}
onPressIn={mockOnPressIn}
isDisabled
size={ButtonSize.Md}
/>,
);
const button = getByRole('button');
// Verify the button is marked as disabled
expect(button.props.disabled).toBe(true);
});
it('renders with start icon', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
startIconName={IconName.Add}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('renders with end icon', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
endIconName={IconName.Add}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('renders with both start and end icons', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
startIconName={IconName.Add}
endIconName={IconName.ArrowRight}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('renders with custom label component', () => {
const CustomLabel = () => <div>Custom Label</div>;
const wrapper = shallow(
<ButtonBase
label={<CustomLabel />}
onPress={() => null}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('handles different sizes', () => {
const sizes = [ButtonSize.Sm, ButtonSize.Md, ButtonSize.Lg];
sizes.forEach((size) => {
const wrapper = shallow(
<ButtonBase label="Click me!" onPress={() => null} size={size} />,
);
expect(wrapper).toMatchSnapshot(`ButtonBase with size ${size}`);
});
});
it('handles different widths', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
width={200}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('handles custom label color', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
labelColor={TextColor.Alternative}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
it('handles custom label text variant', () => {
const wrapper = shallow(
<ButtonBase
label="Click me!"
onPress={() => null}
labelTextVariant={TextVariant.HeadingSM}
size={ButtonSize.Md}
/>,
);
expect(wrapper).toMatchSnapshot();
});
});