Skip to content

Commit 3475d12

Browse files
authored
Add tests for onActive and onSelect on plugins (#400)
1 parent 8bd35e6 commit 3475d12

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,63 @@
1-
import { createPlayground } from '../../../../test/utils';
1+
import userEvent from '@testing-library/user-event';
2+
3+
import {
4+
createCollection,
5+
createPlayground,
6+
createSource,
7+
runAllMicroTasks,
8+
} from '../../../../test/utils';
29
import { createAutocomplete } from '../createAutocomplete';
310

411
describe('plugins', () => {
5-
test.todo('notifies plugins when onSelect');
12+
test('notifies plugins when onActive', async () => {
13+
const onActive = jest.fn();
14+
const plugin = { onActive };
15+
const { inputElement } = createPlayground(createAutocomplete, {
16+
defaultActiveItemId: 0,
17+
plugins: [plugin],
18+
getSources: () => {
19+
return [
20+
createSource({
21+
getItems() {
22+
return [{ label: '1' }, { label: '2' }];
23+
},
24+
onActive,
25+
}),
26+
];
27+
},
28+
});
29+
30+
userEvent.type(inputElement, 'a');
31+
32+
await runAllMicroTasks();
33+
expect(onActive).toHaveBeenCalledTimes(1);
34+
expect(plugin.onActive).toHaveBeenCalledTimes(1);
35+
});
636

7-
test.todo('notifies plugins when onActive');
37+
test('notifies plugins when onSelect', async () => {
38+
const onSelect = jest.fn();
39+
const plugin = { onSelect };
40+
const { inputElement } = createPlayground(createAutocomplete, {
41+
defaultActiveItemId: 0,
42+
plugins: [plugin],
43+
initialState: {
44+
isOpen: true,
45+
collections: [
46+
createCollection({
47+
source: { onSelect },
48+
items: [{ label: '1' }, { label: '2' }],
49+
}),
50+
],
51+
},
52+
});
53+
54+
inputElement.focus();
55+
userEvent.type(inputElement, '{enter}');
56+
57+
await runAllMicroTasks();
58+
expect(onSelect).toHaveBeenCalledTimes(1);
59+
expect(plugin.onSelect).toHaveBeenCalledTimes(1);
60+
});
861

962
test('notifies plugins when onStateChange', () => {
1063
const onStateChange = jest.fn();

0 commit comments

Comments
 (0)