Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { cleanup, fireEvent, render } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { DropdownMenu } from '../DropdownMenu';
import '@testing-library/jest-dom/vitest';
afterEach(cleanup);
describe('DropdownMenu', () => {
it('should render with normal items', () => {
const items = [
{ type: 'item' as const, children: 'Item 1' },
{ type: 'item' as const, children: 'Item 2', disabled: true, variant: 'destructive' as const },
];
const { getByRole } = render(
<DropdownMenu open items={items}>
<button type="button">Trigger</button>
</DropdownMenu>
);
expect(getByRole('menuitem', { name: 'Item 1' })).toBeVisible();
expect(getByRole('menuitem', { name: 'Item 2' })).toHaveAttribute('aria-disabled', 'true');
});
it('keeps disabled submenus closed for pointer and keyboard input', () => {
const { getByRole, queryByRole } = render(
<DropdownMenu
open
items={[{
type: 'subItem',
children: 'More',
disabled: true,
options: [{ type: 'item', children: 'Sub item' }],
}]}
>
<button type="button">Trigger</button>
</DropdownMenu>
);
const submenu = getByRole('menuitem', { name: 'More' });
fireEvent.click(submenu);
fireEvent.keyDown(submenu, { key: 'ArrowRight' });
expect(submenu).toHaveAttribute('aria-disabled', 'true');
expect(queryByRole('menuitem', { name: 'Sub item' })).not.toBeInTheDocument();
});
it('should render custom content without wrapping it as a menu item', () => {
const items = [
{
type: 'custom' as const,
children: <input aria-label="Insert count" defaultValue="1" />,
},
];
const { getByLabelText } = render(
<DropdownMenu open items={items}>
<button type="button">Trigger</button>
</DropdownMenu>
);
const input = getByLabelText('Insert count');
expect(input).toBeInTheDocument();
expect(input.closest('[role="menuitem"]')).toBeNull();
});
it('should invoke onSelect callbacks for item/checkbox/radio', () => {
const onItemSelect = vi.fn();
const onCheckboxSelect = vi.fn();
const onRadioSelect = vi.fn();
const items = [
{ type: 'item' as const, children: 'Run', onSelect: onItemSelect },
{ type: 'checkbox' as const, value: 'c1', label: 'Check', checked: false, onSelect: onCheckboxSelect },
{
type: 'radio' as const,
value: 'a',
onSelect: onRadioSelect,
options: [{ label: 'A', value: 'a' }, { label: 'B', value: 'b' }],
},
];
const { getByText } = render(
<DropdownMenu open items={items}>
<button type="button">Trigger</button>
</DropdownMenu>
);
fireEvent.click(getByText('Run'));
fireEvent.click(getByText('Check'));
fireEvent.click(getByText('B'));
expect(onItemSelect).toHaveBeenCalled();
expect(onCheckboxSelect).toHaveBeenCalledWith('c1');
expect(onRadioSelect).toHaveBeenCalledWith('b');
});
it('should throw when radio option misses value', () => {
const badItems = [
{
type: 'radio' as const,
value: 'a',
options: [{ label: 'Missing Value' }],
},
];
expect(() =>
render(
<DropdownMenu open items={badItems}>
<button type="button">Trigger</button>
</DropdownMenu>
)
).toThrow('[DropdownMenu]: `value` is required');
});
});