From 4809735b0a655f8a2df9e80488f80973f7859667 Mon Sep 17 00:00:00 2001 From: Maxime Gendron Date: Fri, 8 May 2020 14:49:27 -0400 Subject: [PATCH] Add more tests --- .../src/components/select/select.test.tsx | 200 +++++++++++++++++- .../components/select/select.test.tsx.snap | 12 ++ .../react/src/components/select/select.tsx | 32 +-- 3 files changed, 228 insertions(+), 16 deletions(-) diff --git a/packages/react/src/components/select/select.test.tsx b/packages/react/src/components/select/select.test.tsx index 31c5053821..dda75ae2d5 100644 --- a/packages/react/src/components/select/select.test.tsx +++ b/packages/react/src/components/select/select.test.tsx @@ -32,10 +32,208 @@ describe('Select', () => { const callback = jest.fn(); const wrapper = shallow(); + + expect(getByTestId(wrapper, 'listbox').props().visible).toBeFalsy(); + }); + + test('listbox should be open when defaultOpen prop is set to true', () => { + const wrapper = shallow(); + + expect(getByTestId(wrapper, 'input').props().value).toBe('Quebec'); + }); + + test('listbox should open on click', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input-wrapper').simulate('click'); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeFalsy(); + }); + + test('listbox should close on click', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'arrow').simulate('click'); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeTruthy(); + }); + + test('listbox should close when arrow is clicked', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('keydown', { keyCode: 38 }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeTruthy(); + }); + + test('ArrowUp should open listbox [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('keydown', { keyCode: 40 }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeTruthy(); + }); + + test('ArrowDown should open listbox [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('keydown', { keyCode: 13, preventDefault: jest.fn() }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeTruthy(); + }); + + test('ArrowDown should not open listbox [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('keydown', { keyCode: 32, preventDefault: jest.fn() }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeTruthy(); + }); + + test('Spacebar should open listbox [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('keydown', { keyCode: 27 }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeFalsy(); + }); + + test('Escape should close listbox [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('keydown', { keyCode: 27 }); + expect(getByTestId(wrapper, 'input').props().value).toBe(''); + }); + + test('listbox should open when search entry has matching result(s) [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'input').simulate('change', { target: { value: 'at' } }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeFalsy(); + }); + + test('should select value that matches search value on blur [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'listbox').simulate('keydown', { key: 'Escape' }); + expect(getByTestId(wrapper, 'listbox').props().visible).toBeFalsy(); + }); + + test('should clear input value when escape is pressed [searchable]', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'listbox').simulate('keydown', { key: 'ArrowUp' }); + expect(getByTestId(wrapper, 'listbox').props().focusedValue).toBe('yt'); + }); + + test('should focus the first element starting with the letter N', () => { + const wrapper = shallow(); + + getByTestId(wrapper, 'listbox').simulate('keydown', { keyCode: 78, key: 'n' }); + expect(getByTestId(wrapper, 'listbox').props().autofocus).toBeFalsy(); + }); + + test('should close onChange', () => { + const wrapper = shallow(