|
13 | 13 | import {act} from '@testing-library/react';
|
14 | 14 | import {Button, ComboBox, ComboBoxContext, FieldError, Header, Input, Label, ListBox, ListBoxItem, ListBoxLoadMoreItem, ListBoxSection, ListLayout, Popover, Text, Virtualizer} from '../';
|
15 | 15 | import {fireEvent, pointerMap, render, within} from '@react-spectrum/test-utils-internal';
|
16 |
| -import React from 'react'; |
| 16 | +import React, {useState} from 'react'; |
17 | 17 | import {User} from '@react-aria/test-utils';
|
18 | 18 | import userEvent from '@testing-library/user-event';
|
19 | 19 |
|
@@ -408,4 +408,57 @@ describe('ComboBox', () => {
|
408 | 408 | expect(comboboxTester.listbox).toBeTruthy();
|
409 | 409 | expect(options[0]).toHaveTextContent('No results');
|
410 | 410 | });
|
| 411 | + |
| 412 | + it('should support onAction', async () => { |
| 413 | + let onAction = jest.fn(); |
| 414 | + function WithCreateOption() { |
| 415 | + let [inputValue, setInputValue] = useState(''); |
| 416 | + |
| 417 | + return ( |
| 418 | + <ComboBox |
| 419 | + allowsEmptyCollection |
| 420 | + inputValue={inputValue} |
| 421 | + onInputChange={setInputValue}> |
| 422 | + <Label style={{display: 'block'}}>Favorite Animal</Label> |
| 423 | + <div style={{display: 'flex'}}> |
| 424 | + <Input /> |
| 425 | + <Button> |
| 426 | + <span aria-hidden="true" style={{padding: '0 2px'}}>▼</span> |
| 427 | + </Button> |
| 428 | + </div> |
| 429 | + <Popover placement="bottom end"> |
| 430 | + <ListBox> |
| 431 | + {inputValue.length > 0 && ( |
| 432 | + <ListBoxItem onAction={onAction}> |
| 433 | + {`Create "${inputValue}"`} |
| 434 | + </ListBoxItem> |
| 435 | + )} |
| 436 | + <ListBoxItem>Aardvark</ListBoxItem> |
| 437 | + <ListBoxItem>Cat</ListBoxItem> |
| 438 | + <ListBoxItem>Dog</ListBoxItem> |
| 439 | + <ListBoxItem>Kangaroo</ListBoxItem> |
| 440 | + <ListBoxItem>Panda</ListBoxItem> |
| 441 | + <ListBoxItem>Snake</ListBoxItem> |
| 442 | + </ListBox> |
| 443 | + </Popover> |
| 444 | + </ComboBox> |
| 445 | + ); |
| 446 | + } |
| 447 | + |
| 448 | + let tree = render(<WithCreateOption />); |
| 449 | + let comboboxTester = testUtilUser.createTester('ComboBox', {root: tree.container}); |
| 450 | + act(() => { |
| 451 | + comboboxTester.combobox.focus(); |
| 452 | + }); |
| 453 | + |
| 454 | + await user.keyboard('L'); |
| 455 | + |
| 456 | + let options = comboboxTester.options(); |
| 457 | + expect(options).toHaveLength(1); |
| 458 | + expect(options[0]).toHaveTextContent('Create "L"'); |
| 459 | + |
| 460 | + await user.keyboard('{ArrowDown}{Enter}'); |
| 461 | + expect(onAction).toHaveBeenCalledTimes(1); |
| 462 | + expect(comboboxTester.combobox).toHaveValue(''); |
| 463 | + }); |
411 | 464 | });
|
0 commit comments