@@ -7,6 +7,9 @@ import 'jest-styled-components';
7
7
import React from 'react' ;
8
8
import Button from './Button' ;
9
9
import { shallow } from 'enzyme' ;
10
+ import { systemColors } from '../../styles/system-colors' ;
11
+ import { buttonTouchedColors } from './buttonColors' ;
12
+ import { grayscaleColors } from '../../styles/grayscale-colors' ;
10
13
11
14
describe ( '[Button] Unit Test' , ( ) => {
12
15
it ( 'should fire onPress event' , ( ) => {
@@ -18,4 +21,141 @@ describe('[Button] Unit Test', () => {
18
21
button . simulate ( 'press' ) ;
19
22
expect ( onPressMock ) . toHaveBeenCalledTimes ( 1 ) ;
20
23
} ) ;
24
+ it ( 'should not fire onPress event when disabled' , ( ) => {
25
+ const onPressMock = jest . fn ( ) ;
26
+ const textMock = 'test' ;
27
+ const button = shallow (
28
+ < Button disabled onPress = { onPressMock } >
29
+ { textMock }
30
+ </ Button > ,
31
+ ) ;
32
+ button . simulate ( 'press' ) ;
33
+ expect ( onPressMock ) . toHaveBeenCalledTimes ( 0 ) ;
34
+ } ) ;
35
+ it ( 'should not fire onPress event when loading' , ( ) => {
36
+ const onPressMock = jest . fn ( ) ;
37
+ const textMock = 'test' ;
38
+ const button = shallow (
39
+ < Button loading onPress = { onPressMock } >
40
+ { textMock }
41
+ </ Button > ,
42
+ ) ;
43
+ button . simulate ( 'press' ) ;
44
+ expect ( onPressMock ) . toHaveBeenCalledTimes ( 0 ) ;
45
+ } ) ;
46
+ it ( 'should not fire onPress event when loading and disabled' , ( ) => {
47
+ const onPressMock = jest . fn ( ) ;
48
+ const textMock = 'test' ;
49
+ const button = shallow (
50
+ < Button loading disabled onPress = { onPressMock } >
51
+ { textMock }
52
+ </ Button > ,
53
+ ) ;
54
+ button . simulate ( 'press' ) ;
55
+ expect ( onPressMock ) . toHaveBeenCalledTimes ( 0 ) ;
56
+ } ) ;
57
+ it ( 'should show corresponding colors on pressed state' , ( ) => {
58
+ const onPressMock = jest . fn ( ) ;
59
+ const textMock = 'test' ;
60
+ const button = shallow (
61
+ < Button
62
+ color = { systemColors . PRIMARY }
63
+ onPress = { onPressMock }
64
+ icon = { require ( '../../assets/icons/shop/shop.png' ) }
65
+ >
66
+ { textMock }
67
+ </ Button > ,
68
+ ) ;
69
+ const container = shallow ( button . props ( ) . children ( true ) ) ;
70
+ const buttonText = container . find ( 'ButtonText' ) ;
71
+ const buttonIcon = container . find ( 'ButtonIcon' ) ;
72
+
73
+ const containerProps = container . props ( ) as {
74
+ containerTouchedColor ?: string ;
75
+ } ;
76
+
77
+ const buttonTextProps = buttonText . props ( ) as { textColor ?: string } ;
78
+ const buttonIconProps = buttonIcon . props ( ) as { color ?: string } ;
79
+
80
+ expect ( containerProps . containerTouchedColor ) . toBe (
81
+ buttonTouchedColors [ systemColors . PRIMARY ] ,
82
+ ) ;
83
+ expect ( buttonTextProps . textColor ) . toBe ( systemColors . WHITE ) ;
84
+ expect ( buttonIconProps . color ) . toBe ( systemColors . WHITE ) ;
85
+ } ) ;
86
+ it ( 'should show fixed color on pressed state if touchedColor is set' , ( ) => {
87
+ const onPressMock = jest . fn ( ) ;
88
+ const textMock = 'test' ;
89
+ const button = shallow (
90
+ < Button
91
+ color = { systemColors . PRIMARY }
92
+ onPress = { onPressMock }
93
+ touchedColor = { systemColors . SUCCESS }
94
+ icon = { require ( '../../assets/icons/shop/shop.png' ) }
95
+ >
96
+ { textMock }
97
+ </ Button > ,
98
+ ) ;
99
+ const container = shallow ( button . props ( ) . children ( true ) ) ;
100
+ const buttonText = container . find ( 'ButtonText' ) ;
101
+ const buttonIcon = container . find ( 'ButtonIcon' ) ;
102
+
103
+ const containerProps = container . props ( ) as {
104
+ containerTouchedColor ?: string ;
105
+ } ;
106
+
107
+ const buttonTextProps = buttonText . props ( ) as { textColor ?: string } ;
108
+ const buttonIconProps = buttonIcon . props ( ) as { color ?: string } ;
109
+
110
+ expect ( containerProps . containerTouchedColor ) . toBe ( systemColors . SUCCESS ) ;
111
+ expect ( buttonTextProps . textColor ) . toBe ( systemColors . WHITE ) ;
112
+ expect ( buttonIconProps . color ) . toBe ( systemColors . WHITE ) ;
113
+ } ) ;
114
+ it ( 'should show grayscale color as touchedColor if type is text' , ( ) => {
115
+ const onPressMock = jest . fn ( ) ;
116
+ const textMock = 'test' ;
117
+ const button = shallow (
118
+ < Button
119
+ type = "text"
120
+ color = { systemColors . PRIMARY }
121
+ onPress = { onPressMock }
122
+ icon = { require ( '../../assets/icons/shop/shop.png' ) }
123
+ >
124
+ { textMock }
125
+ </ Button > ,
126
+ ) ;
127
+ const container = shallow ( button . props ( ) . children ( true ) ) ;
128
+
129
+ const containerProps = container . props ( ) as {
130
+ containerTouchedColor ?: string ;
131
+ } ;
132
+
133
+ expect ( containerProps . containerTouchedColor ) . toBe ( grayscaleColors . GRAY_100 ) ;
134
+ } ) ;
135
+
136
+ it ( 'should show icon color with prop color if type is text' , ( ) => {
137
+ const onPressMock = jest . fn ( ) ;
138
+ const textMock = 'test' ;
139
+ const button = shallow (
140
+ < Button
141
+ type = "text"
142
+ color = { systemColors . PRIMARY }
143
+ onPress = { onPressMock }
144
+ icon = { require ( '../../assets/icons/shop/shop.png' ) }
145
+ >
146
+ { textMock }
147
+ </ Button > ,
148
+ ) ;
149
+ const container = shallow ( button . props ( ) . children ( true ) ) ;
150
+ const buttonIcon = container . find ( 'ButtonIcon' ) ;
151
+
152
+ const containerProps = container . props ( ) as {
153
+ containerTouchedColor ?: string ;
154
+ } ;
155
+
156
+ const buttonIconProps = buttonIcon . props ( ) as { color ?: string } ;
157
+
158
+ expect ( containerProps . containerTouchedColor ) . toBe ( grayscaleColors . GRAY_100 ) ;
159
+ expect ( buttonIconProps . color ) . toBe ( systemColors . PRIMARY ) ;
160
+ } ) ;
21
161
} ) ;
0 commit comments