Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const authSchema = z
token: z
.string()
.min(1, { message: i18n.BEARER_AUTH_REQUIRED_MESSAGE })
.meta({ sensitive: true }),
.meta({ sensitive: true, label: i18n.BEARER_TOKEN_LABEL }),
})
.meta({ label: i18n.BEARER_AUTH_LABEL });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export const BEARER_AUTH_LABEL = i18n.translate('connectorSpecs.authType.bearerA
defaultMessage: 'Bearer token',
});

export const BEARER_TOKEN_LABEL = i18n.translate('connectorSpecs.bearerAuth.token.label', {
defaultMessage: 'Token',
});

export const BEARER_AUTH_REQUIRED_MESSAGE = i18n.translate(
'connectorSpecs.bearerAuth.token.requiredMessage',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ import {
import { getFieldFromSchema, getFieldsFromSchema, renderField } from './field_builder';
import type { FormConfig } from './form';
import { addMeta } from './schema_connector_metadata';
import { getWidgetComponent } from './widgets';

jest.mock('./widgets', () => {
const module = jest.requireActual('./widgets');
return {
...module,
getWidgetComponent: jest.fn(module.getWidgetComponent),
};
});

const getWidgetComponentMock = getWidgetComponent as jest.Mock;

const wrapper = ({ children }: { children: React.ReactNode }) => (
<IntlProvider locale="en">{children}</IntlProvider>
Expand Down Expand Up @@ -364,3 +375,57 @@ describe('Field Builder', () => {
});
});
});

describe('mocked getWidgetComponent', () => {
const formConfig: FormConfig = { isEdit: true };
const mockWidgetComponent = jest.fn((props) => {
return <div data-testid="mock-widget" />;
});

beforeAll(() => {
getWidgetComponentMock.mockReturnValue(mockWidgetComponent);
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should pass correct props structure to WidgetComponent', () => {
const schema = z.string().default('default-value').meta({
label: 'Username Label',
placeholder: 'Enter your username',
helpText: 'This is help text',
disabled: true,
});

const path = 'username';

const field = getFieldFromSchema({ schema, path, formConfig });
render(renderField({ field }));

expect(mockWidgetComponent).toHaveBeenCalledTimes(1);

const receivedProps = mockWidgetComponent.mock.calls[0][0];

expect(receivedProps.path).toBe('username');
expect(receivedProps.formConfig).toBe(formConfig);

expect(receivedProps.fieldConfig).toBeDefined();
expect(receivedProps.fieldConfig.label).toBe('Username Label');
expect(receivedProps.fieldConfig.defaultValue).toBe('default-value');
expect(receivedProps.fieldConfig.validations).toHaveLength(1);
expect(typeof receivedProps.fieldConfig.validations[0].validator).toBe('function');

expect(receivedProps.fieldProps).toBeDefined();
expect(receivedProps.fieldProps.helpText).toBe('This is help text');
expect(receivedProps.fieldProps.fullWidth).toBe(true);
expect(receivedProps.fieldProps.labelAppend).toBeNull();

expect(receivedProps.fieldProps.euiFieldProps).toBeDefined();
expect(receivedProps.fieldProps.euiFieldProps.placeholder).toBe('Enter your username');
expect(receivedProps.fieldProps.euiFieldProps.disabled).toBe(true);
expect(receivedProps.fieldProps.euiFieldProps['data-test-subj']).toBe(
'generator-field-username'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const renderField = ({ field }: RenderFieldProps) => {
schema={schema}
formConfig={formConfig}
fieldConfig={{
label,
defaultValue,
validations: [
{
Expand All @@ -148,10 +149,8 @@ export const renderField = ({ field }: RenderFieldProps) => {
],
}}
fieldProps={{
label,
helpText,
fullWidth: true,

labelAppend: isOptional ? (
<EuiText size="xs" color="subdued">
{OPTIONAL_LABEL}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface BaseWidgetProps<
formConfig: FormConfig;
/* Configuration specific to the field */
fieldConfig: {
label?: string;
validations: [
{
validator: (...args: Parameters<ValidationFunc>) => ReturnType<ValidationFunc<any>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const renderWithSecretFields = ({
/>
);
})}
{/* will be ignored because it has no label */}
<UseField path="secrets.authType" />
<EncryptedFieldsCallout isEdit={isEdit} isMissingSecrets={isMissingSecrets} />
</FormTestProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const isEmpty = (value: string | undefined): value is string => value != null ||

const getSecretFields = (fields: FieldsMap): FieldsMap =>
Object.keys(fields)
.filter((fieldPath) => fieldPath.includes('secrets'))
.filter((fieldPath) => fieldPath.includes('secrets') && fields[fieldPath].label)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to exclude empty labels from the callout logic. It was no issue before, but in the generator, the authType is hidden, users won't update it and therefore shouldn't show up in this callout.

.reduce((filteredFields, path) => ({ ...filteredFields, [path]: fields[path] }), {});

const getLabelsFromFields = (fields: FieldsMap): string[] =>
Expand Down