Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: required star visibility when using modify prop #1489

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ui/src/components/ControlWrapper/ControlWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ControlGroupWrapper = styled(ControlGroup).attrs((props: { dataName: strin
}
`;

interface ControlWrapperProps {
export interface ControlWrapperProps {
mode: Mode;
utilityFuncts: UtilControlWrapper;
value: AcceptableFormValueOrNullish;
Expand Down Expand Up @@ -128,12 +128,17 @@ class ControlWrapper extends React.PureComponent<ControlWrapperProps> {
</>
);

const isFieldRequired = // modifiedEntitiesData takes precedence over entity
this.props?.modifiedEntitiesData?.required || this.props.entity?.required === undefined
? 'oauth_field' in (this.props.entity || {}) // if required is undefined use true for oauth fields and false for others
: this.props.entity?.required; // if required present use required
const label = this.props?.modifiedEntitiesData?.label || this?.props?.entity?.label || '';
const isRequiredModified =
typeof this.props?.modifiedEntitiesData?.required === 'boolean'
? this.props?.modifiedEntitiesData?.required
: this.props.entity?.required;

const isFieldRequired =
isRequiredModified === undefined // // if oauth_field exists field required by default
? 'oauth_field' in (this.props.entity || {}) // if oauth_field does not exists not required by default
: isRequiredModified;

const label = this.props?.modifiedEntitiesData?.label || this?.props?.entity?.label || '';
return (
this.props.display && (
<ControlGroupWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,37 @@ export const Base: Story = {
fileNameToDisplay: 'Previous File',
},
};

export const WithModifications: Story = {
args: {
...Base.args,
entity: {
field: 'url',
label: 'URL',
type: 'text',
help: 'Enter the URL, for example',
required: true,
encrypted: false,
},
modifiedEntitiesData: { required: false, label: 'Modified URL', help: 'Modified help' },
},
};

export const WithModificationsMakeRequired: Story = {
args: {
...Base.args,
entity: {
field: 'url',
label: 'URL',
type: 'text',
help: 'Enter the URL, for example',
required: false,
encrypted: false,
},
modifiedEntitiesData: {
required: true,
label: 'Modified URL',
help: 'Modified help required',
},
},
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 129 additions & 0 deletions ui/src/components/ControlWrapper/test/ControlWrapper.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import ControlWrapper, { ControlWrapperProps } from '../ControlWrapper';

const renderControlWrapper = (props: Partial<ControlWrapperProps>) => {
render(
<ControlWrapper
mode="create"
utilityFuncts={{
utilCustomFunctions: {
setState: () => {},
setErrorFieldMsg: () => {},
clearAllErrorMsg: () => {},
setErrorMsg: () => {},
},
handleChange: () => {},
addCustomValidator: () => {},
}}
value=""
display
error={false}
disabled={false}
serviceName="testServiceName"
dependencyValues={undefined}
entity={{
field: 'url',
label: 'URL',
type: 'text',
help: 'Enter the URL, for example',
required: true,
validators: [
{
errorMsg:
"Invalid URL provided. URL should start with 'https' as only secure URLs are supported. Provide URL in this format",
type: 'regex',
pattern: '^(https://)[^/]+/?$',
},
],
encrypted: false,
}}
{...props}
/>
);
};

it('check if required star displayed correctly', () => {
renderControlWrapper({});
const requiredStar = screen.queryByText('*');
expect(requiredStar).toBeInTheDocument();
});

it('check if required star not displayed', () => {
renderControlWrapper({
entity: {
field: 'url',
label: 'URL',
type: 'text',
required: false,
},
});
const requiredStar = screen.queryByText('*');
expect(requiredStar).not.toBeInTheDocument();
});

it('check if required star displayed correctly from modifiedEntitiesData', () => {
renderControlWrapper({
entity: {
field: 'url',
label: 'URL',
type: 'text',
required: false,
},
modifiedEntitiesData: { required: true },
});
const requiredStar = screen.queryByText('*');
expect(requiredStar).toBeInTheDocument();
});

it('check if required star not displayed due to modifiedEntitiesData', () => {
renderControlWrapper({
entity: {
field: 'url',
label: 'URL',
type: 'text',
required: true,
},
modifiedEntitiesData: { required: false },
});

const requiredStar = screen.queryByText('*');
expect(requiredStar).not.toBeInTheDocument();
});

it('check if label and help updated due to modifiedEntitiesData', () => {
const modifications = { required: false, label: 'Modified URL', help: 'Modified help' };
renderControlWrapper({
entity: {
field: 'url',
label: 'URL',
help: 'Enter the URL, for example',
type: 'text',
required: true,
},
modifiedEntitiesData: modifications,
});

const label = screen.getByTestId('label'); // label replaced
expect(label).toHaveTextContent(modifications.label);

const help = screen.getByTestId('help'); // help replaced
expect(help).toHaveTextContent(modifications.help);
});

it('check if help added due to modifiedEntitiesData', () => {
const modifications = { help: 'Modified help' };

renderControlWrapper({
entity: {
field: 'url',
label: 'URL',
type: 'text',
required: true,
},
modifiedEntitiesData: modifications,
});

const help = screen.getByTestId('help');
expect(help).toHaveTextContent(modifications.help);
});
Loading