Skip to content

Commit

Permalink
fix: integration multiselector (#4683)
Browse files Browse the repository at this point in the history
Input should use state set outside and derive "all selected" from it.
This was not the case causing issues when loading a form with "wildcard"
pre-selected.
  • Loading branch information
Tymek committed Sep 14, 2023
1 parent e8d5f0c commit 257013c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Expand Up @@ -51,25 +51,21 @@ describe('AddonMultiSelector', () => {

it('can toggle "ALL" checkbox', async () => {
const user = userEvent.setup();
render(
const { rerender } = render(
<IntegrationMultiSelector {...mockProps} selectedItems={['*']} />
);

await user.click(screen.getByTestId('select-all-projects'));

expect(
screen.getByLabelText(/all current and future projects/i)
).not.toBeChecked();
expect(onChange).toHaveBeenCalledWith([]);

expect(screen.getByLabelText('Projects')).toBeEnabled();
rerender(
<IntegrationMultiSelector {...mockProps} selectedItems={[]} />
);

await user.click(screen.getByTestId('select-all-projects'));

expect(
screen.getByLabelText(/all current and future projects/i)
).toBeChecked();

expect(screen.getByLabelText('Projects')).toBeDisabled();
expect(onChange).toHaveBeenCalledWith(['*']);
});

it('renders with autocomplete enabled if default value is not a wildcard', () => {
Expand Down Expand Up @@ -151,4 +147,22 @@ describe('AddonMultiSelector', () => {
expect(screen.queryByText('Alpha')).not.toBeInTheDocument();
});
});

it('will load wildcard status from props', async () => {
const { rerender } = render(
<IntegrationMultiSelector {...mockProps} selectedItems={[]} />
);

expect(
screen.getByLabelText(/all current and future projects/i)
).not.toBeChecked();

rerender(
<IntegrationMultiSelector {...mockProps} selectedItems={['*']} />
);

expect(
screen.getByLabelText(/all current and future projects/i)
).toBeChecked();
});
});
@@ -1,4 +1,4 @@
import React, { ChangeEvent, Fragment, useState, VFC } from 'react';
import React, { ChangeEvent, Fragment, VFC } from 'react';
import { IAutocompleteBoxOption } from '../../../common/AutocompleteBox/AutocompleteBox';
import {
AutocompleteRenderGroupParams,
Expand Down Expand Up @@ -55,9 +55,6 @@ export const IntegrationMultiSelector: VFC<IIntegrationMultiSelectorProps> = ({
description,
required,
}) => {
const [isWildcardSelected, selectWildcard] = useState(
selectedItems.includes(ALL_OPTIONS)
);
const renderInput = (params: AutocompleteRenderInputParams) => (
<TextField
{...params}
Expand All @@ -76,15 +73,15 @@ export const IntegrationMultiSelector: VFC<IIntegrationMultiSelectorProps> = ({
selectedItems.length === options.length &&
selectedItems[0] !== ALL_OPTIONS;

const isWildcardSelected = selectedItems.includes(ALL_OPTIONS);

const onAllItemsChange = (
e: ChangeEvent<HTMLInputElement>,
checked: boolean
) => {
if (checked) {
selectWildcard(true);
onChange([ALL_OPTIONS]);
} else {
selectWildcard(false);
onChange(selectedItems.includes(ALL_OPTIONS) ? [] : selectedItems);
}
};
Expand Down

0 comments on commit 257013c

Please sign in to comment.