Skip to content

Commit

Permalink
fix: prevent submitting alerts per check when FF is off (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
VikaCep authored Feb 18, 2025
1 parent 68bd4bf commit a0062cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/components/CheckForm/checkForm.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { ScriptedCheckSchema } from 'schemas/forms/ScriptedCheckSchema';
import { TCPCheckSchema } from 'schemas/forms/TCPCheckSchema';
import { TracerouteCheckSchema } from 'schemas/forms/TracerouteCheckSchema';

import { Check, CheckAlertDraft, CheckAlertFormRecord, CheckFormValues, CheckType } from 'types';
import { Check, CheckAlertDraft, CheckAlertFormRecord, CheckFormValues, CheckType, FeatureName } from 'types';
import { ROUTES } from 'routing/types';
import { AdHocCheckResponse } from 'datasource/responses.types';
import { useUpdateAlertsForCheck } from 'data/useCheckAlerts';
import { useCUDChecks, useTestCheck } from 'data/useChecks';
import { useFeatureFlag } from 'hooks/useFeatureFlag';
import { useNavigation } from 'hooks/useNavigation';
import { toPayload } from 'components/CheckEditor/checkFormTransformations';
import { getAlertsPayload } from 'components/CheckEditor/transformations/toPayload.alerts';
Expand Down Expand Up @@ -54,6 +55,7 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
const { mutate: testCheck, isPending, error: testError } = useTestCheck({ eventInfo: { checkType } });

const navigateToChecks = useCallback(() => navigate(ROUTES.Checks), [navigate]);
const alertsEnabled = useFeatureFlag(FeatureName.AlertsPerCheck).isEnabled;

const onError = (err: Error | unknown) => {
setSubmittingToApi(false);
Expand Down Expand Up @@ -102,9 +104,9 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
return testCheck(toSubmit, { onSuccess: onTestSuccess });
}

mutateCheck(toSubmit, checkValues?.alerts);
mutateCheck(toSubmit, alertsEnabled ? checkValues?.alerts : undefined);
},
[mutateCheck, onTestSuccess, testCheck]
[mutateCheck, onTestSuccess, testCheck, alertsEnabled]
);

const handleInvalid = useCallback((errs: FieldErrors) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ describe('Api endpoint checks - common fields payload', () => {
[FeatureName.AlertsPerCheck]: true,
});


const { user, read } = await renderNewForm(checkType);

await fillMandatoryFields({ user, checkType });

await goToSection(user, 4);
Expand All @@ -122,6 +121,27 @@ describe('Api endpoint checks - common fields payload', () => {

expect(alertsBody).toEqual({ alerts: [{ name: 'ProbeFailedExecutionsTooHigh', threshold: 0.1 }] });
});

it(`does not submit aletrs per check when the feature flag is disabled`, async () => {
jest.replaceProperty(config, 'featureToggles', {
// @ts-expect-error
[FeatureName.AlertsPerCheck]: false,
});

const { user, read } = await renderNewForm(checkType);

await fillMandatoryFields({ user, checkType });

await goToSection(user, 4);

expect(screen.queryByText('Predefined alerts')).not.toBeInTheDocument();

await submitForm(user);

const { body: alertsBody } = await read(1);

expect(alertsBody).toEqual(undefined);
});
});

describe(`Section 5 (Execution)`, () => {
Expand Down

0 comments on commit a0062cd

Please sign in to comment.