-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Makes autosuggest input use aria-expanded every time the dropdow…
…n is shown
- Loading branch information
Showing
3 changed files
with
133 additions
and
87 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/autosuggest/__tests__/autosuggest-dropdown-states.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import '../../__a11y__/to-validate-a11y'; | ||
import Autosuggest, { AutosuggestProps } from '../../../lib/components/autosuggest'; | ||
import createWrapper from '../../../lib/components/test-utils/dom'; | ||
|
||
import styles from '../../../lib/components/autosuggest/styles.css.js'; | ||
import statusIconStyles from '../../../lib/components/status-indicator/styles.selectors.js'; | ||
|
||
const defaultOptions: AutosuggestProps.Options = [ | ||
{ value: '1', label: 'One' }, | ||
{ value: '2', lang: 'fr' }, | ||
]; | ||
const defaultProps: AutosuggestProps = { | ||
enteredTextLabel: () => 'Use value', | ||
value: '', | ||
onChange: () => {}, | ||
options: defaultOptions, | ||
}; | ||
|
||
function renderAutosuggest(jsx: React.ReactElement) { | ||
const { container, rerender } = render(jsx); | ||
const wrapper = createWrapper(container).findAutosuggest()!; | ||
return { container, wrapper, rerender }; | ||
} | ||
|
||
( | ||
[ | ||
['loading', true], | ||
['error', true], | ||
['finished', false], | ||
] as const | ||
).forEach(([statusType, isSticky]) => { | ||
test(`should display ${statusType} status text as ${isSticky ? 'sticky' : 'non-sticky'} footer`, () => { | ||
const statusText = { | ||
[`${statusType}Text`]: `Test ${statusType} text`, | ||
}; | ||
const { wrapper } = renderAutosuggest(<Autosuggest {...defaultProps} statusType={statusType} {...statusText} />); | ||
wrapper.focus(); | ||
expect(wrapper.findStatusIndicator()!.getElement()).toHaveTextContent(`Test ${statusType} text`); | ||
|
||
const dropdown = wrapper.findDropdown()!.findOpenDropdown()!; | ||
expect(Boolean(dropdown.findByClassName(styles['list-bottom']))).toBe(!isSticky); | ||
}); | ||
|
||
test(`check a11y for ${statusType} and ${isSticky ? 'sticky' : 'non-sticky'} footer`, async () => { | ||
const statusText = { | ||
[`${statusType}Text`]: `Test ${statusType} text`, | ||
}; | ||
const { container, wrapper } = renderAutosuggest( | ||
<Autosuggest {...defaultProps} statusType={statusType} {...statusText} ariaLabel="input" /> | ||
); | ||
wrapper.focus(); | ||
|
||
await expect(container).toValidateA11y(); | ||
}); | ||
}); | ||
|
||
test('should display error status icon with provided aria label', () => { | ||
const { wrapper } = renderAutosuggest( | ||
<Autosuggest | ||
{...defaultProps} | ||
statusType="error" | ||
errorText="Test error text" | ||
errorIconAriaLabel="Test error text" | ||
/> | ||
); | ||
wrapper.focus(); | ||
const statusIcon = wrapper.findStatusIndicator()!.findByClassName(statusIconStyles.icon)!.getElement(); | ||
expect(statusIcon).toHaveAttribute('aria-label', 'Test error text'); | ||
expect(statusIcon).toHaveAttribute('role', 'img'); | ||
}); | ||
|
||
test('should associate the error status footer with the dropdown', () => { | ||
const { wrapper } = renderAutosuggest( | ||
<Autosuggest {...defaultProps} statusType="error" errorText="Test error text" /> | ||
); | ||
wrapper.focus(); | ||
expect(wrapper.findDropdown().find('ul')!.getElement()).toHaveAccessibleDescription('Test error text'); | ||
}); | ||
|
||
test('should associate the finished status footer with the dropdown', () => { | ||
const { wrapper } = renderAutosuggest( | ||
<Autosuggest {...defaultProps} statusType="finished" finishedText="Finished text" /> | ||
); | ||
wrapper.focus(); | ||
expect(wrapper.findDropdown().find('ul')!.getElement()).toHaveAccessibleDescription('Finished text'); | ||
}); | ||
|
||
test('should associate the matches count footer with the dropdown', () => { | ||
const { wrapper } = renderAutosuggest( | ||
<Autosuggest {...defaultProps} value="A" statusType="finished" filteringResultsText={() => '3 items'} /> | ||
); | ||
wrapper.focus(); | ||
expect(wrapper.findDropdown().find('ul')!.getElement()).toHaveAccessibleDescription('3 items'); | ||
}); | ||
|
||
describe('when no options is matched the dropdown is shown but aria-expanded is false', () => { | ||
test('matched option and entered text option', () => { | ||
const { wrapper } = renderAutosuggest(<Autosuggest {...defaultProps} statusType="finished" value="One" />); | ||
wrapper.setInputValue('One'); | ||
expect(wrapper.findDropdown().findOpenDropdown()).not.toBe(null); | ||
expect(wrapper.findEnteredTextOption()).not.toBe(null); | ||
expect(wrapper.findDropdown().findOptions()).toHaveLength(1); | ||
expect(wrapper.findNativeInput().getElement()).toHaveAttribute('aria-expanded', 'true'); | ||
}); | ||
|
||
test('only entered text option', () => { | ||
const { wrapper } = renderAutosuggest(<Autosuggest {...defaultProps} statusType="finished" value="free-text" />); | ||
wrapper.setInputValue('free-text'); | ||
expect(wrapper.findDropdown().findOpenDropdown()).not.toBe(null); | ||
expect(wrapper.findEnteredTextOption()).not.toBe(null); | ||
expect(wrapper.findDropdown().findOptions()).toHaveLength(0); | ||
expect(wrapper.findNativeInput().getElement()).toHaveAttribute('aria-expanded', 'true'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters