@@ -2,11 +2,14 @@ import { mount } from 'enzyme';
2
2
import KeyCode from 'rc-util/lib/KeyCode' ;
3
3
import { act } from 'react-dom/test-utils' ;
4
4
import React from 'react' ;
5
- import OptionList , { OptionListProps , RefOptionListProps } from '../src/OptionList' ;
5
+ import type { OptionListProps , RefOptionListProps } from '../src/OptionList' ;
6
+ import OptionList from '../src/OptionList' ;
6
7
import { injectRunAllTimers } from './utils/common' ;
7
- import { OptionsType } from '../src/interface' ;
8
+ import type { OptionsType } from '../src/interface' ;
8
9
import { flattenOptions } from '../src/utils/valueUtil' ;
9
10
11
+ jest . mock ( '../src/utils/platformUtil' ) ;
12
+
10
13
describe ( 'OptionList' , ( ) => {
11
14
injectRunAllTimers ( jest ) ;
12
15
@@ -103,6 +106,39 @@ describe('OptionList', () => {
103
106
) ;
104
107
} ) ;
105
108
109
+ // mocked how we detect running platform in test environment
110
+ it ( 'special key operation on Mac' , ( ) => {
111
+ const onActiveValue = jest . fn ( ) ;
112
+ const listRef = React . createRef < RefOptionListProps > ( ) ;
113
+ mount (
114
+ generateList ( {
115
+ options : [ { value : '1' } , { value : '2' } ] ,
116
+ onActiveValue,
117
+ ref : listRef ,
118
+ } ) ,
119
+ ) ;
120
+
121
+ onActiveValue . mockReset ( ) ;
122
+ act ( ( ) => {
123
+ listRef . current . onKeyDown ( { which : KeyCode . N , ctrlKey : true } as any ) ;
124
+ } ) ;
125
+ expect ( onActiveValue ) . toHaveBeenCalledWith (
126
+ '2' ,
127
+ expect . anything ( ) ,
128
+ expect . objectContaining ( { source : 'keyboard' } ) ,
129
+ ) ;
130
+
131
+ onActiveValue . mockReset ( ) ;
132
+ act ( ( ) => {
133
+ listRef . current . onKeyDown ( { which : KeyCode . P , ctrlKey : true } as any ) ;
134
+ } ) ;
135
+ expect ( onActiveValue ) . toHaveBeenCalledWith (
136
+ '1' ,
137
+ expect . anything ( ) ,
138
+ expect . objectContaining ( { source : 'keyboard' } ) ,
139
+ ) ;
140
+ } ) ;
141
+
106
142
it ( 'hover to active' , ( ) => {
107
143
const onActiveValue = jest . fn ( ) ;
108
144
const wrapper = mount (
@@ -113,10 +149,7 @@ describe('OptionList', () => {
113
149
) ;
114
150
115
151
onActiveValue . mockReset ( ) ;
116
- wrapper
117
- . find ( '.rc-select-item-option' )
118
- . last ( )
119
- . simulate ( 'mouseMove' ) ;
152
+ wrapper . find ( '.rc-select-item-option' ) . last ( ) . simulate ( 'mouseMove' ) ;
120
153
expect ( onActiveValue ) . toHaveBeenCalledWith (
121
154
'2' ,
122
155
expect . anything ( ) ,
@@ -125,10 +158,7 @@ describe('OptionList', () => {
125
158
126
159
// Same item not repeat trigger
127
160
onActiveValue . mockReset ( ) ;
128
- wrapper
129
- . find ( '.rc-select-item-option' )
130
- . last ( )
131
- . simulate ( 'mouseMove' ) ;
161
+ wrapper . find ( '.rc-select-item-option' ) . last ( ) . simulate ( 'mouseMove' ) ;
132
162
expect ( onActiveValue ) . not . toHaveBeenCalled ( ) ;
133
163
} ) ;
134
164
@@ -140,29 +170,24 @@ describe('OptionList', () => {
140
170
) ;
141
171
142
172
const preventDefault = jest . fn ( ) ;
143
- wrapper
144
- . find ( '.rc-select-item-option' )
145
- . last ( )
146
- . simulate ( 'mouseDown' , {
147
- preventDefault,
148
- } ) ;
173
+ wrapper . find ( '.rc-select-item-option' ) . last ( ) . simulate ( 'mouseDown' , {
174
+ preventDefault,
175
+ } ) ;
149
176
150
177
expect ( preventDefault ) . toHaveBeenCalled ( ) ;
151
178
} ) ;
152
179
153
180
it ( 'Data attributes should be set correct' , ( ) => {
154
181
const wrapper = mount (
155
182
generateList ( {
156
- options : [ { value : '1' , label : 'my-label' } , { value : '2' , 'data-num' : '123' } ] ,
183
+ options : [
184
+ { value : '1' , label : 'my-label' } ,
185
+ { value : '2' , 'data-num' : '123' } ,
186
+ ] ,
157
187
} ) ,
158
188
) ;
159
189
160
- expect (
161
- wrapper
162
- . find ( '.rc-select-item-option' )
163
- . last ( )
164
- . prop ( 'data-num' ) ,
165
- ) . toBe ( '123' ) ;
190
+ expect ( wrapper . find ( '.rc-select-item-option' ) . last ( ) . prop ( 'data-num' ) ) . toBe ( '123' ) ;
166
191
} ) ;
167
192
168
193
it ( 'should render title defaultly' , ( ) => {
@@ -171,12 +196,7 @@ describe('OptionList', () => {
171
196
options : [ { value : '1' , label : 'my-label' } ] ,
172
197
} ) ,
173
198
) ;
174
- expect (
175
- wrapper
176
- . find ( '.rc-select-item-option' )
177
- . first ( )
178
- . prop ( 'title' ) ,
179
- ) . toBe ( 'my-label' ) ;
199
+ expect ( wrapper . find ( '.rc-select-item-option' ) . first ( ) . prop ( 'title' ) ) . toBe ( 'my-label' ) ;
180
200
} ) ;
181
201
182
202
it ( 'should render title' , ( ) => {
@@ -185,12 +205,7 @@ describe('OptionList', () => {
185
205
options : [ { value : '1' , label : 'my-label' , title : 'title' } ] ,
186
206
} ) ,
187
207
) ;
188
- expect (
189
- wrapper
190
- . find ( '.rc-select-item-option' )
191
- . first ( )
192
- . prop ( 'title' ) ,
193
- ) . toBe ( 'title' ) ;
208
+ expect ( wrapper . find ( '.rc-select-item-option' ) . first ( ) . prop ( 'title' ) ) . toBe ( 'title' ) ;
194
209
} ) ;
195
210
196
211
it ( 'should not render title when title is empty string' , ( ) => {
@@ -199,12 +214,7 @@ describe('OptionList', () => {
199
214
options : [ { value : '1' , label : 'my-label' , title : '' } ] ,
200
215
} ) ,
201
216
) ;
202
- expect (
203
- wrapper
204
- . find ( '.rc-select-item-option' )
205
- . first ( )
206
- . prop ( 'title' ) ,
207
- ) . toBe ( '' ) ;
217
+ expect ( wrapper . find ( '.rc-select-item-option' ) . first ( ) . prop ( 'title' ) ) . toBe ( '' ) ;
208
218
} ) ;
209
219
210
220
it ( 'should render title from label when title is undefined' , ( ) => {
@@ -213,12 +223,7 @@ describe('OptionList', () => {
213
223
options : [ { value : '1' , label : 'my-label' , title : undefined } ] ,
214
224
} ) ,
215
225
) ;
216
- expect (
217
- wrapper
218
- . find ( '.rc-select-item-option' )
219
- . first ( )
220
- . prop ( 'title' ) ,
221
- ) . toBe ( 'my-label' ) ;
226
+ expect ( wrapper . find ( '.rc-select-item-option' ) . first ( ) . prop ( 'title' ) ) . toBe ( 'my-label' ) ;
222
227
} ) ;
223
228
224
229
it ( 'should not render title defaultly when label is ReactNode' , ( ) => {
@@ -227,11 +232,6 @@ describe('OptionList', () => {
227
232
options : [ { value : '1' , label : < div > label</ div > } ] ,
228
233
} ) ,
229
234
) ;
230
- expect (
231
- wrapper
232
- . find ( '.rc-select-item-option' )
233
- . first ( )
234
- . prop ( 'title' ) ,
235
- ) . toBe ( undefined ) ;
235
+ expect ( wrapper . find ( '.rc-select-item-option' ) . first ( ) . prop ( 'title' ) ) . toBe ( undefined ) ;
236
236
} ) ;
237
237
} ) ;
0 commit comments