11import React , { useState } from 'react' ;
2+ import { render , fireEvent } from '@testing-library/react' ;
23import { expect as chaiExpect } from 'chai' ;
34import { configure , mount } from 'enzyme' ;
45import Adapter from 'enzyme-adapter-react-16' ;
@@ -9,7 +10,52 @@ configure({ adapter: new Adapter() });
910
1011const WRAPPER = `.${ WRAPPER_CLASS_IDENTITIFIER } ` ;
1112
12- const OPTION_LIST = [ { id : '' , name : 'Please select one country' } , { id : 'us' , name : 'US' } , { id : 'ca' , name : 'CA' } , { id : 'uk' , name : 'UK' } , { id : 'fr' , name : 'France' } ] ;
13+ const OPTION_LIST = [
14+ { id : '' , name : 'Please select one country' } ,
15+ { id : 'us' , name : 'US' } ,
16+ { id : 'ca' , name : 'CA' } ,
17+ { id : 'uk' , name : 'UK' } ,
18+ { id : 'fr' , name : 'France' } ,
19+ ] ;
20+
21+ describe ( 'Select component keydown' , ( ) => {
22+ it ( 'keydown' , ( ) => {
23+ // sequence matters for some reason
24+ const div = document . createElement ( 'div' ) ;
25+ document . body . appendChild ( div ) ;
26+ let value = '' ;
27+ const { container } = render (
28+ < Select
29+ value = { '' }
30+ onBlur = { ( ) => { } }
31+ optionList = { OPTION_LIST }
32+ onChange = { item => {
33+ value = item . id ;
34+ } }
35+ attributesWrapper = { { id : 'wrapper' } }
36+ attributesInput = { { id : 'input' } }
37+ /> ,
38+ div ,
39+ ) ;
40+ const $wrapper = document . querySelector ( WRAPPER ) ;
41+ fireEvent . click ( $wrapper ) ;
42+ const arrowDown = new KeyboardEvent ( 'keydown' , {
43+ bubbles : true ,
44+ key : 'ArrowDown' ,
45+ keyCode : 40 ,
46+ which : 40 ,
47+ } ) ;
48+ const enter = new KeyboardEvent ( 'keydown' , {
49+ bubbles : true ,
50+ key : 'Enter' ,
51+ keyCode : 13 ,
52+ which : 13 ,
53+ } ) ;
54+ fireEvent ( container , arrowDown ) ;
55+ fireEvent ( container , enter ) ;
56+ expect ( value ) . toEqual ( OPTION_LIST [ 1 ] . id ) ;
57+ } ) ;
58+ } ) ;
1359
1460describe ( 'Select component' , ( ) => {
1561 it ( '[Toggling "validate"]: Should show msgHtml(err) when toggling "validate"' , ( ) => {
@@ -239,8 +285,14 @@ describe('Select component', () => {
239285 ) . toEqual ( 'success' ) ;
240286 } ) ;
241287
242- const LIST1 = [ { id : 'us' , name : 'US' } , { id : 'ca' , name : 'CA' } ] ;
243- const LIST2 = [ { id : 'uk' , name : 'UK' } , { id : 'fr' , name : 'France' } ] ;
288+ const LIST1 = [
289+ { id : 'us' , name : 'US' } ,
290+ { id : 'ca' , name : 'CA' } ,
291+ ] ;
292+ const LIST2 = [
293+ { id : 'uk' , name : 'UK' } ,
294+ { id : 'fr' , name : 'France' } ,
295+ ] ;
244296 const MyComponent = ( ) => {
245297 const stateOptionList = useState ( LIST1 ) ;
246298 const stateCurId = useState ( LIST1 [ 0 ] . id ) ;
0 commit comments