From 756aa0ced336b28c70df15398619c066301161b7 Mon Sep 17 00:00:00 2001 From: zombiej Date: Mon, 6 Sep 2021 14:23:17 +0800 Subject: [PATCH 1/2] test: Test driven --- src/OptionList.tsx | 5 +++-- tests/Accessibility.test.tsx | 41 +++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/OptionList.tsx b/src/OptionList.tsx index fb8d3a8bd..ff81cc311 100644 --- a/src/OptionList.tsx +++ b/src/OptionList.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import { useEffect } from 'react'; import KeyCode from 'rc-util/lib/KeyCode'; import omit from 'rc-util/lib/omit'; import pickAttrs from 'rc-util/lib/pickAttrs'; @@ -140,12 +141,12 @@ const OptionList: React.ForwardRefRenderFunction< }; // Auto active first item when list length or searchValue changed - React.useEffect(() => { + useEffect(() => { setActive(defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1); }, [memoFlattenOptions.length, searchValue]); // Auto scroll to item position in single mode - React.useEffect(() => { + useEffect(() => { /** * React will skip `onChange` when component update. * `setActive` function will call root accessibility state update which makes re-render. diff --git a/tests/Accessibility.test.tsx b/tests/Accessibility.test.tsx index 0bcf1e8f6..92ae997d7 100644 --- a/tests/Accessibility.test.tsx +++ b/tests/Accessibility.test.tsx @@ -1,7 +1,8 @@ import { mount } from 'enzyme'; import * as React from 'react'; +import KeyCode from 'rc-util/lib/KeyCode'; import Select from '../src'; -import { injectRunAllTimers } from './utils/common'; +import { injectRunAllTimers, expectOpen } from './utils/common'; describe('Select.Accessibility', () => { injectRunAllTimers(jest); @@ -23,4 +24,42 @@ describe('Select.Accessibility', () => { }), ); }); + + it.only('active index should keep', () => { + const wrapper = mount( + { expectOpen(wrapper, false); // Next Match - wrapper.find('input').simulate('change', { target: { value: 'l' } }); + wrapper.find('input').simulate('change', { target: { value: '' } }); + wrapper.find('input').simulate('change', { target: { value: 'g' } }); jest.runAllTimers(); expectOpen(wrapper);