Skip to content

Commit 50f2d08

Browse files
add a dedicated autoSelect test describe
1 parent 5e1fbec commit 50f2d08

File tree

2 files changed

+50
-78
lines changed

2 files changed

+50
-78
lines changed

packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ describe('<Autocomplete />', () => {
2929

3030
describe('combobox', () => {
3131
it('should clear the input when blur', () => {
32-
const { container } = render(
32+
const { getByRole } = render(
3333
<Autocomplete renderInput={params => <TextField {...params} />} />,
3434
);
35-
const input = container.querySelector('input');
35+
const input = getByRole('textbox');
3636
input.focus();
3737
fireEvent.change(document.activeElement, { target: { value: 'a' } });
3838
expect(input.value).to.equal('a');
@@ -49,12 +49,51 @@ describe('<Autocomplete />', () => {
4949
});
5050
});
5151

52-
describe('multiple', () => {
52+
describe('prop: autoSelect', () => {
53+
it('should add new value when autoSelect & multiple on blur', () => {
54+
const handleChange = spy();
55+
const options = ['one', 'two'];
56+
render(
57+
<Autocomplete
58+
autoSelect
59+
multiple
60+
value={[options[0]]}
61+
options={options}
62+
onChange={handleChange}
63+
renderInput={params => <TextField autoFocus {...params} />}
64+
/>,
65+
);
66+
fireEvent.change(document.activeElement, { target: { value: 't' } });
67+
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
68+
document.activeElement.blur();
69+
expect(handleChange.callCount).to.equal(1);
70+
expect(handleChange.args[0][1]).to.deep.equal(options);
71+
});
72+
73+
it('should add new value when autoSelect & multiple & freeSolo on blur', () => {
74+
const handleChange = spy();
75+
render(
76+
<Autocomplete
77+
autoSelect
78+
freeSolo
79+
multiple
80+
onChange={handleChange}
81+
renderInput={params => <TextField autoFocus {...params} />}
82+
/>,
83+
);
84+
fireEvent.change(document.activeElement, { target: { value: 'a' } });
85+
document.activeElement.blur();
86+
expect(handleChange.callCount).to.equal(1);
87+
expect(handleChange.args[0][1]).to.deep.equal(['a']);
88+
});
89+
});
90+
91+
describe('prop: multiple', () => {
5392
it('should not crash', () => {
54-
const { container } = render(
93+
const { getByRole } = render(
5594
<Autocomplete renderInput={params => <TextField {...params} />} multiple />,
5695
);
57-
const input = container.querySelector('input');
96+
const input = getByRole('textbox');
5897
input.focus();
5998
document.activeElement.blur();
6099
input.focus();
@@ -98,74 +137,6 @@ describe('<Autocomplete />', () => {
98137
expect(handleChange.args[0][1]).to.deep.equal([options[1]]);
99138
expect(document.activeElement).to.equal(getByRole('textbox'));
100139
});
101-
102-
it('should add new value when freeSolo & multiple on blur', () => {
103-
const handleChange = spy();
104-
const { container } = render(
105-
<Autocomplete
106-
freeSolo
107-
multiple
108-
onChange={handleChange}
109-
renderInput={params => <TextField {...params} />}
110-
/>,
111-
);
112-
const input = container.querySelector('input');
113-
input.focus();
114-
fireEvent.change(document.activeElement, { target: { value: 'a' } });
115-
document.activeElement.blur();
116-
expect(handleChange.callCount).to.equal(1);
117-
expect(handleChange.args[0][1]).to.deep.equal(['a']);
118-
});
119-
120-
it('should not add new value on blur if value is empty', () => {
121-
const handleChange = spy();
122-
const { container } = render(
123-
<Autocomplete
124-
freeSolo
125-
multiple
126-
onChange={handleChange}
127-
renderInput={params => <TextField {...params} />}
128-
/>,
129-
);
130-
const input = container.querySelector('input');
131-
input.focus();
132-
fireEvent.change(document.activeElement, { target: { value: '' } });
133-
document.activeElement.blur();
134-
expect(handleChange.callCount).to.equal(0);
135-
});
136-
137-
it('should not add new value on blur without freeSolo', () => {
138-
const handleChange = spy();
139-
const { container } = render(
140-
<Autocomplete
141-
multiple
142-
onChange={handleChange}
143-
renderInput={params => <TextField {...params} />}
144-
/>,
145-
);
146-
const input = container.querySelector('input');
147-
input.focus();
148-
fireEvent.change(document.activeElement, { target: { value: 'a' } });
149-
document.activeElement.blur();
150-
expect(handleChange.callCount).to.equal(0);
151-
});
152-
153-
it('should not add new value on blur without multiple', () => {
154-
const handleChange = spy();
155-
const { container } = render(
156-
<Autocomplete
157-
freeSolo
158-
onChange={handleChange}
159-
renderInput={params => <TextField {...params} />}
160-
/>,
161-
);
162-
const input = container.querySelector('input');
163-
input.focus();
164-
fireEvent.change(document.activeElement, { target: { value: 'a' } });
165-
document.activeElement.blur();
166-
expect(input.value).to.equal('a');
167-
expect(handleChange.args[0][1]).to.deep.equal('a');
168-
});
169140
});
170141

171142
it('should trigger a form expectedly', () => {
@@ -592,14 +563,15 @@ describe('<Autocomplete />', () => {
592563

593564
describe('prop: disabled', () => {
594565
it('should disable the input', () => {
595-
const { container } = render(
566+
const { getByRole } = render(
596567
<Autocomplete
597568
disabled
598569
options={['one', 'two', 'three']}
599570
renderInput={params => <TextField {...params} />}
600571
/>,
601572
);
602-
expect(container.querySelector('input').disabled).to.be.true;
573+
const input = getByRole('textbox');
574+
expect(input.disabled).to.be.true;
603575
});
604576

605577
it('should disable the popup button', () => {
@@ -713,14 +685,14 @@ describe('<Autocomplete />', () => {
713685

714686
it('should not select undefined ', () => {
715687
const handleChange = spy();
716-
const { container, getByRole } = render(
688+
const { getByRole } = render(
717689
<Autocomplete
718690
onChange={handleChange}
719691
options={['one', 'two']}
720692
renderInput={params => <TextField {...params} />}
721693
/>,
722694
);
723-
const input = container.querySelector('input');
695+
const input = getByRole('textbox');
724696
fireEvent.click(input);
725697

726698
const listbox = getByRole('listbox');

packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ export default function useAutocomplete(props) {
654654

655655
if (autoSelect && selectedIndexRef.current !== -1) {
656656
selectNewValue(event, filteredOptions[selectedIndexRef.current]);
657-
} else if (freeSolo && inputValue !== '') {
657+
} else if (autoSelect && freeSolo && inputValue !== '') {
658658
selectNewValue(event, inputValue, 'freeSolo');
659659
} else if (!freeSolo) {
660660
resetInputValue(event, value);

0 commit comments

Comments
 (0)