-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.test.tsx
161 lines (145 loc) · 5 KB
/
Button.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
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
/**
* @format
*/
import 'react-native';
import 'jest-styled-components';
import React from 'react';
import Button from './Button';
import { shallow } from 'enzyme';
import { systemColors } from '../../styles/system-colors';
import { buttonTouchedColors } from './buttonColors';
import { grayscaleColors } from '../../styles/grayscale-colors';
describe('[Button] Unit Test', () => {
it('should fire onPress event', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(<Button onPress={onPressMock}>{textMock}</Button>);
button.simulate('press');
expect(onPressMock).toHaveBeenCalledTimes(1);
});
it('should not fire onPress event when disabled', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button disabled onPress={onPressMock}>
{textMock}
</Button>,
);
button.simulate('press');
expect(onPressMock).toHaveBeenCalledTimes(0);
});
it('should not fire onPress event when loading', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button loading onPress={onPressMock}>
{textMock}
</Button>,
);
button.simulate('press');
expect(onPressMock).toHaveBeenCalledTimes(0);
});
it('should not fire onPress event when loading and disabled', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button loading disabled onPress={onPressMock}>
{textMock}
</Button>,
);
button.simulate('press');
expect(onPressMock).toHaveBeenCalledTimes(0);
});
it('should show corresponding colors on pressed state', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button
color={systemColors.PRIMARY}
onPress={onPressMock}
icon={require('../../assets/icons/shop/shop.png')}
>
{textMock}
</Button>,
);
const container = shallow(button.props().children(true));
const buttonText = container.find('ButtonText');
const buttonIcon = container.find('ButtonIcon');
const containerProps = container.props() as {
containerTouchedColor?: string;
};
const buttonTextProps = buttonText.props() as { textColor?: string };
const buttonIconProps = buttonIcon.props() as { color?: string };
expect(containerProps.containerTouchedColor).toBe(
buttonTouchedColors[systemColors.PRIMARY],
);
expect(buttonTextProps.textColor).toBe(systemColors.WHITE);
expect(buttonIconProps.color).toBe(systemColors.WHITE);
});
it('should show fixed color on pressed state if touchedColor is set', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button
color={systemColors.PRIMARY}
onPress={onPressMock}
touchedColor={systemColors.SUCCESS}
icon={require('../../assets/icons/shop/shop.png')}
>
{textMock}
</Button>,
);
const container = shallow(button.props().children(true));
const buttonText = container.find('ButtonText');
const buttonIcon = container.find('ButtonIcon');
const containerProps = container.props() as {
containerTouchedColor?: string;
};
const buttonTextProps = buttonText.props() as { textColor?: string };
const buttonIconProps = buttonIcon.props() as { color?: string };
expect(containerProps.containerTouchedColor).toBe(systemColors.SUCCESS);
expect(buttonTextProps.textColor).toBe(systemColors.WHITE);
expect(buttonIconProps.color).toBe(systemColors.WHITE);
});
it('should show grayscale color as touchedColor if type is text', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button
type="text"
color={systemColors.PRIMARY}
onPress={onPressMock}
icon={require('../../assets/icons/shop/shop.png')}
>
{textMock}
</Button>,
);
const container = shallow(button.props().children(true));
const containerProps = container.props() as {
containerTouchedColor?: string;
};
expect(containerProps.containerTouchedColor).toBe(grayscaleColors.GRAY_100);
});
it('should show icon color with prop color if type is text', () => {
const onPressMock = jest.fn();
const textMock = 'test';
const button = shallow(
<Button
type="text"
color={systemColors.PRIMARY}
onPress={onPressMock}
icon={require('../../assets/icons/shop/shop.png')}
>
{textMock}
</Button>,
);
const container = shallow(button.props().children(true));
const buttonIcon = container.find('ButtonIcon');
const containerProps = container.props() as {
containerTouchedColor?: string;
};
const buttonIconProps = buttonIcon.props() as { color?: string };
expect(containerProps.containerTouchedColor).toBe(grayscaleColors.GRAY_100);
expect(buttonIconProps.color).toBe(systemColors.PRIMARY);
});
});