|
1 | 1 | /** |
2 | | - * Copyright IBM Corp. 2016, 2023 |
| 2 | + * Copyright IBM Corp. 2016, 2025 |
3 | 3 | * |
4 | 4 | * This source code is licensed under the Apache-2.0 license found in the |
5 | 5 | * LICENSE file in the root directory of this source tree. |
@@ -536,7 +536,7 @@ describe('FilterableMultiSelect', () => { |
536 | 536 | item ? ( |
537 | 537 | <span className="test-element" data-testid="custom-id-item"> |
538 | 538 | {item.text}{' '} |
539 | | - <span role="img" alt="fire"> |
| 539 | + <span role="img" aria-label="fire"> |
540 | 540 | {' '} |
541 | 541 | 🔥 |
542 | 542 | </span> |
@@ -922,4 +922,53 @@ describe('FilterableMultiSelect', () => { |
922 | 922 | `${prefix}--multi-select--filterable--input-focused` |
923 | 923 | ); |
924 | 924 | }); |
| 925 | + |
| 926 | + it('should call `onMenuChange` exactly once on mount when `open` prop is provided', async () => { |
| 927 | + render(<FilterableMultiSelect {...mockProps} open />); |
| 928 | + await waitForPosition(); |
| 929 | + |
| 930 | + expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1); |
| 931 | + expect(mockProps.onMenuChange).toHaveBeenCalledWith(true); |
| 932 | + }); |
| 933 | + |
| 934 | + it('should not re-trigger `onMenuChange` on re-render if `open` prop remains unchanged', async () => { |
| 935 | + const { rerender } = render(<FilterableMultiSelect {...mockProps} open />); |
| 936 | + await waitForPosition(); |
| 937 | + |
| 938 | + // Initially called once on mount. |
| 939 | + expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1); |
| 940 | + expect(mockProps.onMenuChange).toHaveBeenCalledWith(true); |
| 941 | + |
| 942 | + // Rerender with the same open prop value. |
| 943 | + rerender(<FilterableMultiSelect {...mockProps} open />); |
| 944 | + await waitForPosition(); |
| 945 | + |
| 946 | + // The callback should not be called again. |
| 947 | + expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1); |
| 948 | + }); |
| 949 | + |
| 950 | + it('should not call `onMenuChange` on mount when uncontrolled', async () => { |
| 951 | + render(<FilterableMultiSelect {...mockProps} />); |
| 952 | + await waitForPosition(); |
| 953 | + |
| 954 | + // Since uncontrolled mode only fires on interactions, expect no call on |
| 955 | + // mount. |
| 956 | + expect(mockProps.onMenuChange).not.toHaveBeenCalled(); |
| 957 | + }); |
| 958 | + |
| 959 | + it('should call `onMenuChange` when user interactions trigger state changes', async () => { |
| 960 | + render(<FilterableMultiSelect {...mockProps} />); |
| 961 | + await waitForPosition(); |
| 962 | + |
| 963 | + // Open the menu by clicking the combobox. |
| 964 | + await userEvent.click(screen.getByRole('combobox')); |
| 965 | + expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1); |
| 966 | + expect(mockProps.onMenuChange).toHaveBeenCalledWith(true); |
| 967 | + mockProps.onMenuChange.mockClear(); |
| 968 | + |
| 969 | + // Close the menu by clicking outside of it. |
| 970 | + await userEvent.click(document.body); |
| 971 | + expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1); |
| 972 | + expect(mockProps.onMenuChange).toHaveBeenCalledWith(false); |
| 973 | + }); |
925 | 974 | }); |
0 commit comments