Skip to content

Commit be7a193

Browse files
test(core): test getInputProps Enter behavior
1 parent ea09d8d commit be7a193

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

packages/autocomplete-core/src/__tests__/getInputProps.test.ts

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,75 @@ describe('getInputProps', () => {
791791
});
792792

793793
describe('Enter', () => {
794-
test.todo('is a noop without selectedItemId');
794+
test('is a noop without selectedItemId', () => {
795+
const onStateChange = jest.fn();
796+
const { inputProps } = createPlayground(createAutocomplete, {
797+
onStateChange,
798+
initialState: {
799+
isOpen: true,
800+
selectedItemId: null,
801+
collections: [
802+
createCollection({
803+
items: [{ label: '1' }, { label: '2' }],
804+
}),
805+
],
806+
},
807+
});
795808

796-
test.todo('is a noop with empty collections');
809+
const stateChanges = (onStateChange.mock.calls[0] || []).length;
810+
const event = {
811+
...new KeyboardEvent('keydown'),
812+
key: 'Enter',
813+
};
814+
inputProps.onKeyDown(event);
815+
816+
expect(onStateChange).toHaveBeenCalledTimes(stateChanges);
817+
});
818+
819+
test('is a noop with empty collections', () => {
820+
const onStateChange = jest.fn();
821+
const { inputProps } = createPlayground(createAutocomplete, {
822+
onStateChange,
823+
initialState: {
824+
isOpen: true,
825+
selectedItemId: 0,
826+
collections: [],
827+
},
828+
});
797829

798-
test.todo('prevents the default event behavior');
830+
const stateChanges = (onStateChange.mock.calls[0] || []).length;
831+
const event = {
832+
...new KeyboardEvent('keydown'),
833+
key: 'Enter',
834+
};
835+
inputProps.onKeyDown(event);
836+
837+
expect(onStateChange).toHaveBeenCalledTimes(stateChanges);
838+
});
839+
840+
test('prevents the default event behavior', () => {
841+
const { inputProps } = createPlayground(createAutocomplete, {
842+
openOnFocus: true,
843+
initialState: {
844+
selectedItemId: 0,
845+
collections: [
846+
createCollection({
847+
items: [{ label: '1' }, { label: '2' }],
848+
}),
849+
],
850+
},
851+
});
852+
853+
const event = {
854+
...new KeyboardEvent('keydown'),
855+
key: 'Enter',
856+
preventDefault: jest.fn(),
857+
};
858+
859+
inputProps.onKeyDown(event);
860+
861+
expect(event.preventDefault).toHaveBeenCalledTimes(1);
862+
});
799863

800864
describe('Plain Enter', () => {
801865
test.todo('calls onSelect with item URL');

0 commit comments

Comments
 (0)