Skip to content

Commit

Permalink
fix: pass disabled props for radio bar component (#997)
Browse files Browse the repository at this point in the history
  • Loading branch information
soleksy-splunk authored Jan 10, 2024
1 parent 312c8be commit a4eb6f9
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 10 deletions.
3 changes: 3 additions & 0 deletions splunk_add_on_ucc_framework/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,9 @@
"additionalProperties": false
},
"minItems": 1
},
"disableonEdit": {
"type": "boolean"
}
},
"required": ["rows"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.35.1Ree62a73b",
"version": "5.35.1R7fe3d58d",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.3"
}
Expand Down
176 changes: 176 additions & 0 deletions ui/src/components/CheckboxGroup/CheckboxGroup.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import CheckboxGroup from './CheckboxGroup';
import { CheckboxGroupProps } from './checkboxGroup.utils';
import { MODE_CREATE } from '../../constants/modes';

const handleChange = jest.fn();

const defaultCheckboxProps: CheckboxGroupProps = {
mode: MODE_CREATE,
field: 'api',
value: 'collect_collaboration/1200,collect_file/1,collect_task/1',
controlOptions: {
rows: [
{
field: 'collect_collaboration',
checkbox: {
label: 'Collect folder collaboration',
},
input: {
defaultValue: 1200,
required: false,
},
},
{
field: 'collect_file',
checkbox: {
label: 'Collect file metadata',
},
input: {
defaultValue: 1,
required: true,
},
},
{
field: 'collect_task',
checkbox: {
label: 'Collect tasks and comments',
},
input: {
defaultValue: 1,
required: true,
},
},
],
},
handleChange,
};

const renderFeature = (additionalProps?: Partial<CheckboxGroupProps>) => {
const props = {
...defaultCheckboxProps,
...additionalProps,
};
render(<CheckboxGroup {...props} />);
};

it('renders checkbox group correctly', async () => {
renderFeature();
defaultCheckboxProps.controlOptions.rows.forEach((row) => {
const optionWithCorrectText = screen.getByText(row?.checkbox?.label || 'unexisting string');
expect(optionWithCorrectText).toBeInTheDocument();
expect(optionWithCorrectText.dataset?.disabled).toBeUndefined();
});
});

it('renders disabled checkbox group correctly', async () => {
renderFeature({ disabled: true });
defaultCheckboxProps.controlOptions.rows.forEach((row) => {
const optionWithCorrectText = screen.getByText(row?.checkbox?.label || 'unexisting string');
expect(optionWithCorrectText).toBeInTheDocument();
expect(optionWithCorrectText.dataset.disabled).toEqual('true');
});
});

it('increment value correctly', async () => {
renderFeature();
const incrementButtons = screen.getAllByTestId('increment');
const [firstIncrementer, secondIncrementer, thirdIncrementer] = incrementButtons;

await userEvent.click(firstIncrementer);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1201,collect_file/1,collect_task/1',
'checkboxGroup'
);
await userEvent.click(secondIncrementer);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1201,collect_file/2,collect_task/1',
'checkboxGroup'
);
await userEvent.click(thirdIncrementer);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1201,collect_file/2,collect_task/2',
'checkboxGroup'
);
});

it('decrement value correctly', async () => {
renderFeature();
const decrementButtons = screen.getAllByTestId('decrement');
const [firstDecrement, secondDecrement, thirdDecrement] = decrementButtons;

await userEvent.click(firstDecrement);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1199,collect_file/1,collect_task/1',
'checkboxGroup'
);
await userEvent.click(secondDecrement);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1199,collect_file/0,collect_task/1',
'checkboxGroup'
);
await userEvent.click(thirdDecrement);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1199,collect_file/0,collect_task/0',
'checkboxGroup'
);
});

it('mixed incrementing and decrementing value correctly', async () => {
renderFeature();
const incrementButtons = screen.getAllByTestId('increment');
const [firstIncrementer, secondIncrementer, thirdIncrementer] = incrementButtons;

const decrementButtons = screen.getAllByTestId('decrement');
const [firstDecrement, secondDecrement, thirdDecrement] = decrementButtons;

await userEvent.click(firstDecrement);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1199,collect_file/1,collect_task/1',
'checkboxGroup'
);

await userEvent.click(thirdIncrementer);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1199,collect_file/1,collect_task/2',
'checkboxGroup'
);

await userEvent.click(secondDecrement);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1199,collect_file/0,collect_task/2',
'checkboxGroup'
);

await userEvent.click(firstIncrementer);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1200,collect_file/0,collect_task/2',
'checkboxGroup'
);

await userEvent.click(thirdDecrement);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
'collect_collaboration/1200,collect_file/0,collect_task/1',
'checkboxGroup'
);

await userEvent.click(secondIncrementer);
expect(handleChange).toHaveBeenCalledWith(
defaultCheckboxProps.field,
defaultCheckboxProps.value,
'checkboxGroup'
);
});
5 changes: 3 additions & 2 deletions ui/src/components/CheckboxGroup/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { useValidation } from './checkboxGroupValidation';
import { MODE_CREATE } from '../../constants/modes';

function CheckboxGroup(props: CheckboxGroupProps) {
const { field, handleChange, controlOptions, addCustomValidator } = props;

const { field, handleChange, controlOptions, addCustomValidator, disabled } = props;
const flattenedRowsWithGroups = getFlattenRowsWithGroups(controlOptions);
const shouldUseDefaultValue = props.mode === MODE_CREATE && props.value === null;
const value = shouldUseDefaultValue
Expand Down Expand Up @@ -71,6 +70,7 @@ function CheckboxGroup(props: CheckboxGroupProps) {
group={row}
values={values}
handleRowChange={handleRowChange}
disabled={disabled}
/>
</ColumnLayout.Row>
);
Expand All @@ -81,6 +81,7 @@ function CheckboxGroup(props: CheckboxGroupProps) {
row={row}
values={values}
handleRowChange={handleRowChange}
disabled={disabled}
/>
</ColumnLayout.Row>
);
Expand Down
3 changes: 3 additions & 0 deletions ui/src/components/CheckboxGroup/CheckboxRowWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ function CheckboxRowWrapper({
row,
values,
handleRowChange,
disabled,
}: {
row: Row;
values: ValueByField;
handleRowChange: (newValue: { field: string; checkbox: boolean; text?: string }) => void;
disabled?: boolean;
}) {
const valueForField = values.get(row.field);
return (
Expand All @@ -19,6 +21,7 @@ function CheckboxRowWrapper({
checkbox={!!valueForField?.checkbox}
input={valueForField ? valueForField.inputValue : row.input?.defaultValue}
handleChange={handleRowChange}
disabled={disabled}
/>
);
}
Expand Down
4 changes: 3 additions & 1 deletion ui/src/components/CheckboxGroup/CheckboxSubGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ interface CheckboxSubGroupProps {
group: GroupWithRows;
values: ValueByField;
handleRowChange: (newValue: { field: string; checkbox: boolean; text?: string }) => void;
disabled?: boolean;
}

function CheckboxSubGroup({ group, values, handleRowChange }: CheckboxSubGroupProps) {
function CheckboxSubGroup({ group, values, handleRowChange, disabled }: CheckboxSubGroupProps) {
const checkedCheckboxesCount = getCheckedCheckboxesCount(group, values);
return (
<Group
Expand All @@ -28,6 +29,7 @@ function CheckboxSubGroup({ group, values, handleRowChange }: CheckboxSubGroupPr
<StyledCheckboxRowWrapper>
{group.rows.map((rowInsideGroup) => (
<CheckboxRowWrapper
disabled={disabled}
row={rowInsideGroup}
values={values}
handleRowChange={handleRowChange}
Expand Down
1 change: 1 addition & 0 deletions ui/src/components/CheckboxGroup/checkboxGroup.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export interface CheckboxGroupProps {
validator: (submittedField: string, submittedValue: string) => void
) => void;
handleChange: (field: string, value: string, componentType?: 'checkboxGroup') => void;
disabled?: boolean;
}

export function isGroupWithRows(item: GroupWithRows | Row): item is GroupWithRows {
Expand Down
30 changes: 30 additions & 0 deletions ui/src/components/FileInputComponent/FileInputComponent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,33 @@ test('Default error message disappears when reuploading encrypted file', async (
const nullHelpElement = await screen.queryByTestId('help');
expect(nullHelpElement).toBeNull();
});

it('File input disabled - rendered correctly with disabled attr', () => {
// throws error Could not parse CSS styleshee, which only obscures tests
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();

const field = 'testFileField';
const disabled = true;
const controlOptions = {
supportedFileTypes: ['json'],
maxFileSize: 10,
};
const handleChange = jest.fn();
const testFileName = 'testFileName.json';

render(
<FileInputComponent
field={field}
disabled={disabled}
controlOptions={controlOptions}
handleChange={handleChange}
fileNameToDisplay={testFileName}
encrypted={false}
/>
);

const fileComponent = screen.getByTestId('file-input');
expect(fileComponent).toBeInTheDocument();
expect(fileComponent).toHaveAttribute('disabled');
consoleSpy.mockRestore();
});
53 changes: 53 additions & 0 deletions ui/src/components/MultiInputComponent/MultiInputComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';

import { render, screen } from '@testing-library/react';
import MultiInputComponent, { MultiInputComponentProps } from './MultiInputComponent';

const handleChange = jest.fn();

const defaultInputProps: MultiInputComponentProps = {
field: 'defaultFieldName',
controlOptions: {
delimiter: ',',
createSearchChoice: true,
referenceName: 'referenceName',
dependencies: undefined,
endpointUrl: undefined,
labelField: 'labelField',
items: [
{ label: 'label1', value: 'value1' },
{ label: 'label2', value: 'value2' },
{ label: 'label3', value: 'value3' },
],
},
disabled: false,
value: 'defaultValue',
error: false,
dependencyValues: {},
handleChange,
};

const renderFeature = (additionalProps?: Partial<MultiInputComponentProps>) => {
const props = {
...defaultInputProps,
...additionalProps,
};
render(<MultiInputComponent {...props} />);
};

it('renders correctly', () => {
renderFeature();
const inputComponent = screen.getByTestId('multiselect');
expect(inputComponent).toBeInTheDocument();
expect(inputComponent.getAttribute('data-test-values')).toEqual(
// eslint-disable-next-line no-useless-escape
`[\"${defaultInputProps.value}\"]`
);
});

it('renders as disabled correctly', () => {
renderFeature({ disabled: true });
const inputComponent = screen.getByTestId('multiselect');
expect(inputComponent).toBeInTheDocument();
expect(inputComponent.getAttribute('aria-disabled')).toEqual('true');
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import styled from 'styled-components';
import axios from 'axios';
import WaitSpinner from '@splunk/react-ui/WaitSpinner';

import { axiosCallWrapper } from '../util/axiosCallWrapper';
import { filterResponse } from '../util/util';
import { axiosCallWrapper } from '../../util/axiosCallWrapper';
import { filterResponse } from '../../util/util';

const MultiSelectWrapper = styled(Multiselect)`
width: 320px !important;
Expand All @@ -15,7 +15,7 @@ const WaitSpinnerWrapper = styled(WaitSpinner)`
margin-left: 5px;
`;

interface MultiInputComponentProps {
export interface MultiInputComponentProps {
handleChange: (field: string, data: string) => void;
field: string;
controlOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export const Base: Story = {
},
],
},
disabled: false,
},
};
Loading

0 comments on commit a4eb6f9

Please sign in to comment.