|
7 | 7 |
|
8 | 8 | import React, { useState } from 'react'; |
9 | 9 | import { render, screen, within, fireEvent, act } from '@testing-library/react'; |
| 10 | +import { useCombobox } from 'downshift'; |
10 | 11 | import userEvent from '@testing-library/user-event'; |
11 | 12 | import { |
12 | 13 | findListBoxNode, |
@@ -493,6 +494,40 @@ describe('ComboBox', () => { |
493 | 494 | expect(findInputNode()).toHaveDisplayValue(''); |
494 | 495 | expect(mockProps.onChange).toHaveBeenCalled(); |
495 | 496 | }); |
| 497 | + it('should call onChange when downshiftProps onStateChange is provided', async () => { |
| 498 | + const downshiftProps = { |
| 499 | + onStateChange: jest.fn(), |
| 500 | + }; |
| 501 | + render( |
| 502 | + <ComboBox |
| 503 | + {...mockProps} |
| 504 | + selectedItem={mockProps.items[0]} |
| 505 | + downshiftProps={downshiftProps} |
| 506 | + /> |
| 507 | + ); |
| 508 | + expect(mockProps.onChange).not.toHaveBeenCalled(); |
| 509 | + expect(downshiftProps.onStateChange).not.toHaveBeenCalled(); |
| 510 | + await openMenu(); |
| 511 | + expect(downshiftProps.onStateChange).toHaveBeenCalledTimes(1); |
| 512 | + await userEvent.click(screen.getByRole('option', { name: 'Item 2' })); |
| 513 | + expect(mockProps.onChange).toHaveBeenCalledTimes(1); |
| 514 | + expect(downshiftProps.onStateChange).toHaveBeenCalledTimes(3); |
| 515 | + expect(downshiftProps.onStateChange).toHaveBeenNthCalledWith(2, { |
| 516 | + selectedItem: { |
| 517 | + id: 'id-2', |
| 518 | + label: 'Item 2', |
| 519 | + value: 2, |
| 520 | + }, |
| 521 | + type: useCombobox.stateChangeTypes.ItemClick, |
| 522 | + }); |
| 523 | + expect(downshiftProps.onStateChange).toHaveBeenLastCalledWith({ |
| 524 | + selectedItem: undefined, |
| 525 | + type: useCombobox.stateChangeTypes.FunctionSetHighlightedIndex, |
| 526 | + }); |
| 527 | + expect( |
| 528 | + screen.getByRole('combobox', { value: 'Item 2' }) |
| 529 | + ).toBeInTheDocument(); |
| 530 | + }); |
496 | 531 | }); |
497 | 532 |
|
498 | 533 | describe('when disabled', () => { |
|
0 commit comments