Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiWu committed Nov 15, 2022
1 parent 8d222b4 commit 725a38b
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ export const RulesList = ({
setShowErrors((prevValue) => {
if (!prevValue) {
const rulesToExpand = rulesState.data.reduce((acc, ruleItem) => {
if (ruleItem.lastRun?.outcome === RuleLastRunOutcomeValues[2]) {
// Check both outcome and executionStatus for now until we deprecate executionStatus
if (
ruleItem.lastRun?.outcome === RuleLastRunOutcomeValues[2] ||
ruleItem.executionStatus.status === 'error'
) {
return {
...acc,
[ruleItem.id]: (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getRuleHealthColor, getRuleStatusMessage } from './rule_status_helpers';
import { RuleTableItem } from '../../types';

import { getIsExperimentalFeatureEnabled } from '../get_experimental_features';

jest.mock('../get_experimental_features', () => ({
getIsExperimentalFeatureEnabled: jest.fn(),
}));

const mockRule = {
id: '1',
enabled: true,
executionStatus: {
status: 'active',
},
lastRun: {
outcome: 'succeeded',
},
} as RuleTableItem;

const warningRule = {
...mockRule,
executionStatus: {
status: 'warning',
},
lastRun: {
outcome: 'warning',
},
} as RuleTableItem;

const failedRule = {
...mockRule,
executionStatus: {
status: 'error',
},
lastRun: {
outcome: 'failed',
},
} as RuleTableItem;

const licenseErrorRule = {
...mockRule,
executionStatus: {
status: 'error',
error: {
reason: 'license',
},
},
lastRun: {
outcome: 'failed',
warning: 'license',
},
} as RuleTableItem;

beforeEach(() => {
(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => true);
});

describe('getRuleHealthColor', () => {
it('should return the correct color for successful rule', () => {
let color = getRuleHealthColor(mockRule);
expect(color).toEqual('success');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);

color = getRuleHealthColor(mockRule);
expect(color).toEqual('success');
});

it('should return the correct color for warning rule', () => {
let color = getRuleHealthColor(warningRule);
expect(color).toEqual('warning');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);

color = getRuleHealthColor(warningRule);
expect(color).toEqual('warning');
});

it('should return the correct color for failed rule', () => {
let color = getRuleHealthColor(failedRule);
expect(color).toEqual('danger');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);

color = getRuleHealthColor(failedRule);
expect(color).toEqual('danger');
});
});

describe('getRuleStatusMessage', () => {
it('should get the status message for a successful rule', () => {
let statusMessage = getRuleStatusMessage(mockRule);
expect(statusMessage).toEqual('Succeeded');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);
statusMessage = getRuleStatusMessage(mockRule);
expect(statusMessage).toEqual('Active');
});

it('should get the status message for a warning rule', () => {
let statusMessage = getRuleStatusMessage(warningRule);
expect(statusMessage).toEqual('Warning');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);
statusMessage = getRuleStatusMessage(warningRule);
expect(statusMessage).toEqual('Warning');
});

it('should get the status message for a failed rule', () => {
let statusMessage = getRuleStatusMessage(failedRule);
expect(statusMessage).toEqual('Failed');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);
statusMessage = getRuleStatusMessage(failedRule);
expect(statusMessage).toEqual('Error');
});

it('should get the status message for a license error rule', () => {
let statusMessage = getRuleStatusMessage(licenseErrorRule);
expect(statusMessage).toEqual('License Error');

(getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false);
statusMessage = getRuleStatusMessage(licenseErrorRule);
expect(statusMessage).toEqual('License Error');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export const getRuleHealthColor = (rule: Rule) => {
};

export const getIsLicenseError = (rule: Rule) => {
return rule.lastRun?.warning === RuleExecutionStatusErrorReasons.License;
return (
rule.lastRun?.warning === RuleExecutionStatusErrorReasons.License ||
rule.executionStatus.error?.reason === RuleExecutionStatusErrorReasons.License
);
};

export const getRuleStatusMessage = (rule: Rule) => {
Expand All @@ -67,7 +70,7 @@ export const getRuleStatusMessage = (rule: Rule) => {
return ALERT_STATUS_LICENSE_ERROR;
}
if (isRuleLastRunOutcomeEnabled) {
return (rule.lastRun && rulesLastRunOutcomeTranslationMapping[rule.lastRun.outcome]) || '';
return rule.lastRun && rulesLastRunOutcomeTranslationMapping[rule.lastRun.outcome];
}
return rulesStatusesTranslationsMapping[rule.executionStatus.status];
};

0 comments on commit 725a38b

Please sign in to comment.