From 4517204aee2ddb7f9c9597534bc37316bd437bd6 Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 25 Jan 2020 17:38:30 +0100 Subject: [PATCH 1/4] Initial --- .../material-ui-lab/src/useAutocomplete/useAutocomplete.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index 031f372f283564..99a1d31bb37da7 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -653,7 +653,9 @@ export default function useAutocomplete(props) { } if (autoSelect && selectedIndexRef.current !== -1) { - handleValue(event, filteredOptions[selectedIndexRef.current]); + selectNewValue(event, filteredOptions[selectedIndexRef.current]); + } else if (freeSolo && inputValue !== '') { + selectNewValue(event, inputValue, 'freeSolo'); } else if (!freeSolo) { resetInputValue(event, value); } From f05ad0c6472a4a9a59d23666799ad5108855a639 Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 25 Jan 2020 21:48:32 +0100 Subject: [PATCH 2/4] Add unit tests --- .../src/Autocomplete/Autocomplete.test.js | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index f73305a3fa11e9..300e62900cc63f 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -98,6 +98,56 @@ describe('', () => { expect(handleChange.args[0][1]).to.deep.equal([options[1]]); expect(document.activeElement).to.equal(getByRole('textbox')); }); + + it('should add new value when freeSolo & multiple on blur', () => { + const handleChange = spy(); + const { container } = render( + } />, + ); + const input = container.querySelector('input'); + input.focus(); + fireEvent.change(document.activeElement, { target: { value: 'a' } }); + document.activeElement.blur(); + expect(handleChange.callCount).to.equal(1); + expect(handleChange.args[0][1]).to.deep.equal(['a']); + }); + + it('should not add new value on blur if value is empty', () => { + const handleChange = spy(); + const { container } = render( + } />, + ); + const input = container.querySelector('input'); + input.focus(); + fireEvent.change(document.activeElement, { target: { value: '' } }); + document.activeElement.blur(); + expect(handleChange.callCount).to.equal(0); + }); + + it('should not add new value on blur without freeSolo', () => { + const handleChange = spy(); + const { container } = render( + } />, + ); + const input = container.querySelector('input'); + input.focus(); + fireEvent.change(document.activeElement, { target: { value: 'a' } }); + document.activeElement.blur(); + expect(handleChange.callCount).to.equal(0); + }); + + it('should not add new value on blur without multiple', () => { + const handleChange = spy(); + const { container } = render( + } />, + ); + const input = container.querySelector('input'); + input.focus(); + fireEvent.change(document.activeElement, { target: { value: 'a' } }); + document.activeElement.blur(); + expect(input.value).to.equal('a'); + expect(handleChange.args[0][1]).to.deep.equal('a'); + }); }); it('should trigger a form expectedly', () => { From 5e1fbecd6fa785e0355f2cc3ac0a9eb3c520b075 Mon Sep 17 00:00:00 2001 From: Serhii Date: Sat, 25 Jan 2020 21:57:25 +0100 Subject: [PATCH 3/4] pretier --- .../src/Autocomplete/Autocomplete.test.js | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index 300e62900cc63f..0f161c95169ef1 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -102,7 +102,12 @@ describe('', () => { it('should add new value when freeSolo & multiple on blur', () => { const handleChange = spy(); const { container } = render( - } />, + } + />, ); const input = container.querySelector('input'); input.focus(); @@ -115,7 +120,12 @@ describe('', () => { it('should not add new value on blur if value is empty', () => { const handleChange = spy(); const { container } = render( - } />, + } + />, ); const input = container.querySelector('input'); input.focus(); @@ -127,7 +137,11 @@ describe('', () => { it('should not add new value on blur without freeSolo', () => { const handleChange = spy(); const { container } = render( - } />, + } + />, ); const input = container.querySelector('input'); input.focus(); @@ -139,7 +153,11 @@ describe('', () => { it('should not add new value on blur without multiple', () => { const handleChange = spy(); const { container } = render( - } />, + } + />, ); const input = container.querySelector('input'); input.focus(); From 50f2d084ffd71daceb0873e8ccd3b29aeb2dfb06 Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Sat, 25 Jan 2020 23:25:36 +0100 Subject: [PATCH 4/4] add a dedicated autoSelect test describe --- .../src/Autocomplete/Autocomplete.test.js | 126 +++++++----------- .../src/useAutocomplete/useAutocomplete.js | 2 +- 2 files changed, 50 insertions(+), 78 deletions(-) diff --git a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js index 0f161c95169ef1..633763d7d4ec53 100644 --- a/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js +++ b/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js @@ -29,10 +29,10 @@ describe('', () => { describe('combobox', () => { it('should clear the input when blur', () => { - const { container } = render( + const { getByRole } = render( } />, ); - const input = container.querySelector('input'); + const input = getByRole('textbox'); input.focus(); fireEvent.change(document.activeElement, { target: { value: 'a' } }); expect(input.value).to.equal('a'); @@ -49,12 +49,51 @@ describe('', () => { }); }); - describe('multiple', () => { + describe('prop: autoSelect', () => { + it('should add new value when autoSelect & multiple on blur', () => { + const handleChange = spy(); + const options = ['one', 'two']; + render( + } + />, + ); + fireEvent.change(document.activeElement, { target: { value: 't' } }); + fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' }); + document.activeElement.blur(); + expect(handleChange.callCount).to.equal(1); + expect(handleChange.args[0][1]).to.deep.equal(options); + }); + + it('should add new value when autoSelect & multiple & freeSolo on blur', () => { + const handleChange = spy(); + render( + } + />, + ); + fireEvent.change(document.activeElement, { target: { value: 'a' } }); + document.activeElement.blur(); + expect(handleChange.callCount).to.equal(1); + expect(handleChange.args[0][1]).to.deep.equal(['a']); + }); + }); + + describe('prop: multiple', () => { it('should not crash', () => { - const { container } = render( + const { getByRole } = render( } multiple />, ); - const input = container.querySelector('input'); + const input = getByRole('textbox'); input.focus(); document.activeElement.blur(); input.focus(); @@ -98,74 +137,6 @@ describe('', () => { expect(handleChange.args[0][1]).to.deep.equal([options[1]]); expect(document.activeElement).to.equal(getByRole('textbox')); }); - - it('should add new value when freeSolo & multiple on blur', () => { - const handleChange = spy(); - const { container } = render( - } - />, - ); - const input = container.querySelector('input'); - input.focus(); - fireEvent.change(document.activeElement, { target: { value: 'a' } }); - document.activeElement.blur(); - expect(handleChange.callCount).to.equal(1); - expect(handleChange.args[0][1]).to.deep.equal(['a']); - }); - - it('should not add new value on blur if value is empty', () => { - const handleChange = spy(); - const { container } = render( - } - />, - ); - const input = container.querySelector('input'); - input.focus(); - fireEvent.change(document.activeElement, { target: { value: '' } }); - document.activeElement.blur(); - expect(handleChange.callCount).to.equal(0); - }); - - it('should not add new value on blur without freeSolo', () => { - const handleChange = spy(); - const { container } = render( - } - />, - ); - const input = container.querySelector('input'); - input.focus(); - fireEvent.change(document.activeElement, { target: { value: 'a' } }); - document.activeElement.blur(); - expect(handleChange.callCount).to.equal(0); - }); - - it('should not add new value on blur without multiple', () => { - const handleChange = spy(); - const { container } = render( - } - />, - ); - const input = container.querySelector('input'); - input.focus(); - fireEvent.change(document.activeElement, { target: { value: 'a' } }); - document.activeElement.blur(); - expect(input.value).to.equal('a'); - expect(handleChange.args[0][1]).to.deep.equal('a'); - }); }); it('should trigger a form expectedly', () => { @@ -592,14 +563,15 @@ describe('', () => { describe('prop: disabled', () => { it('should disable the input', () => { - const { container } = render( + const { getByRole } = render( } />, ); - expect(container.querySelector('input').disabled).to.be.true; + const input = getByRole('textbox'); + expect(input.disabled).to.be.true; }); it('should disable the popup button', () => { @@ -713,14 +685,14 @@ describe('', () => { it('should not select undefined ', () => { const handleChange = spy(); - const { container, getByRole } = render( + const { getByRole } = render( } />, ); - const input = container.querySelector('input'); + const input = getByRole('textbox'); fireEvent.click(input); const listbox = getByRole('listbox'); diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js index 99a1d31bb37da7..6e5cfcb14e6db4 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js @@ -654,7 +654,7 @@ export default function useAutocomplete(props) { if (autoSelect && selectedIndexRef.current !== -1) { selectNewValue(event, filteredOptions[selectedIndexRef.current]); - } else if (freeSolo && inputValue !== '') { + } else if (autoSelect && freeSolo && inputValue !== '') { selectNewValue(event, inputValue, 'freeSolo'); } else if (!freeSolo) { resetInputValue(event, value);