|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { getRuleHealthColor, getRuleStatusMessage } from './rule_status_helpers'; |
| 9 | +import { RuleTableItem } from '../../types'; |
| 10 | + |
| 11 | +import { getIsExperimentalFeatureEnabled } from '../get_experimental_features'; |
| 12 | + |
| 13 | +jest.mock('../get_experimental_features', () => ({ |
| 14 | + getIsExperimentalFeatureEnabled: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +const mockRule = { |
| 18 | + id: '1', |
| 19 | + enabled: true, |
| 20 | + executionStatus: { |
| 21 | + status: 'active', |
| 22 | + }, |
| 23 | + lastRun: { |
| 24 | + outcome: 'succeeded', |
| 25 | + }, |
| 26 | +} as RuleTableItem; |
| 27 | + |
| 28 | +const warningRule = { |
| 29 | + ...mockRule, |
| 30 | + executionStatus: { |
| 31 | + status: 'warning', |
| 32 | + }, |
| 33 | + lastRun: { |
| 34 | + outcome: 'warning', |
| 35 | + }, |
| 36 | +} as RuleTableItem; |
| 37 | + |
| 38 | +const failedRule = { |
| 39 | + ...mockRule, |
| 40 | + executionStatus: { |
| 41 | + status: 'error', |
| 42 | + }, |
| 43 | + lastRun: { |
| 44 | + outcome: 'failed', |
| 45 | + }, |
| 46 | +} as RuleTableItem; |
| 47 | + |
| 48 | +const licenseErrorRule = { |
| 49 | + ...mockRule, |
| 50 | + executionStatus: { |
| 51 | + status: 'error', |
| 52 | + error: { |
| 53 | + reason: 'license', |
| 54 | + }, |
| 55 | + }, |
| 56 | + lastRun: { |
| 57 | + outcome: 'failed', |
| 58 | + warning: 'license', |
| 59 | + }, |
| 60 | +} as RuleTableItem; |
| 61 | + |
| 62 | +beforeEach(() => { |
| 63 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => true); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('getRuleHealthColor', () => { |
| 67 | + it('should return the correct color for successful rule', () => { |
| 68 | + let color = getRuleHealthColor(mockRule); |
| 69 | + expect(color).toEqual('success'); |
| 70 | + |
| 71 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 72 | + |
| 73 | + color = getRuleHealthColor(mockRule); |
| 74 | + expect(color).toEqual('success'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return the correct color for warning rule', () => { |
| 78 | + let color = getRuleHealthColor(warningRule); |
| 79 | + expect(color).toEqual('warning'); |
| 80 | + |
| 81 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 82 | + |
| 83 | + color = getRuleHealthColor(warningRule); |
| 84 | + expect(color).toEqual('warning'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return the correct color for failed rule', () => { |
| 88 | + let color = getRuleHealthColor(failedRule); |
| 89 | + expect(color).toEqual('danger'); |
| 90 | + |
| 91 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 92 | + |
| 93 | + color = getRuleHealthColor(failedRule); |
| 94 | + expect(color).toEqual('danger'); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('getRuleStatusMessage', () => { |
| 99 | + it('should get the status message for a successful rule', () => { |
| 100 | + let statusMessage = getRuleStatusMessage(mockRule); |
| 101 | + expect(statusMessage).toEqual('Succeeded'); |
| 102 | + |
| 103 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 104 | + statusMessage = getRuleStatusMessage(mockRule); |
| 105 | + expect(statusMessage).toEqual('Active'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should get the status message for a warning rule', () => { |
| 109 | + let statusMessage = getRuleStatusMessage(warningRule); |
| 110 | + expect(statusMessage).toEqual('Warning'); |
| 111 | + |
| 112 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 113 | + statusMessage = getRuleStatusMessage(warningRule); |
| 114 | + expect(statusMessage).toEqual('Warning'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should get the status message for a failed rule', () => { |
| 118 | + let statusMessage = getRuleStatusMessage(failedRule); |
| 119 | + expect(statusMessage).toEqual('Failed'); |
| 120 | + |
| 121 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 122 | + statusMessage = getRuleStatusMessage(failedRule); |
| 123 | + expect(statusMessage).toEqual('Error'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should get the status message for a license error rule', () => { |
| 127 | + let statusMessage = getRuleStatusMessage(licenseErrorRule); |
| 128 | + expect(statusMessage).toEqual('License Error'); |
| 129 | + |
| 130 | + (getIsExperimentalFeatureEnabled as jest.Mock<any, any>).mockImplementation(() => false); |
| 131 | + statusMessage = getRuleStatusMessage(licenseErrorRule); |
| 132 | + expect(statusMessage).toEqual('License Error'); |
| 133 | + }); |
| 134 | +}); |
0 commit comments