Skip to content

Commit

Permalink
Add feature flag for the intermediate release process
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 12, 2024
1 parent 95813fd commit 6a593e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,32 @@ describe('Credentials', () => {
expect(await screen.findByText(value)).toBeInTheDocument();
});

/**
* Test for the intermediate release process
*/
it('does not show the component if the value is undefined', async () => {
render(
<IntlProvider locale="en">
<AdditionalFields {...props} value={undefined} />
</IntlProvider>
);

expect(screen.queryByTestId('additional_fieldsJsonEditor')).not.toBeInTheDocument();
});

it('changes the value correctly', async () => {
const newValue = JSON.stringify({ bar: 'test' });

render(
<IntlProvider locale="en">
<AdditionalFields {...props} value={undefined} />
<AdditionalFields {...props} />
</IntlProvider>
);

userEvent.paste(await screen.findByTestId('additional_fieldsJsonEditor'), newValue);
const editor = await screen.findByTestId('additional_fieldsJsonEditor');

userEvent.clear(editor);
userEvent.paste(editor, newValue);

await waitFor(() => {
expect(onChange).toHaveBeenCalledWith(newValue);
Expand All @@ -59,7 +75,7 @@ describe('Credentials', () => {

render(
<IntlProvider locale="en">
<AdditionalFields {...props} value={undefined} />
<AdditionalFields {...props} />
</IntlProvider>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export const AdditionalFieldsComponent: React.FC<AdditionalFieldsProps> = ({
messageVariables,
onChange,
}) => {
/**
* Hide the component if the value is not defined.
* This is needed for the intermediate release process.
* Users will not be able to use the field if they have never set it.
* On the next Serverless release the check will be removed.
*/
if (value === undefined) {
return null;
}

return (
<JsonEditorWithMessageVariables
messageVariables={messageVariables}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ describe('validateJSON', () => {
'A maximum of 1 additional fields can be defined at a time.'
);
});

it('does not return an error for an object', () => {
expect(validateJSON({ value: { foo: 'test' } })).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
* 2.0.
*/

import { isPlainObject } from 'lodash';
import { i18n } from '@kbn/i18n';

interface ValidateJSONArgs {
value?: string | null;
value?: string | null | Record<string, unknown>;
maxProperties?: number;
}

const isObject = (value?: ValidateJSONArgs['value']): value is Record<string, unknown> => {
return isPlainObject(value);
};

export const MAX_ATTRIBUTES_ERROR = (length: number) =>
i18n.translate('xpack.stackConnectors.schema.additionalFieldsLengthError', {
values: { length },
Expand All @@ -27,6 +32,10 @@ export const INVALID_JSON_FORMAT = i18n.translate(

export const validateJSON = ({ value, maxProperties }: ValidateJSONArgs) => {
try {
if (isObject(value)) {
return;
}

if (value) {
const parsedOtherFields = JSON.parse(value);

Expand Down

0 comments on commit 6a593e9

Please sign in to comment.