Skip to content

Commit

Permalink
fix: Dropdown tests (#2187)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware authored May 22, 2024
1 parent 14f70c9 commit 4035dd2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
17 changes: 1 addition & 16 deletions packages/blade/src/components/Dropdown/DropdownHeaderFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const DropdownHeader = assignWithoutSideEffects(_DropdownHeader, {
type DropdownFooter = Pick<BaseFooterProps, 'children' | 'testID'>;

const _DropdownFooter = ({ children, testID }: DropdownFooter): React.ReactElement => {
const { setHasFooterAction, activeIndex, onTriggerKeydown, isOpen } = useDropdown();
const { setHasFooterAction, isOpen } = useDropdown();
const footerRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
Expand All @@ -73,21 +73,6 @@ const _DropdownFooter = ({ children, testID }: DropdownFooter): React.ReactEleme
<BaseBox
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ref={footerRef as any}
{...(isReactNative()
? {}
: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onKeyDown: (e: any) => {
const nativeEvent = e.nativeEvent;
const shouldIgnoreDropdownKeydown =
(nativeEvent.key === ' ' || nativeEvent.key === 'Enter') && activeIndex < 0;

if (!shouldIgnoreDropdownKeydown) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
onTriggerKeydown?.({ event: e.nativeEvent } as any);
}
},
})}
{...makeAccessible({
role: isReactNative() ? undefined : 'group',
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const BasicSelectItem: StoryFn<typeof Dropdown> = (props): React.ReactEle
return <BasicDropdown {...props} />;
};

BasicSelectItem.play = async ({ canvasElement }) => {
const { getByRole } = within(canvasElement);
BasicSelectItem.play = async () => {
const { getByRole } = within(document.body);
const selectInput = getByRole('combobox', { name: 'City' });
await userEvent.click(selectInput);
const option = getByRole('option', { name: 'Bengaluru' });
Expand All @@ -50,8 +50,8 @@ export const MultiSelectItem: StoryFn<typeof Dropdown> = (props): React.ReactEle
return <BasicDropdown {...props} selectionType="multiple" />;
};

MultiSelectItem.play = async ({ canvasElement }) => {
const { getByRole, getByLabelText, queryByLabelText } = within(canvasElement);
MultiSelectItem.play = async () => {
const { getByRole, getByLabelText, queryByLabelText } = within(document.body);
const selectInput = getByRole('combobox', { name: 'City' });
await userEvent.click(selectInput);
await expect(queryByLabelText('Close Bengaluru tag')).toBeFalsy();
Expand Down Expand Up @@ -85,8 +85,8 @@ export const Accessibility: StoryFn<typeof Dropdown> = (props): React.ReactEleme
);
};

Accessibility.play = async ({ canvasElement }) => {
const { getByRole, getByTestId } = within(canvasElement);
Accessibility.play = async () => {
const { getByRole, getByTestId } = within(document.body);
const selectInput = getByRole('combobox', { name: 'City' });
selectInput.focus();
await userEvent.keyboard('{ArrowDown}');
Expand Down Expand Up @@ -162,8 +162,8 @@ export const FooterActions: StoryFn<typeof Dropdown> = (): React.ReactElement =>
);
};

FooterActions.play = async ({ canvasElement }) => {
const { getByRole, queryByRole, getByTestId } = within(canvasElement);
FooterActions.play = async () => {
const { getByRole, queryByRole, getByTestId } = within(document.body);
const selectInput = getByRole('combobox', { name: 'Fruits' });
await userEvent.click(selectInput);
await waitFor(() => expect(getByRole('dialog', { name: 'Fruits' })).toBeVisible());
Expand Down Expand Up @@ -229,8 +229,8 @@ export const ControlledDropdownSingleSelect: StoryFn<typeof Dropdown> = (): Reac
);
};

ControlledDropdownSingleSelect.play = async ({ canvasElement }) => {
const { getByRole } = within(canvasElement);
ControlledDropdownSingleSelect.play = async () => {
const { getByRole } = within(document.body);
const selectInput = getByRole('combobox', { name: 'Select City' });

// external button control selection test
Expand Down Expand Up @@ -291,8 +291,8 @@ export const ControlledDropdownMultiSelect: StoryFn<typeof Dropdown> = (): React
);
};

ControlledDropdownMultiSelect.play = async ({ canvasElement }) => {
const { getByRole, queryAllByLabelText } = within(canvasElement);
ControlledDropdownMultiSelect.play = async () => {
const { getByRole, queryAllByLabelText } = within(document.body);
const selectInput = getByRole('combobox', { name: 'Select City' });

// Select 1 item programatically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const ItemSelect: StoryFn<typeof Dropdown> = (props): React.ReactElement
return <BasicDropdown {...props} />;
};

ItemSelect.play = async ({ canvasElement }) => {
const { getByRole } = within(canvasElement);
ItemSelect.play = async () => {
const { getByRole } = within(document.body);
const selectInput = getByRole('combobox', { name: 'City' });
await userEvent.click(selectInput);
const option = getByRole('option', { name: 'Bengaluru' });
Expand All @@ -58,8 +58,8 @@ export const ItemSort: StoryFn<typeof Dropdown> = (props): React.ReactElement =>
return <BasicDropdown {...props} />;
};

ItemSort.play = async ({ canvasElement }) => {
const { getByRole, queryByRole } = within(canvasElement);
ItemSort.play = async () => {
const { getByRole, queryByRole } = within(document.body);
const autoComplete = getByRole('combobox', { name: 'City' });
await userEvent.type(autoComplete, 'p');
const bengaluruOption = queryByRole('option', { name: 'Bengaluru' });
Expand All @@ -74,8 +74,8 @@ export const ItemMultiSelect: StoryFn<typeof Dropdown> = (props): React.ReactEle
return <BasicDropdown {...props} selectionType="multiple" />;
};

ItemMultiSelect.play = async ({ canvasElement }) => {
const { getByRole, getByLabelText, queryByLabelText } = within(canvasElement);
ItemMultiSelect.play = async () => {
const { getByRole, getByLabelText, queryByLabelText } = within(document.body);
const autoComplete = getByRole('combobox', { name: 'City' });
await userEvent.type(autoComplete, 'b');
await expect(queryByLabelText('Close Bengaluru tag')).toBeFalsy();
Expand Down Expand Up @@ -113,8 +113,8 @@ export const Accessibility: StoryFn<typeof Dropdown> = (props): React.ReactEleme
);
};

Accessibility.play = async ({ canvasElement }) => {
const { getByRole, getByTestId } = within(canvasElement);
Accessibility.play = async () => {
const { getByRole, getByTestId } = within(document.body);
const autoComplete = getByRole('combobox', { name: 'City' });
await userEvent.type(autoComplete, 'i');

Expand Down Expand Up @@ -199,8 +199,8 @@ export const ControlledDropdownSingleSelect: StoryFn<typeof Dropdown> = (): Reac
);
};

ControlledDropdownSingleSelect.play = async ({ canvasElement }) => {
const { getByRole, getByTestId } = within(canvasElement);
ControlledDropdownSingleSelect.play = async () => {
const { getByRole, getByTestId } = within(document.body);
const selectInput = getByRole('combobox', { name: 'Select City' });

// external button control selection test
Expand Down Expand Up @@ -271,8 +271,8 @@ export const ControlledDropdownMultiSelect: StoryFn<typeof Dropdown> = (): React
);
};

ControlledDropdownMultiSelect.play = async ({ canvasElement }) => {
const { getByRole, queryAllByLabelText } = within(canvasElement);
ControlledDropdownMultiSelect.play = async () => {
const { getByRole, queryAllByLabelText } = within(document.body);
const selectInput = getByRole('combobox', { name: 'Select City' });

// Select 1 item controlled
Expand Down Expand Up @@ -359,8 +359,8 @@ export const ControlledFiltering: StoryFn<typeof Dropdown> = (): React.ReactElem
);
};

ControlledFiltering.play = async ({ canvasElement }) => {
const { getByRole, queryByRole } = within(canvasElement);
ControlledFiltering.play = async () => {
const { getByRole, queryByRole } = within(document.body);
const selectInput = getByRole('combobox', { name: 'Cities' });

await userEvent.click(selectInput);
Expand Down

0 comments on commit 4035dd2

Please sign in to comment.