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 downsample validation when downsample action disappears from the previous phase #140628

Merged
merged 3 commits into from
Sep 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import { emptyField } from '../../helpers/field_validators';
import { FieldHook, FieldValidateResponse, VALIDATION_TYPES, FieldConfig } from '..';

describe('useField() hook', () => {
let fieldHook: FieldHook;

beforeAll(() => {
jest.useFakeTimers();
});
Expand All @@ -26,22 +24,23 @@ describe('useField() hook', () => {
jest.useRealTimers();
});

const TestField = ({ field }: { field: FieldHook }) => {
fieldHook = field;
return null;
};

const getTestForm = (config?: FieldConfig) => () => {
const { form } = useForm();

return (
<Form form={form}>
<UseField path="test-path" component={TestField} config={config} />
</Form>
);
};

describe('field.validate()', () => {
let fieldHook: FieldHook;
const TestField = ({ field }: { field: FieldHook }) => {
fieldHook = field;
return null;
};

const getTestForm = (config?: FieldConfig) => () => {
const { form } = useForm();

return (
<Form form={form}>
<UseField path="test-path" component={TestField} config={config} />
</Form>
);
};

const EMPTY_VALUE = ' ';

test('it should not invalidate a field with arrayItem validation when isBlocking is false', async () => {
Expand Down Expand Up @@ -149,4 +148,45 @@ describe('useField() hook', () => {
expect(validatorFn).toBeCalledTimes(0);
});
});

describe('fieldsToValidateOnChange', () => {
const getTestForm =
(configField1?: FieldConfig, configField2?: FieldConfig) =>
({ showField1, showField2 }: { showField1: boolean; showField2: boolean }) => {
const { form } = useForm();

return (
<Form form={form}>
{showField1 && <UseField path="field1" component={() => null} config={configField1} />}
{showField2 && <UseField path="field2" component={() => null} config={configField2} />}
</Form>
);
};

test('validates dependent fields on unmount', async () => {
const field2ValidatorFn = jest.fn();
const TestForm = getTestForm(
{
fieldsToValidateOnChange: ['field1', 'field2'],
},
{
validations: [
{
validator: field2ValidatorFn,
},
],
}
);

const wrapper = registerTestBed(TestForm, {
memoryRouter: { wrapComponent: false },
})({ showField1: true, showField2: true });
expect(field2ValidatorFn).toBeCalledTimes(0);

await act(async () => {
wrapper.setProps({ showField1: false });
});
expect(field2ValidatorFn).toBeCalledTimes(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,14 @@ export const useField = <T, FormType = FormData, I = T>(
// We only remove the field from the form "fieldsRefs" map when its path
// changes (which in practice never occurs) or whenever the <UseField /> unmounts
__removeField(path);

// We also have to trigger validation of dependant fields
const dependantFields = fieldsToValidateOnChange?.filter((f) => f !== path);
if (dependantFields?.length) {
validateFields(dependantFields);
}
};
}, [path, __removeField]);
}, [path, __removeField, fieldsToValidateOnChange, validateFields]);

// Value change: notify prop listener (<UseField onChange={() => {...}})
// We have a separate useEffect for this as the "onChange" handler pass through prop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ describe('<EditPolicy /> downsample interval validation', () => {
'cold'
);

// disable warm phase;
// disable warm phase, check that we now get an error because of the hot phase;
await actions.togglePhase('warm');
// TODO: there is a bug that disabling a phase doesn't trigger downsample validation in other phases,
// users can work around it by changing the value
actions.errors.waitForValidation();
actions.errors.expectMessages(
['Must be greater than and a multiple of the hot phase value (60m)'],
'cold'
);
await actions.cold.downsample.setDownsampleInterval('120', 'm');
actions.errors.waitForValidation();
actions.errors.expectMessages([], 'cold');
Expand Down