From 0598d60234d15b9f20355bd6c06e8a0447723b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=A4ggmark?= Date: Wed, 25 Sep 2019 05:10:11 -0700 Subject: [PATCH] Tests: Adds throwUnhandledRejections to jest setup (#19398) * Chore: Adds throwUnhandledRejections to jest setup * Bring back @grafana/runtime import * Fix broken metric metadata aggregation test --- .../app/features/alerting/TestRuleResult.test.tsx | 6 +++--- public/app/features/alerting/TestRuleResult.tsx | 13 ++++++++++--- .../query_ctrl.test.ts | 12 ++++++++---- .../grafana-azure-monitor-datasource/query_ctrl.ts | 6 +++--- public/test/jest-setup.ts | 8 ++++++++ 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/public/app/features/alerting/TestRuleResult.test.tsx b/public/app/features/alerting/TestRuleResult.test.tsx index ff8422ccf4b..5ba4e8bef66 100644 --- a/public/app/features/alerting/TestRuleResult.test.tsx +++ b/public/app/features/alerting/TestRuleResult.test.tsx @@ -1,9 +1,9 @@ import React from 'react'; +import { TestRuleResult, Props } from './TestRuleResult'; +import { DashboardModel } from '../dashboard/state'; import { shallow } from 'enzyme'; -import { DashboardModel } from '../dashboard/state/DashboardModel'; -import { Props, TestRuleResult } from './TestRuleResult'; -jest.mock('app/core/services/backend_srv', () => ({ +jest.mock('@grafana/runtime/src/services/backendSrv', () => ({ getBackendSrv: () => ({ post: jest.fn(), }), diff --git a/public/app/features/alerting/TestRuleResult.tsx b/public/app/features/alerting/TestRuleResult.tsx index e6296b6998b..282f5e60303 100644 --- a/public/app/features/alerting/TestRuleResult.tsx +++ b/public/app/features/alerting/TestRuleResult.tsx @@ -1,9 +1,10 @@ import React, { PureComponent } from 'react'; +import { LoadingPlaceholder, JSONFormatter } from '@grafana/ui'; + import appEvents from 'app/core/app_events'; import { CopyToClipboard } from 'app/core/components/CopyToClipboard/CopyToClipboard'; -import { getBackendSrv } from '@grafana/runtime'; import { DashboardModel } from '../dashboard/state/DashboardModel'; -import { LoadingPlaceholder, JSONFormatter } from '@grafana/ui'; +import { getBackendSrv, BackendSrv } from '@grafana/runtime'; export interface Props { panelId: number; @@ -25,6 +26,12 @@ export class TestRuleResult extends PureComponent { formattedJson: any; clipboard: any; + backendSrv: BackendSrv = null; + + constructor(props: Props) { + super(props); + this.backendSrv = getBackendSrv(); + } componentDidMount() { this.testRule(); @@ -35,7 +42,7 @@ export class TestRuleResult extends PureComponent { const payload = { dashboard: dashboard.getSaveModelClone(), panelId }; this.setState({ isLoading: true }); - const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload); + const testRuleResponse = await this.backendSrv.post(`/api/alerts/test`, payload); this.setState({ isLoading: false, testRuleResponse }); } diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.test.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.test.ts index 9c83b5d9312..7e4bef5a9b8 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.test.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.test.ts @@ -194,8 +194,8 @@ describe('AzureMonitorQueryCtrl', () => { describe('when onMetricNameChange is triggered for the Metric Names dropdown', () => { const response: any = { primaryAggType: 'Average', - supportAggOptions: ['Average', 'Total'], - supportedTimeGrains: ['PT1M', 'P1D'], + supportedAggTypes: ['Average', 'Total'], + supportedTimeGrains: [{ text: 'PT1M', value: 'PT1M' }, { text: 'P1D', value: 'P1D' }], dimensions: [], }; @@ -227,8 +227,12 @@ describe('AzureMonitorQueryCtrl', () => { it('should set the options and default selected value for the Aggregations dropdown', () => { queryCtrl.onMetricNameChange().then(() => { expect(queryCtrl.target.azureMonitor.aggregation).toBe('Average'); - expect(queryCtrl.target.azureMonitor.aggOptions).toBe(['Average', 'Total']); - expect(queryCtrl.target.azureMonitor.timeGrains).toBe(['PT1M', 'P1D']); + expect(queryCtrl.target.azureMonitor.aggOptions).toEqual(['Average', 'Total']); + expect(queryCtrl.target.azureMonitor.timeGrains).toEqual([ + { text: 'auto', value: 'auto' }, + { text: 'PT1M', value: 'PT1M' }, + { text: 'P1D', value: 'P1D' }, + ]); }); }); }); diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts index 0e799d27518..545e77f4cd9 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/query_ctrl.ts @@ -6,7 +6,7 @@ import './editor/editor_component'; import kbn from 'app/core/utils/kbn'; import { TemplateSrv } from 'app/features/templating/template_srv'; -import { auto } from 'angular'; +import { auto, IPromise } from 'angular'; import { DataFrame } from '@grafana/data'; export interface ResultFormat { @@ -396,9 +396,9 @@ export class AzureMonitorQueryCtrl extends QueryCtrl { this.target.azureMonitor.dimension = ''; } - onMetricNameChange() { + onMetricNameChange(): IPromise { if (!this.target.azureMonitor.metricName || this.target.azureMonitor.metricName === this.defaultDropdownValue) { - return; + return Promise.resolve(); } return this.datasource diff --git a/public/test/jest-setup.ts b/public/test/jest-setup.ts index bfcceca7a59..4d4c748f166 100644 --- a/public/test/jest-setup.ts +++ b/public/test/jest-setup.ts @@ -41,3 +41,11 @@ const localStorageMock = (() => { global.localStorage = localStorageMock; // Object.defineProperty(window, 'localStorage', { value: localStorageMock }); + +const throwUnhandledRejections = () => { + process.on('unhandledRejection', err => { + throw err; + }); +}; + +throwUnhandledRejections();