Add whatwg-fetch and fixes to dashboard tests that break when polyfill is added

This commit is contained in:
Tom Ratcliffe 2024-04-04 11:51:52 +01:00 committed by Tom Ratcliffe
parent 2674043cef
commit 2ab13071fb
3 changed files with 20 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import 'whatwg-fetch';
import { act, render } from '@testing-library/react'; import { act, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import React from 'react'; import React from 'react';
@ -7,6 +8,7 @@ import { byTestId } from 'testing-library-selector';
import { DataSourceApi } from '@grafana/data'; import { DataSourceApi } from '@grafana/data';
import { PromOptions, PrometheusDatasource } from '@grafana/prometheus'; import { PromOptions, PrometheusDatasource } from '@grafana/prometheus';
import { locationService, setDataSourceSrv } from '@grafana/runtime'; import { locationService, setDataSourceSrv } from '@grafana/runtime';
import { backendSrv } from 'app/core/services/backend_srv';
import { fetchRules } from 'app/features/alerting/unified/api/prometheus'; import { fetchRules } from 'app/features/alerting/unified/api/prometheus';
import { fetchRulerRules } from 'app/features/alerting/unified/api/ruler'; import { fetchRulerRules } from 'app/features/alerting/unified/api/ruler';
import * as ruleActionButtons from 'app/features/alerting/unified/components/rules/RuleActionsButtons'; import * as ruleActionButtons from 'app/features/alerting/unified/components/rules/RuleActionsButtons';
@ -14,6 +16,7 @@ import {
MockDataSourceSrv, MockDataSourceSrv,
grantUserPermissions, grantUserPermissions,
mockDataSource, mockDataSource,
mockFolder,
mockPromAlertingRule, mockPromAlertingRule,
mockPromRuleGroup, mockPromRuleGroup,
mockPromRuleNamespace, mockPromRuleNamespace,
@ -192,6 +195,9 @@ describe('PanelAlertTabContent', () => {
AccessControlAction.AlertingRuleExternalRead, AccessControlAction.AlertingRuleExternalRead,
AccessControlAction.AlertingRuleExternalWrite, AccessControlAction.AlertingRuleExternalWrite,
]); ]);
jest.spyOn(backendSrv, 'getFolderByUid').mockResolvedValue(mockFolder());
mocks.getAllDataSources.mockReturnValue(Object.values(dataSources)); mocks.getAllDataSources.mockReturnValue(Object.values(dataSources));
const dsService = new MockDataSourceSrv(dataSources); const dsService = new MockDataSourceSrv(dataSources);
dsService.datasources[dataSources.prometheus.uid] = new PrometheusDatasource( dsService.datasources[dataSources.prometheus.uid] = new PrometheusDatasource(

View File

@ -1,3 +1,4 @@
import 'whatwg-fetch';
import { render, screen, waitFor } from '@testing-library/react'; import { render, screen, waitFor } from '@testing-library/react';
import React from 'react'; import React from 'react';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
@ -7,6 +8,7 @@ import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock';
import { selectors as e2eSelectors } from '@grafana/e2e-selectors'; import { selectors as e2eSelectors } from '@grafana/e2e-selectors';
import { config, locationService } from '@grafana/runtime'; import { config, locationService } from '@grafana/runtime';
import { GrafanaContext } from 'app/core/context/GrafanaContext'; import { GrafanaContext } from 'app/core/context/GrafanaContext';
import { backendSrv } from 'app/core/services/backend_srv';
import { configureStore } from 'app/store/configureStore'; import { configureStore } from 'app/store/configureStore';
import { DashboardRoutes } from '../../../types'; import { DashboardRoutes } from '../../../types';
@ -50,6 +52,11 @@ function setup(props: Partial<PublicDashboardPageProxyProps>) {
describe('PublicDashboardPageProxy', () => { describe('PublicDashboardPageProxy', () => {
beforeEach(() => { beforeEach(() => {
config.featureToggles.publicDashboardsScene = false; config.featureToggles.publicDashboardsScene = false;
// Mock the dashboard UID response so we don't get any refused connection errors
// from this test (as the fetch polyfill means this logic would actually try and call the API)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(backendSrv, 'getPublicDashboardByUid').mockResolvedValue({ dashboard: {}, meta: {} } as any);
}); });
describe('when scene feature enabled', () => { describe('when scene feature enabled', () => {

View File

@ -1,3 +1,4 @@
import 'whatwg-fetch';
import { act, render, screen, waitFor } from '@testing-library/react'; import { act, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event'; import userEvent from '@testing-library/user-event';
import React, { ReactNode } from 'react'; import React, { ReactNode } from 'react';
@ -56,6 +57,12 @@ describe('AddToDashboardButton', () => {
setEchoSrv(new Echo()); setEchoSrv(new Echo());
}); });
beforeEach(() => {
// Mock the search response so we don't get any refused connection errors
// from this test (as the fetch polyfill means this logic would actually try and call the API)
jest.spyOn(backendSrv, 'search').mockResolvedValue([]);
});
it('Is disabled if explore pane has no queries', async () => { it('Is disabled if explore pane has no queries', async () => {
setup(<AddToDashboard exploreId={'left'} />, []); setup(<AddToDashboard exploreId={'left'} />, []);