From a0062cddad4321b7920ba1a6818aadab37fe4a1f Mon Sep 17 00:00:00 2001 From: Virginia Cepeda Date: Tue, 18 Feb 2025 13:58:19 -0300 Subject: [PATCH] fix: prevent submitting alerts per check when FF is off (#1068) --- src/components/CheckForm/checkForm.hooks.ts | 8 ++++--- .../CommonFields.payload.test.tsx | 24 +++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/components/CheckForm/checkForm.hooks.ts b/src/components/CheckForm/checkForm.hooks.ts index 645d83137..a61e55bcd 100644 --- a/src/components/CheckForm/checkForm.hooks.ts +++ b/src/components/CheckForm/checkForm.hooks.ts @@ -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'; @@ -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); @@ -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) => { diff --git a/src/page/NewCheck/__tests__/ApiEndPointChecks/CommonFields.payload.test.tsx b/src/page/NewCheck/__tests__/ApiEndPointChecks/CommonFields.payload.test.tsx index 3fbd6ed9e..5270cc3bd 100644 --- a/src/page/NewCheck/__tests__/ApiEndPointChecks/CommonFields.payload.test.tsx +++ b/src/page/NewCheck/__tests__/ApiEndPointChecks/CommonFields.payload.test.tsx @@ -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); @@ -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)`, () => {