From b91b9a1e38878f7c408129ff2c9c70e6d6bb70d5 Mon Sep 17 00:00:00 2001 From: Tom Ratcliffe Date: Fri, 11 Oct 2024 11:08:50 +0100 Subject: [PATCH] Alerting: Use `jest/expect-expect` rule to check for assertions (#93112) --- public/app/features/alerting/.eslintrc | 1 + .../alerting/unified/MuteTimings.test.tsx | 16 ++++++++-------- .../features/alerting/unified/Receivers.test.tsx | 6 +++--- .../unified/RuleEditorGrafanaRules.test.tsx | 3 ++- .../alerting/unified/hooks/useAbilities.test.tsx | 4 ---- public/test/helpers/alertingRuleEditor.tsx | 12 ++++++++---- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/public/app/features/alerting/.eslintrc b/public/app/features/alerting/.eslintrc index 00287bc0bb8..c34b924496a 100644 --- a/public/app/features/alerting/.eslintrc +++ b/public/app/features/alerting/.eslintrc @@ -12,6 +12,7 @@ "extends": ["plugin:testing-library/react"], "rules": { "testing-library/prefer-user-event": "error", + "jest/expect-expect": ["error", { "assertFunctionNames": ["expect*", "reducerTester"] }], }, }, ], diff --git a/public/app/features/alerting/unified/MuteTimings.test.tsx b/public/app/features/alerting/unified/MuteTimings.test.tsx index c329ddcecab..c583d45599a 100644 --- a/public/app/features/alerting/unified/MuteTimings.test.tsx +++ b/public/app/features/alerting/unified/MuteTimings.test.tsx @@ -158,7 +158,7 @@ const defaultConfigWithBothTimeIntervalsField: AlertManagerCortexConfig = { template_files: {}, }; -const expectedToHaveRedirectedToRoutesRoute = async () => +const expectToHaveRedirectedToRoutesRoute = async () => expect(await screen.findByText(indexPageText)).toBeInTheDocument(); const fillOutForm = async ({ @@ -226,7 +226,7 @@ describe('Mute timings', () => { await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); const requests = await capture; const alertmanagerUpdate = await getAlertmanagerConfigUpdate(requests); @@ -252,7 +252,7 @@ describe('Mute timings', () => { }); await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); const requests = await capture; const alertmanagerUpdate = await getAlertmanagerConfigUpdate(requests); @@ -278,7 +278,7 @@ describe('Mute timings', () => { }); await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); }); it('prepopulates the form when editing a mute timing', async () => { @@ -310,7 +310,7 @@ describe('Mute timings', () => { await fillOutForm(formValues); await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); const requests = await capture; const alertmanagerUpdate = await getAlertmanagerConfigUpdate(requests); @@ -345,7 +345,7 @@ describe('Mute timings', () => { await fillOutForm({ name: 'Lunch breaks' }); await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); }); it('shows error when mute timing does not exist', async () => { @@ -367,7 +367,7 @@ describe('Mute timings', () => { await fillOutForm({ name: 'a new mute timing' }); await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); }); it('shows error when mute timing does not exist', async () => { @@ -384,7 +384,7 @@ describe('Mute timings', () => { }); await saveMuteTiming(); - await expectedToHaveRedirectedToRoutesRoute(); + await expectToHaveRedirectedToRoutesRoute(); }); it('loads view form for provisioned interval', async () => { diff --git a/public/app/features/alerting/unified/Receivers.test.tsx b/public/app/features/alerting/unified/Receivers.test.tsx index 6a4d4227e6f..36b18b52ee3 100644 --- a/public/app/features/alerting/unified/Receivers.test.tsx +++ b/public/app/features/alerting/unified/Receivers.test.tsx @@ -19,7 +19,7 @@ import NewReceiverView from './components/receivers/NewReceiverView'; const server = setupMswServer(); -const assertSaveWasSuccessful = async () => { +const expectSaveWasSuccessful = async () => { // TODO: Have a better way to assert that the contact point was saved. This is instead asserting on some // text that's present on the list page, as there's a lot of overlap in text between the form and the list page return waitFor(() => expect(screen.getByText(/search by name or type/i)).toBeInTheDocument(), { timeout: 2000 }); @@ -77,7 +77,7 @@ it('can save a contact point with a select dropdown', async () => { await saveContactPoint(); - await assertSaveWasSuccessful(); + await expectSaveWasSuccessful(); }); it('can save existing Telegram contact point', async () => { @@ -89,5 +89,5 @@ it('can save existing Telegram contact point', async () => { // trigger this error if it regresses await saveContactPoint(); - await assertSaveWasSuccessful(); + await expectSaveWasSuccessful(); }); diff --git a/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx b/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx index be6cb47b1a7..40fd64998b4 100644 --- a/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx @@ -118,7 +118,8 @@ describe('RuleEditor grafana managed rules', () => { await clickSelectOption(groupInput, grafanaRulerGroup.name); await userEvent.type(ui.inputs.annotationValue(1).get(), 'some description'); - // save and check what was sent to backend await userEvent.click(ui.buttons.saveAndExit.get()); + + expect(await screen.findByRole('status')).toHaveTextContent('Rule added successfully'); }); }); diff --git a/public/app/features/alerting/unified/hooks/useAbilities.test.tsx b/public/app/features/alerting/unified/hooks/useAbilities.test.tsx index 510e5390379..e58a09f6666 100644 --- a/public/app/features/alerting/unified/hooks/useAbilities.test.tsx +++ b/public/app/features/alerting/unified/hooks/useAbilities.test.tsx @@ -207,10 +207,6 @@ describe('AlertRule abilities', () => { expect(result.current).toMatchSnapshot(); }); - - it('should not allow certain actions for provisioned rules', () => {}); - - it('should not allow certain actions for federated rules', () => {}); }); function createAlertmanagerWrapper(alertmanagerSourceName: string) { diff --git a/public/test/helpers/alertingRuleEditor.tsx b/public/test/helpers/alertingRuleEditor.tsx index e07a335fad9..465b2ef0dc3 100644 --- a/public/test/helpers/alertingRuleEditor.tsx +++ b/public/test/helpers/alertingRuleEditor.tsx @@ -3,6 +3,7 @@ import { render } from 'test/test-utils'; import { byRole, byTestId, byText } from 'testing-library-selector'; import { selectors } from '@grafana/e2e-selectors'; +import { AppNotificationList } from 'app/core/components/AppNotifications/AppNotificationList'; import RuleEditor from 'app/features/alerting/unified/RuleEditor'; export const ui = { @@ -36,10 +37,13 @@ export const ui = { export function renderRuleEditor(identifier?: string, recording = false) { return render( - - } /> - } /> - , + <> + + + } /> + } /> + + , { historyOptions: { initialEntries: [