diff --git a/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx b/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx index 8545ea4f1d6..b76654076cf 100644 --- a/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorGrafanaRules.test.tsx @@ -1,7 +1,6 @@ import 'whatwg-fetch'; import { screen, waitFor, waitForElementToBeRemoved, within } from '@testing-library/react'; import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event'; -import { setupServer } from 'msw/node'; import React from 'react'; import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor'; import { clickSelectOption } from 'test/helpers/selectOptionInTest'; @@ -9,7 +8,11 @@ import { byRole } from 'testing-library-selector'; import { setDataSourceSrv } from '@grafana/runtime'; import { contextSrv } from 'app/core/services/context_srv'; -import { mockApi } from 'app/features/alerting/unified/mockApi'; +import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; +import { + defaultAlertmanagerChoiceResponse, + mockAlertmanagerChoiceResponse, +} from 'app/features/alerting/unified/mocks/alertmanagerApi'; import { DashboardSearchHit, DashboardSearchItemType } from 'app/features/search/types'; import { AccessControlAction } from 'app/types'; import { GrafanaAlertStateDecision, PromApplication } from 'app/types/unified-alerting-dto'; @@ -64,13 +67,13 @@ const mocks = { }, }; -const server = setupServer(); +const server = setupMswServer(); const getLabelInput = (selector: HTMLElement) => within(selector).getByRole('combobox'); describe('RuleEditor grafana managed rules', () => { beforeEach(() => { mockApi(server).eval({ results: {} }); - server.listen(); + mockAlertmanagerChoiceResponse(server, defaultAlertmanagerChoiceResponse); jest.clearAllMocks(); contextSrv.isEditor = true; contextSrv.hasEditPermissionInFolders = true; @@ -89,14 +92,6 @@ describe('RuleEditor grafana managed rules', () => { ]); }); - afterEach(() => { - server.resetHandlers(); - }); - - afterAll(() => { - server.close(); - }); - it('can create new grafana managed alert', async () => { const dataSources = { default: mockDataSource( diff --git a/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx b/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx index 806bc82c75d..b09d022eb3f 100644 --- a/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx +++ b/public/app/features/alerting/unified/RuleEditorRecordingRule.test.tsx @@ -1,7 +1,6 @@ import 'whatwg-fetch'; import { screen, waitFor, waitForElementToBeRemoved, within } from '@testing-library/react'; import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event'; -import { setupServer } from 'msw/node'; import React from 'react'; import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor'; import { clickSelectOption } from 'test/helpers/selectOptionInTest'; @@ -9,7 +8,7 @@ import { byText } from 'testing-library-selector'; import { setDataSourceSrv } from '@grafana/runtime'; import { contextSrv } from 'app/core/services/context_srv'; -import { mockApi } from 'app/features/alerting/unified/mockApi'; +import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; import { AccessControlAction } from 'app/types'; import { PromApplication } from 'app/types/unified-alerting-dto'; @@ -96,12 +95,11 @@ const mocks = { const getLabelInput = (selector: HTMLElement) => within(selector).getByRole('combobox'); -const server = setupServer(); +const server = setupMswServer(); describe('RuleEditor recording rules', () => { beforeEach(() => { mockApi(server).eval({ results: {} }); - server.listen(); jest.clearAllMocks(); contextSrv.isEditor = true; contextSrv.hasEditPermissionInFolders = true; @@ -120,14 +118,6 @@ describe('RuleEditor recording rules', () => { ]); }); - afterEach(() => { - server.resetHandlers(); - }); - - afterAll(() => { - server.close(); - }); - it('can create a new cloud recording rule', async () => { setDataSourceSrv(new MockDataSourceSrv(dataSources)); mocks.getAllDataSources.mockReturnValue(Object.values(dataSources)); diff --git a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx index 5ac83f63d87..1ae84f9f1d6 100644 --- a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/simplifiedRouting/SimplifiedRuleEditor.test.tsx @@ -1,7 +1,6 @@ import 'whatwg-fetch'; import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { setupServer } from 'msw/node'; import React from 'react'; import { Route } from 'react-router-dom'; import { TestProvider } from 'test/helpers/TestProvider'; @@ -21,7 +20,7 @@ import { } from 'app/features/alerting/unified/api/ruler'; import * as useContactPoints from 'app/features/alerting/unified/components/contact-points/useContactPoints'; import * as dsByPermission from 'app/features/alerting/unified/hooks/useAlertManagerSources'; -import { mockApi } from 'app/features/alerting/unified/mockApi'; +import { mockApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; import { MockDataSourceSrv, grantUserPermissions, mockDataSource } from 'app/features/alerting/unified/mocks'; import { AlertmanagerProvider } from 'app/features/alerting/unified/state/AlertmanagerContext'; import { fetchRulerRulesIfNotFetchedYet } from 'app/features/alerting/unified/state/actions'; @@ -105,12 +104,11 @@ const mocks = { }, }; -const server = setupServer(); +const server = setupMswServer(); describe('Can create a new grafana managed alert unsing simplified routing', () => { beforeEach(() => { mockApi(server).eval({ results: {} }); - server.listen(); jest.clearAllMocks(); contextSrv.isEditor = true; contextSrv.hasEditPermissionInFolders = true; @@ -142,14 +140,6 @@ describe('Can create a new grafana managed alert unsing simplified routing', () }); }); - afterEach(() => { - server.resetHandlers(); - }); - - afterAll(() => { - server.close(); - }); - const dataSources = { default: mockDataSource( { diff --git a/public/app/features/alerting/unified/components/rules/RulesFilter.test.tsx b/public/app/features/alerting/unified/components/rules/RulesFilter.test.tsx index dcadfb9d858..eeb0791da31 100644 --- a/public/app/features/alerting/unified/components/rules/RulesFilter.test.tsx +++ b/public/app/features/alerting/unified/components/rules/RulesFilter.test.tsx @@ -1,20 +1,19 @@ import 'whatwg-fetch'; import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { setupServer } from 'msw/node'; import React from 'react'; import { TestProvider } from 'test/helpers/TestProvider'; import { byLabelText, byRole } from 'testing-library-selector'; import { locationService, setDataSourceSrv } from '@grafana/runtime'; -import { mockSearchApi } from 'app/features/alerting/unified/mockApi'; +import { mockSearchApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; import * as analytics from '../../Analytics'; import { MockDataSourceSrv } from '../../mocks'; import RulesFilter from './RulesFilter'; -const server = setupServer(); +const server = setupMswServer(); jest.spyOn(analytics, 'logInfo'); jest.mock('./MultipleDataSourcePicker', () => { @@ -44,15 +43,6 @@ const ui = { beforeEach(() => { locationService.replace({ search: '' }); mockSearchApi(server).search([]); - server.listen({ onUnhandledRequest: 'error' }); -}); - -afterEach(() => { - server.resetHandlers(); -}); - -afterAll(() => { - server.close(); }); describe('RulesFilter', () => { diff --git a/public/app/features/alerting/unified/hooks/useAbilities.test.tsx b/public/app/features/alerting/unified/hooks/useAbilities.test.tsx index 935708701b2..0a626fdd737 100644 --- a/public/app/features/alerting/unified/hooks/useAbilities.test.tsx +++ b/public/app/features/alerting/unified/hooks/useAbilities.test.tsx @@ -6,7 +6,11 @@ import React, { PropsWithChildren } from 'react'; import { Router } from 'react-router-dom'; import { TestProvider } from 'test/helpers/TestProvider'; -import { mockFolderApi } from 'app/features/alerting/unified/mockApi'; +import { mockFolderApi, setupMswServer } from 'app/features/alerting/unified/mockApi'; +import { + defaultAlertmanagerChoiceResponse, + mockAlertmanagerChoiceResponse, +} from 'app/features/alerting/unified/mocks/alertmanagerApi'; import { AlertManagerDataSourceJsonData, AlertManagerImplementation } from 'app/plugins/datasource/alertmanager/types'; import { AccessControlAction } from 'app/types'; import { RulerGrafanaRuleDTO } from 'app/types/unified-alerting-dto'; @@ -141,17 +145,18 @@ describe('alertmanager abilities', () => { }); describe('AlertRule abilities', () => { + const server = setupMswServer(); it('should report that all actions are supported for a Grafana Managed alert rule', async () => { const rule = getGrafanaRule(); // TODO: Remove server mocking within test once server is run before all tests - const server = setupServer(); mockFolderApi(server).folder( (rule.rulerRule as RulerGrafanaRuleDTO).grafana_alert.namespace_uid, mockFolder({ accessControl: { [AccessControlAction.AlertingRuleUpdate]: false }, }) ); + mockAlertmanagerChoiceResponse(server, defaultAlertmanagerChoiceResponse); server.listen(); const abilities = renderHook(() => useAllAlertRuleAbilities(rule), { wrapper: TestProvider }); @@ -165,8 +170,6 @@ describe('AlertRule abilities', () => { }); expect(abilities.result.current).toMatchSnapshot(); - - server.close(); }); it('should report no permissions while we are loading data for cloud rule', async () => {