|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { usePrefillAlertConditions } from '../usePrefillAlertConditions'; |
| 4 | + |
| 5 | +const TEST_MAPPINGS = { |
| 6 | + op: { |
| 7 | + '>': '1', |
| 8 | + '<': '2', |
| 9 | + '=': '3', |
| 10 | + }, |
| 11 | + matchType: { |
| 12 | + avg: '3', |
| 13 | + sum: '4', |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +jest.mock('react-router-dom-v5-compat', () => { |
| 18 | + const mockThreshold1 = { |
| 19 | + index: '0d11f426-a02e-48da-867c-b79c6ef1ff06', |
| 20 | + isEditEnabled: false, |
| 21 | + keyIndex: 1, |
| 22 | + selectedGraph: 'graph', |
| 23 | + thresholdColor: 'Orange', |
| 24 | + thresholdFormat: 'Text', |
| 25 | + thresholdLabel: 'Caution', |
| 26 | + thresholdOperator: '>', |
| 27 | + thresholdTableOptions: 'A', |
| 28 | + thresholdUnit: 'rpm', |
| 29 | + thresholdValue: 800, |
| 30 | + }; |
| 31 | + const mockThreshold2 = { |
| 32 | + index: 'edbe8ef2-fa54-4cb9-b343-7afe883bb714', |
| 33 | + isEditEnabled: false, |
| 34 | + keyIndex: 0, |
| 35 | + selectedGraph: 'graph', |
| 36 | + thresholdColor: 'Red', |
| 37 | + thresholdFormat: 'Text', |
| 38 | + thresholdLabel: 'Danger', |
| 39 | + thresholdOperator: '<', |
| 40 | + thresholdTableOptions: 'A', |
| 41 | + thresholdUnit: 'rpm', |
| 42 | + thresholdValue: 900, |
| 43 | + }; |
| 44 | + return { |
| 45 | + ...jest.requireActual('react-router-dom-v5-compat'), |
| 46 | + useLocation: jest.fn().mockReturnValue({ |
| 47 | + state: { |
| 48 | + thresholds: [mockThreshold1, mockThreshold2], |
| 49 | + }, |
| 50 | + }), |
| 51 | + }; |
| 52 | +}); |
| 53 | + |
| 54 | +const mockStagedQuery = { |
| 55 | + builder: { |
| 56 | + queryData: [ |
| 57 | + { |
| 58 | + reduceTo: 'avg', |
| 59 | + }, |
| 60 | + ], |
| 61 | + }, |
| 62 | +}; |
| 63 | + |
| 64 | +describe('usePrefillAlertConditions', () => { |
| 65 | + it('returns the correct matchType for a single query', () => { |
| 66 | + const { result } = renderHook(() => |
| 67 | + usePrefillAlertConditions(mockStagedQuery as any), |
| 68 | + ); |
| 69 | + expect(result.current.matchType).toBe(TEST_MAPPINGS.matchType.avg); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns null matchType for a single query with unsupported time aggregation', () => { |
| 73 | + const { result } = renderHook(() => |
| 74 | + usePrefillAlertConditions({ |
| 75 | + builder: { queryData: [{ reduceTo: 'p90' }] }, |
| 76 | + } as any), |
| 77 | + ); |
| 78 | + expect(result.current.matchType).toBe(null); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns the correct matchType for multiple queries with same time aggregation', () => { |
| 82 | + const { result } = renderHook(() => |
| 83 | + usePrefillAlertConditions({ |
| 84 | + builder: { |
| 85 | + queryData: [ |
| 86 | + { |
| 87 | + reduceTo: 'avg', |
| 88 | + }, |
| 89 | + { |
| 90 | + reduceTo: 'avg', |
| 91 | + }, |
| 92 | + ], |
| 93 | + }, |
| 94 | + } as any), |
| 95 | + ); |
| 96 | + expect(result.current.matchType).toBe(TEST_MAPPINGS.matchType.avg); |
| 97 | + }); |
| 98 | + |
| 99 | + it('returns null matchType for multiple queries with different time aggregation', () => { |
| 100 | + const { result } = renderHook(() => |
| 101 | + usePrefillAlertConditions({ |
| 102 | + builder: { |
| 103 | + queryData: [ |
| 104 | + { |
| 105 | + reduceTo: 'avg', |
| 106 | + }, |
| 107 | + { |
| 108 | + reduceTo: 'sum', |
| 109 | + }, |
| 110 | + ], |
| 111 | + }, |
| 112 | + } as any), |
| 113 | + ); |
| 114 | + expect(result.current.matchType).toBe(null); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns the correct op, target, targetUnit from the higher priority threshold for multiple thresholds', () => { |
| 118 | + const { result } = renderHook(() => |
| 119 | + usePrefillAlertConditions(mockStagedQuery as any), |
| 120 | + ); |
| 121 | + expect(result.current.op).toBe(TEST_MAPPINGS.op['<']); |
| 122 | + expect(result.current.target).toBe(900); |
| 123 | + expect(result.current.targetUnit).toBe('rpm'); |
| 124 | + }); |
| 125 | +}); |
0 commit comments