From 85600c7c90c97bed6fc6bfd3ea4089d741515ed9 Mon Sep 17 00:00:00 2001 From: Aleksandra Danilina Date: Wed, 2 Aug 2023 19:26:55 +0200 Subject: [PATCH] test: Add unit tests --- .../button-dropdown-accessibility.test.tsx | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/button-dropdown/__tests__/button-dropdown-accessibility.test.tsx b/src/button-dropdown/__tests__/button-dropdown-accessibility.test.tsx index b12ee173be..32d0818681 100644 --- a/src/button-dropdown/__tests__/button-dropdown-accessibility.test.tsx +++ b/src/button-dropdown/__tests__/button-dropdown-accessibility.test.tsx @@ -12,6 +12,11 @@ const renderButtonDropdown = (props: ButtonDropdownProps) => { return createWrapper(renderResult.container).findButtonDropdown()!; }; +const renderWithTrigger = (props: ButtonDropdownProps, triggerName: string) => { + const renderResult = render({triggerName}); + return createWrapper(renderResult.container).findButtonDropdown()!; +}; + const items: ButtonDropdownProps.Items = [ { id: 'i1', text: 'item1', description: 'Item 1 description' }, { @@ -67,6 +72,29 @@ const items: ButtonDropdownProps.Items = [ expect(groups[0].findAll('[role="menuitem"]').length).toBe(2); expect(groups[1].findAll('[role="menuitem"]').length).toBe(1); }); + + it('auto-labels dropdown with trigger title', () => { + const wrapper = renderWithTrigger({ ...props }, 'Actions'); + wrapper.openDropdown(); + + const menuElement = wrapper.findOpenDropdown()!.find('[role="menu"]')!; + expect(menuElement.getElement()).toHaveAccessibleName('Actions'); + }); + + it('uses aria-label as accessible name for dropdown if presented', () => { + const wrapper = renderWithTrigger({ ...props, ariaLabel: 'Custom label' }, 'Actions'); + wrapper.openDropdown(); + + const menuElement = wrapper.findOpenDropdown()!.find('[role="menu"]')!; + expect(menuElement.getElement()).toHaveAccessibleName('Custom label'); + }); + + it('does not label dropdown with icon trigger', () => { + const wrapper = renderButtonDropdown({ ...props, variant: 'icon' }); + wrapper.openDropdown(); + const menuElement = wrapper.findOpenDropdown()!.find('[role="menu"]')!; + expect(menuElement.getElement()).not.toHaveAttribute('aria-labelledby'); + }); }); });