diff --git a/src/components/CheckEditor/CheckEditor.test.tsx b/src/components/CheckEditor/CheckEditor.test.tsx index 40a51e46d..331bf7cc2 100644 --- a/src/components/CheckEditor/CheckEditor.test.tsx +++ b/src/components/CheckEditor/CheckEditor.test.tsx @@ -136,6 +136,7 @@ describe('PING', () => { it('correctly populates default values', async () => { const check = { + id: 32, job: 'carne asada', target: 'target.com', enabled: true, @@ -182,9 +183,10 @@ describe('HTTP', () => { expect(advanced).toBeInTheDocument(); }); - it('correctly populates default values', async () => { + it('correctly populates default values for preexisting check', async () => { const check = { job: 'carne asada', + id: 32, target: 'https://target.com', enabled: true, labels: [{ name: 'a great label', value: 'totally awesome label' }], @@ -262,6 +264,7 @@ describe('HTTP', () => { expect(await within(advancedOptions).findByPlaceholderText('name')).toHaveValue('a great label'); expect(await within(advancedOptions).findByPlaceholderText('value')).toHaveValue('totally awesome label'); expect(await within(advancedOptions).findByText('V6')).toBeInTheDocument(); + // Follow redirect field expect(await within(advancedOptions).findByRole('checkbox')).not.toBeChecked(); expect( await within(advancedOptions).findByLabelText('Cache busting query parameter name', { exact: false }) diff --git a/src/components/CheckEditor/CheckEditor.tsx b/src/components/CheckEditor/CheckEditor.tsx index 7268938da..bd4e1e19f 100644 --- a/src/components/CheckEditor/CheckEditor.tsx +++ b/src/components/CheckEditor/CheckEditor.tsx @@ -27,7 +27,7 @@ import { CHECK_TYPE_OPTIONS } from 'components/constants'; import { useForm, FormContext, Controller } from 'react-hook-form'; interface Props { - check: Check; + check?: Check; instance: SMDataSource; onReturn: (reload: boolean) => void; } @@ -44,7 +44,7 @@ export const CheckEditor: FC = ({ check, instance, onReturn }) => { const { execute: onSubmit, error, loading: submitting } = useAsyncCallback(async (values: CheckFormValues) => { const updatedCheck = getCheckFromFormValues(values, defaultValues); - if (check.id) { + if (check?.id) { await instance.updateCheck({ id: check.id, tenantId: check.tenantId, @@ -59,7 +59,7 @@ export const CheckEditor: FC = ({ check, instance, onReturn }) => { const submissionError = error as SubmissionError; const onRemoveCheck = async () => { - const id = check.id; + const id = check?.id; if (!id) { return; } diff --git a/src/components/CheckEditor/checkFormTransformations.ts b/src/components/CheckEditor/checkFormTransformations.ts index 98bf608e0..eff1ea032 100644 --- a/src/components/CheckEditor/checkFormTransformations.ts +++ b/src/components/CheckEditor/checkFormTransformations.ts @@ -25,6 +25,7 @@ import { HttpSslOption, HttpRegexValidationType, HeaderMatch, + DnsResponseCodes, } from 'types'; import { @@ -36,6 +37,22 @@ import { } from 'components/constants'; import { checkType } from 'utils'; +const fallbackCheck = { + job: '', + target: '', + frequency: 60000, + timeout: 3000, + enabled: true, + labels: [], + probes: [], + settings: { + ping: { + ipVersion: IpVersion.V4, + dontFragment: false, + }, + }, +} as Check; + export function selectableValueFrom(value: T, label?: string): SelectableValue { const labelValue = String(value); return { label: label ?? labelValue, value }; @@ -68,6 +85,7 @@ export function fallbackSettings(t: CheckType): Settings { ipVersion: IpVersion.V4, protocol: DnsProtocol.UDP, port: 53, + validRCodes: [DnsResponseCodes.NOERROR], }, }; } @@ -248,8 +266,19 @@ const getFormSettingsForCheck = (settings: Settings): SettingsFormValues => { } }; -export const getDefaultValuesFromCheck = (check: Check): CheckFormValues => { +const getAllFormSettingsForCheck = (): SettingsFormValues => { + return { + http: getHttpSettingsFormValues(fallbackSettings(CheckType.HTTP)), + tcp: getTcpSettingsFormValues(fallbackSettings(CheckType.TCP)), + dns: getDnsSettingsFormValues(fallbackSettings(CheckType.DNS)), + ping: getPingSettingsFormValues(fallbackSettings(CheckType.PING)), + }; +}; + +export const getDefaultValuesFromCheck = (check: Check = fallbackCheck): CheckFormValues => { const defaultCheckType = checkType(check.settings); + const settings = check.id ? getFormSettingsForCheck(check.settings) : getAllFormSettingsForCheck(); + return { ...check, timeout: check.timeout / 1000, @@ -257,7 +286,7 @@ export const getDefaultValuesFromCheck = (check: Check): CheckFormValues => { probes: check.probes, checkType: CHECK_TYPE_OPTIONS.find(checkTypeOption => checkTypeOption.value === defaultCheckType) ?? CHECK_TYPE_OPTIONS[1], - settings: getFormSettingsForCheck(check.settings), + settings, }; }; diff --git a/src/components/DnsSettings.test.tsx b/src/components/DnsSettings.test.tsx deleted file mode 100644 index 6fac03fbc..000000000 --- a/src/components/DnsSettings.test.tsx +++ /dev/null @@ -1,55 +0,0 @@ -import React from 'react'; -import DnsSettingsForm from './DnsSettings'; -import { screen, render } from '@testing-library/react'; -import userEvent from '@testing-library/user-event'; -import { DnsResponseCodes, Label } from 'types'; -import { MockFormWrapper } from '../__mocks__/MockFormWrapper'; - -jest.unmock('utils'); -jest.setTimeout(10000); - -const onUpdateMock = jest.fn(); -const defaultSettings = {}; -const defaultLabels: Label[] = []; - -const renderDnsSettings = ({ isEditor = true, settings = defaultSettings, labels = defaultLabels } = {}) => { - return render( - - - - ); -}; - -beforeEach(() => { - onUpdateMock.mockReset(); -}); - -describe('Default values', () => { - test('response codes default to NOERROR', async () => { - renderDnsSettings(); - const validationExpandButton = await screen.findByText('Validation'); - userEvent.click(validationExpandButton); - const noErrorResponseCode = await screen.findByText(DnsResponseCodes.NOERROR); - expect(noErrorResponseCode).toBeInTheDocument(); - }); - - test('DNS Settings', async () => { - renderDnsSettings(); - const dnsSettings = await screen.findByText('DNS settings'); - userEvent.click(dnsSettings); - expect(await screen.findByText('A')).toBeInTheDocument(); - expect(await screen.findByText('UDP')).toBeInTheDocument(); - const server = await screen.findByLabelText('Server'); - expect(server).toHaveValue('8.8.8.8'); - const port = await screen.findByLabelText('Port'); - expect(port).toHaveValue(53); - }); - - test('Advanced options', async () => { - renderDnsSettings(); - const advancedExpand = await screen.findByText('Advanced options'); - userEvent.click(advancedExpand); - const ipInput = await screen.findByText('V4'); - expect(ipInput).toBeInTheDocument(); - }); -}); diff --git a/src/components/DnsSettings.tsx b/src/components/DnsSettings.tsx index a0da9962c..60b9e314c 100644 --- a/src/components/DnsSettings.tsx +++ b/src/components/DnsSettings.tsx @@ -14,11 +14,10 @@ import { useTheme, } from '@grafana/ui'; import { useFormContext, Controller, useFieldArray } from 'react-hook-form'; -import { CheckType, ResponseMatchType } from 'types'; +import { ResponseMatchType } from 'types'; import { Collapse } from 'components/Collapse'; import { LabelField } from './LabelField'; import { DNS_RESPONSE_CODES, DNS_RECORD_TYPES, DNS_PROTOCOLS, IP_OPTIONS } from './constants'; -import { fallbackSettings } from './CheckEditor/checkFormTransformations'; interface Props { isEditor: boolean; @@ -30,8 +29,6 @@ const RESPONSE_MATCH_OPTIONS = [ { label: `Validate ${ResponseMatchType.Additional} matches`, value: ResponseMatchType.Additional }, ]; -const fallbackValues = fallbackSettings(CheckType.DNS); - const DnsSettingsForm: FC = ({ isEditor }) => { const { spacing } = useTheme(); @@ -58,12 +55,7 @@ const DnsSettingsForm: FC = ({ isEditor }) => { `} > - + = ({ isEditor }) => { name="settings.dns.server" type="text" placeholder="server" - defaultValue={fallbackValues.dns?.server} /> - + - + @@ -178,7 +157,7 @@ const DnsSettingsForm: FC = ({ isEditor }) => { - + diff --git a/src/components/http/HttpSettings.tsx b/src/components/http/HttpSettings.tsx index 3c0e70b38..027d239c6 100644 --- a/src/components/http/HttpSettings.tsx +++ b/src/components/http/HttpSettings.tsx @@ -201,13 +201,7 @@ export const HttpSettingsForm: FC = ({ isEditor }) => { invalid={Boolean(errors?.settings?.http?.method)} error={errors?.settings?.http?.method} > - + diff --git a/src/page/ChecksPage.tsx b/src/page/ChecksPage.tsx index 3dc50c516..d0e780ee2 100644 --- a/src/page/ChecksPage.tsx +++ b/src/page/ChecksPage.tsx @@ -2,7 +2,7 @@ import React, { PureComponent } from 'react'; // Types -import { Check, GrafanaInstances, IpVersion } from 'types'; +import { Check, GrafanaInstances } from 'types'; import { getLocationSrv } from '@grafana/runtime'; import { CheckEditor } from 'components/CheckEditor'; import { CheckList } from 'components/CheckList'; @@ -91,22 +91,7 @@ export class ChecksPage extends PureComponent { return ; } if (addNew) { - const template = { - job: '', - target: '', - frequency: 60000, - timeout: 3000, - enabled: true, - labels: [], - probes: [], - settings: { - ping: { - ipVersion: IpVersion.V4, - dontFragment: false, - }, - }, - } as Check; - return ; + return ; } return ; }