@@ -22,23 +22,15 @@ const renderIndicatorIcons = (props: IndicatorIconsProps) => {
22
22
} ;
23
23
} ;
24
24
25
- const createIndicatorIconsProps = ( ) => {
26
- const onClearMouseDownSpy = jest . fn ( ) ;
27
- const onCaretMouseDownSpy = jest . fn ( ) ;
28
-
29
- const props : IndicatorIconsProps = {
30
- menuOpen : false ,
31
- showClear : true ,
32
- onClearMouseDown : onClearMouseDownSpy ,
33
- onCaretMouseDown : onCaretMouseDownSpy
34
- } ;
25
+ const onClearMouseDownSpy = jest . fn ( ) ;
26
+ const onCaretMouseDownSpy = jest . fn ( ) ;
35
27
36
- return {
37
- props ,
38
- onClearMouseDownSpy ,
39
- onCaretMouseDownSpy
40
- } ;
41
- } ;
28
+ const BASE_PROPS : IndicatorIconsProps = {
29
+ menuOpen : false ,
30
+ showClear : true ,
31
+ onClearMouseDown : onClearMouseDownSpy ,
32
+ onCaretMouseDown : onCaretMouseDownSpy
33
+ } as const ;
42
34
43
35
const customIconFn = ( props : Partial < IndicatorIconsProps > ) : ReactNode => {
44
36
const { menuOpen, isLoading, isInvalid, isDisabled } = props ;
@@ -56,95 +48,82 @@ const customIconFn = (props: Partial<IndicatorIconsProps>): ReactNode => {
56
48
// ============================================
57
49
58
50
test ( 'clear icon has a static className (enables styling via classic CSS)' , async ( ) => {
59
- const { props } = createIndicatorIconsProps ( ) ;
60
- const { getByTestId } = renderIndicatorIcons ( props ) ;
51
+ const { getByTestId } = renderIndicatorIcons ( BASE_PROPS ) ;
61
52
const firstChildOfClearIconElement = getByTestId ( CLEAR_ICON_TESTID ! ) . firstChild ;
62
53
expect ( firstChildOfClearIconElement ) . toHaveClass ( CLEAR_ICON_CLS ) ;
63
54
} ) ;
64
55
65
56
test ( 'clear indicator has functioning "click" user interactions' , async ( ) => {
66
- const { props, onClearMouseDownSpy } = createIndicatorIconsProps ( ) ;
67
- const { user, getByTestId } = renderIndicatorIcons ( props ) ;
57
+ const { user, getByTestId } = renderIndicatorIcons ( BASE_PROPS ) ;
68
58
const clearIndicatorEl = getByTestId ( CLEAR_ICON_TESTID ! ) ;
69
-
70
59
await user . click ( clearIndicatorEl ) ;
71
-
72
60
expect ( onClearMouseDownSpy ) . toBeCalled ( ) ;
73
61
} ) ;
74
62
75
63
test ( 'caret indicator has functioning "click" user interactions' , async ( ) => {
76
- const { props, onCaretMouseDownSpy } = createIndicatorIconsProps ( ) ;
77
- const { user, getByTestId } = renderIndicatorIcons ( props ) ;
64
+ const { user, getByTestId } = renderIndicatorIcons ( BASE_PROPS ) ;
78
65
const caretIndicatorEl = getByTestId ( CARET_ICON_TESTID ! ) ;
79
-
80
66
await user . click ( caretIndicatorEl ) ;
81
-
82
67
expect ( onCaretMouseDownSpy ) . toBeCalled ( ) ;
83
68
} ) ;
84
69
85
70
test ( 'clear icon is not rendered and loading animation is rendered when "isLoading" = true' , async ( ) => {
86
- const { props } = createIndicatorIconsProps ( ) ;
87
- const mergedProps = { ...props , isLoading : true } ;
88
- const { queryByTestId } = renderIndicatorIcons ( mergedProps ) ;
71
+ const props = { ...BASE_PROPS , isLoading : true } ;
72
+ const { queryByTestId } = renderIndicatorIcons ( props ) ;
89
73
expect ( queryByTestId ( CLEAR_ICON_TESTID ! ) ) . toBeNull ( ) ;
90
74
} ) ;
91
75
92
76
test ( 'loading can render as a custom node (instead of default LoadingDots.tsx component)' , async ( ) => {
93
77
const loadingNodeText = 'loading-node' ;
94
78
const loadingNode = < span > { loadingNodeText } </ span > ;
95
- const { props } = createIndicatorIconsProps ( ) ;
96
79
97
- const mergedProps = {
98
- ...props ,
80
+ const props = {
81
+ ...BASE_PROPS ,
99
82
loadingNode,
100
83
isLoading : true ,
101
84
} ;
102
85
103
- const { getByText } = renderIndicatorIcons ( mergedProps ) ;
104
-
86
+ const { getByText } = renderIndicatorIcons ( props ) ;
105
87
expect ( getByText ( loadingNodeText ) ) . toBeInTheDocument ( ) ;
106
88
} ) ;
107
89
108
90
test ( 'clear icon can render as a ReactNode' , async ( ) => {
109
91
const clearIconText = 'clear-icon-node' ;
110
92
const clearIcon = < span > { clearIconText } </ span > ;
111
- const { props } = createIndicatorIconsProps ( ) ;
112
- const mergedProps = { ...props , clearIcon } ;
113
- const { getByText } = renderIndicatorIcons ( mergedProps ) ;
114
-
93
+ const props = { ...BASE_PROPS , clearIcon } ;
94
+ const { getByText } = renderIndicatorIcons ( props ) ;
115
95
expect ( getByText ( clearIconText ) ) . toBeInTheDocument ( ) ;
116
96
} ) ;
117
97
118
98
test ( 'clear icon can render as a callback function with return type of ReactNode - callback accepts forwarded state props from wrapping component.' , async ( ) => {
119
- const { props } = createIndicatorIconsProps ( ) ;
120
- const mergedProps = { ...props , menuOpen : true , clearIcon : customIconFn } ;
121
- const { getByTestId } = renderIndicatorIcons ( mergedProps ) ;
99
+ const props = {
100
+ ...BASE_PROPS ,
101
+ menuOpen : true ,
102
+ clearIcon : customIconFn
103
+ } ;
104
+
105
+ const { getByTestId } = renderIndicatorIcons ( props ) ;
122
106
123
107
// Build test-id from forwarded state javascript object payload
124
- const { menuOpen, isLoading, isInvalid, isDisabled } = mergedProps ;
108
+ const { menuOpen, isLoading, isInvalid, isDisabled } = props ;
125
109
const forwardedStateId = `${ menuOpen } -${ isLoading } -${ isInvalid } -${ isDisabled } `
126
-
127
110
expect ( getByTestId ( forwardedStateId ) ) . toBeInTheDocument ( ) ;
128
111
} ) ;
129
112
130
113
test ( 'caret icon can render as a ReactNode' , async ( ) => {
131
114
const caretIconText = 'caret-icon-node' ;
132
115
const caretIcon = < span > { caretIconText } </ span > ;
133
- const { props } = createIndicatorIconsProps ( ) ;
134
- const mergedProps = { ...props , caretIcon } ;
135
- const { getByText } = renderIndicatorIcons ( mergedProps ) ;
136
-
116
+ const props = { ...BASE_PROPS , caretIcon } ;
117
+ const { getByText } = renderIndicatorIcons ( props ) ;
137
118
expect ( getByText ( caretIconText ) ) . toBeInTheDocument ( ) ;
138
119
} ) ;
139
120
140
121
test ( 'caret icon can render as a callback function with return type of ReactNode - callback accepts forwarded state props from wrapping component.' , async ( ) => {
141
- const { props } = createIndicatorIconsProps ( ) ;
142
- const mergedProps = { ...props , menuOpen : true , caretIcon : customIconFn } ;
143
- const { getByTestId } = renderIndicatorIcons ( mergedProps ) ;
122
+ const props = { ...BASE_PROPS , menuOpen : true , caretIcon : customIconFn } ;
123
+ const { getByTestId } = renderIndicatorIcons ( props ) ;
144
124
145
125
// Build test-id from forwarded state javascript object payload
146
- const { menuOpen, isLoading, isInvalid, isDisabled } = mergedProps ;
126
+ const { menuOpen, isLoading, isInvalid, isDisabled } = props ;
147
127
const forwardedStateId = `${ menuOpen } -${ isLoading } -${ isInvalid } -${ isDisabled } `
148
-
149
128
expect ( getByTestId ( forwardedStateId ) ) . toBeInTheDocument ( ) ;
150
129
} ) ;
0 commit comments