Skip to content

Commit 531b274

Browse files
author
Edward Xiao
committed
- test keyup by @testing-lirary/react
1 parent ffb66b3 commit 531b274

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@babel/preset-env": "^7.1.0",
6161
"@babel/preset-react": "^7.0.0",
6262
"@babel/preset-typescript": "^7.1.0",
63+
"@testing-library/react": "^9.3.2",
6364
"@types/jest": "^23.3.5",
6465
"@types/react": "^16.8.14",
6566
"@types/react-dom": "^16.8.4",

src/__tests__/Select.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState } from 'react';
2+
import { render, fireEvent } from '@testing-library/react';
23
import { expect as chaiExpect } from 'chai';
34
import { configure, mount } from 'enzyme';
45
import Adapter from 'enzyme-adapter-react-16';
@@ -9,7 +10,52 @@ configure({ adapter: new Adapter() });
910

1011
const 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

1460
describe('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);

src/js/Inputs/Select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ const component: React.FC<Props> = ({
398398
const newkeyCodeList = [...keycodeList, keyCode];
399399
const str = String.fromCharCode(...newkeyCodeList).toLowerCase();
400400
let index = -1;
401-
optionList.filter((i, k) => {
401+
optionList.forEach((i, k) => {
402402
const { name } = i;
403403
if (name.toLowerCase().startsWith(str)) {
404404
if (index === -1) {

0 commit comments

Comments
 (0)