|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { Provider } from 'react-redux'; |
| 5 | +import { |
| 6 | + HashRouter as Router, |
| 7 | + RouteComponentProps, |
| 8 | + Route, |
| 9 | + Switch, |
| 10 | +} from 'react-router-dom'; |
| 11 | +import { render, fireEvent, waitFor } from '@testing-library/react'; |
| 12 | +import { SampleAnomalies } from '../SampleAnomalies'; |
| 13 | +import configureStore from '../../../../redux/configureStore'; |
| 14 | +import { httpClientMock, coreServicesMock } from '../../../../../test/mocks'; |
| 15 | +import { mockedStore } from '../../../../redux/utils/testUtils'; |
| 16 | +import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices'; |
| 17 | +import { prepareDetector, focusOnFirstWrongFeature } from '../../utils/helpers'; |
| 18 | +import { createMemoryHistory } from 'history'; |
| 19 | +import { |
| 20 | + FeaturesFormikValues, |
| 21 | + ImputationFormikValues, |
| 22 | + RuleFormikValues, |
| 23 | +} from '../../../../pages/ConfigureModel/models/interfaces'; |
| 24 | +import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils'; |
| 25 | +import { FEATURE_TYPE } from '../../../../models/interfaces'; |
| 26 | + |
| 27 | +// Mock the helper functions |
| 28 | +jest.mock('../../utils/helpers', () => ({ |
| 29 | + ...jest.requireActual('../../utils/helpers'), |
| 30 | + prepareDetector: jest.fn(() => ({ |
| 31 | + // mock a detector object with at least an id, preventing the undefined error when detector.id is accessed in getSampleAdResult. |
| 32 | + id: 'test-detector-id', |
| 33 | + name: 'test-detector', |
| 34 | + description: 'test-detector-description', |
| 35 | + })), |
| 36 | + focusOnFirstWrongFeature: jest.fn(() => false), |
| 37 | +})); |
| 38 | + |
| 39 | +// Mock the Redux actions if necessary |
| 40 | +jest.mock('../../../../redux/reducers/previewAnomalies', () => ({ |
| 41 | + previewDetector: jest.fn(() => async () => Promise.resolve()), |
| 42 | +})); |
| 43 | + |
| 44 | +describe('<SampleAnomalies /> spec', () => { |
| 45 | + beforeEach(() => { |
| 46 | + jest.clearAllMocks(); |
| 47 | + console.error = jest.fn(); |
| 48 | + console.warn = jest.fn(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('calls prepareDetector with imputationOption and suppressionRules when previewDetector button is clicked', async () => { |
| 52 | + // Set up the mock props |
| 53 | + const mockDetector = getRandomDetector(); |
| 54 | + |
| 55 | + const mockFeatureList: FeaturesFormikValues[] = [ |
| 56 | + { |
| 57 | + featureId: 'feature1', |
| 58 | + featureName: 'Feature 1', |
| 59 | + featureEnabled: true, |
| 60 | + aggregationBy: 'sum', |
| 61 | + aggregationOf: [{ label: 'bytes' }], |
| 62 | + featureType: FEATURE_TYPE.SIMPLE, |
| 63 | + aggregationQuery: '', |
| 64 | + }, |
| 65 | + ]; |
| 66 | + |
| 67 | + const mockImputationOption: ImputationFormikValues = { |
| 68 | + imputationMethod: 'zero', |
| 69 | + }; |
| 70 | + |
| 71 | + const mockSuppressionRules: RuleFormikValues[] = [ |
| 72 | + { |
| 73 | + featureName: '', |
| 74 | + absoluteThreshold: undefined, |
| 75 | + relativeThreshold: undefined, |
| 76 | + aboveBelow: 'above', |
| 77 | + }, |
| 78 | + ]; |
| 79 | + |
| 80 | + const props = { |
| 81 | + detector: mockDetector, |
| 82 | + featureList: mockFeatureList, |
| 83 | + shingleSize: 8, |
| 84 | + categoryFields: ['category1'], |
| 85 | + errors: {}, |
| 86 | + setFieldTouched: jest.fn(), |
| 87 | + imputationOption: mockImputationOption, |
| 88 | + suppressionRules: mockSuppressionRules, |
| 89 | + }; |
| 90 | + |
| 91 | + // Create a mock history and store |
| 92 | + const history = createMemoryHistory(); |
| 93 | + const initialState = { |
| 94 | + anomalies: { |
| 95 | + anomaliesResult: { |
| 96 | + anomalies: [], |
| 97 | + }, |
| 98 | + }, |
| 99 | + }; |
| 100 | + const store = mockedStore(); |
| 101 | + |
| 102 | + const { getByText } = render( |
| 103 | + <Provider store={store}> |
| 104 | + <Router history={history}> |
| 105 | + <CoreServicesContext.Provider value={coreServicesMock}> |
| 106 | + <SampleAnomalies {...props} /> |
| 107 | + </CoreServicesContext.Provider> |
| 108 | + </Router> |
| 109 | + </Provider> |
| 110 | + ); |
| 111 | + |
| 112 | + // Simulate clicking the previewDetector button |
| 113 | + fireEvent.click(getByText('Preview anomalies')); |
| 114 | + |
| 115 | + // Wait for async actions to complete |
| 116 | + await waitFor(() => { |
| 117 | + expect(prepareDetector).toHaveBeenCalledWith( |
| 118 | + props.featureList, |
| 119 | + props.shingleSize, |
| 120 | + props.categoryFields, |
| 121 | + expect.anything(), // newDetector (could be the same as props.detector or modified) |
| 122 | + true, |
| 123 | + props.imputationOption, |
| 124 | + props.suppressionRules |
| 125 | + ); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments