|
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'; |
2 | 9 | import { createAutocomplete } from '../createAutocomplete'; |
3 | 10 |
|
4 | 11 | 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 | + }); |
6 | 36 |
|
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 | + }); |
8 | 61 |
|
9 | 62 | test('notifies plugins when onStateChange', () => { |
10 | 63 | const onStateChange = jest.fn(); |
|
0 commit comments