From bb04a080bdd02c582a21f233f070fc00960d3b14 Mon Sep 17 00:00:00 2001 From: Jovan Cvetkovic Date: Sun, 26 Mar 2023 21:13:07 +0200 Subject: [PATCH] [FEATURE] Create global state object for async requests #491 Signed-off-by: Jovan Cvetkovic --- .../Alerts/__snapshots__/Alerts.test.tsx.snap | 7 ++ .../DetectorRulesView.test.tsx.snap | 3 + .../UpdateAlertConditions.test.tsx.snap | 6 + .../UpdateDetectorBasicDetails.test.tsx.snap | 7 ++ .../AlertTriggersView.test.tsx.snap | 3 + .../DetectorDetails.test.tsx.snap | 16 +++ .../DetectorDetailsView.test.tsx.snap | 9 ++ .../__snapshots__/Detectors.test.tsx.snap | 3 + public/store/DetectorsStore.test.ts | 103 ++++++++++++++++++ public/store/DetectorsStore.tsx | 1 + test/mocks/services/browserHistory.mock.ts | 1 + .../notifications/NotificationsStart.mock.ts | 3 + 12 files changed, 162 insertions(+) create mode 100644 public/store/DetectorsStore.test.ts diff --git a/public/pages/Alerts/containers/Alerts/__snapshots__/Alerts.test.tsx.snap b/public/pages/Alerts/containers/Alerts/__snapshots__/Alerts.test.tsx.snap index 050feb771..5f531740b 100644 --- a/public/pages/Alerts/containers/Alerts/__snapshots__/Alerts.test.tsx.snap +++ b/public/pages/Alerts/containers/Alerts/__snapshots__/Alerts.test.tsx.snap @@ -49,6 +49,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -138,6 +141,7 @@ exports[` spec renders the component 1`] = ` "location": Object { "pathname": "", }, + "push": [MockFunction], "replace": [MockFunction], } } @@ -158,6 +162,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/components/DetectorRulesView/__snapshots__/DetectorRulesView.test.tsx.snap b/public/pages/Detectors/components/DetectorRulesView/__snapshots__/DetectorRulesView.test.tsx.snap index 6598f2bc1..6216984cc 100644 --- a/public/pages/Detectors/components/DetectorRulesView/__snapshots__/DetectorRulesView.test.tsx.snap +++ b/public/pages/Detectors/components/DetectorRulesView/__snapshots__/DetectorRulesView.test.tsx.snap @@ -183,6 +183,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/components/UpdateAlertConditions/__snapshots__/UpdateAlertConditions.test.tsx.snap b/public/pages/Detectors/components/UpdateAlertConditions/__snapshots__/UpdateAlertConditions.test.tsx.snap index 5c1981715..735f7d5f9 100644 --- a/public/pages/Detectors/components/UpdateAlertConditions/__snapshots__/UpdateAlertConditions.test.tsx.snap +++ b/public/pages/Detectors/components/UpdateAlertConditions/__snapshots__/UpdateAlertConditions.test.tsx.snap @@ -392,6 +392,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -999,6 +1002,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/components/UpdateBasicDetails/__snapshots__/UpdateDetectorBasicDetails.test.tsx.snap b/public/pages/Detectors/components/UpdateBasicDetails/__snapshots__/UpdateDetectorBasicDetails.test.tsx.snap index a787a5377..b020ea5a1 100644 --- a/public/pages/Detectors/components/UpdateBasicDetails/__snapshots__/UpdateDetectorBasicDetails.test.tsx.snap +++ b/public/pages/Detectors/components/UpdateBasicDetails/__snapshots__/UpdateDetectorBasicDetails.test.tsx.snap @@ -190,6 +190,7 @@ exports[` spec renders the component 1`] = ` "location": Object { "pathname": "", }, + "push": [MockFunction], "replace": [MockFunction] { "calls": Array [ Array [ @@ -576,6 +577,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -904,6 +908,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/containers/AlertTriggersView/__snapshots__/AlertTriggersView.test.tsx.snap b/public/pages/Detectors/containers/AlertTriggersView/__snapshots__/AlertTriggersView.test.tsx.snap index 4b134ef5d..2083bd05c 100644 --- a/public/pages/Detectors/containers/AlertTriggersView/__snapshots__/AlertTriggersView.test.tsx.snap +++ b/public/pages/Detectors/containers/AlertTriggersView/__snapshots__/AlertTriggersView.test.tsx.snap @@ -183,6 +183,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/containers/Detector/__snapshots__/DetectorDetails.test.tsx.snap b/public/pages/Detectors/containers/Detector/__snapshots__/DetectorDetails.test.tsx.snap index e492bc285..45b6924a6 100644 --- a/public/pages/Detectors/containers/Detector/__snapshots__/DetectorDetails.test.tsx.snap +++ b/public/pages/Detectors/containers/Detector/__snapshots__/DetectorDetails.test.tsx.snap @@ -195,6 +195,7 @@ exports[` spec renders the component 1`] = ` "location": Object { "pathname": "", }, + "push": [MockFunction], "replace": [MockFunction], } } @@ -207,6 +208,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -887,6 +891,7 @@ exports[` spec renders the component 1`] = ` "location": Object { "pathname": "", }, + "push": [MockFunction], "replace": [MockFunction], } } @@ -901,6 +906,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -1293,6 +1301,7 @@ exports[` spec renders the component 1`] = ` "location": Object { "pathname": "", }, + "push": [MockFunction], "replace": [MockFunction], } } @@ -1307,6 +1316,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -2648,6 +2660,7 @@ exports[` spec renders the component 1`] = ` "location": Object { "pathname": "", }, + "push": [MockFunction], "replace": [MockFunction], } } @@ -2662,6 +2675,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/containers/DetectorDetailsView/__snapshots__/DetectorDetailsView.test.tsx.snap b/public/pages/Detectors/containers/DetectorDetailsView/__snapshots__/DetectorDetailsView.test.tsx.snap index 3f9cac30e..d59f77923 100644 --- a/public/pages/Detectors/containers/DetectorDetailsView/__snapshots__/DetectorDetailsView.test.tsx.snap +++ b/public/pages/Detectors/containers/DetectorDetailsView/__snapshots__/DetectorDetailsView.test.tsx.snap @@ -186,6 +186,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -377,6 +380,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } @@ -1517,6 +1523,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/pages/Detectors/containers/Detectors/__snapshots__/Detectors.test.tsx.snap b/public/pages/Detectors/containers/Detectors/__snapshots__/Detectors.test.tsx.snap index 7cf9f7c8f..0a66bbe88 100644 --- a/public/pages/Detectors/containers/Detectors/__snapshots__/Detectors.test.tsx.snap +++ b/public/pages/Detectors/containers/Detectors/__snapshots__/Detectors.test.tsx.snap @@ -23,6 +23,9 @@ exports[` spec renders the component 1`] = ` Object { "toasts": Object { "addDanger": [MockFunction], + "addInfo": [MockFunction], + "addSuccess": [MockFunction], + "addWarning": [MockFunction], }, } } diff --git a/public/store/DetectorsStore.test.ts b/public/store/DetectorsStore.test.ts new file mode 100644 index 000000000..09fc82794 --- /dev/null +++ b/public/store/DetectorsStore.test.ts @@ -0,0 +1,103 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { DataStore } from './DataStore'; +import notificationsStartMock from '../../test/mocks/services/notifications/NotificationsStart.mock'; +import services from '../../test/mocks/services'; +import { DetectorsStore } from './DetectorsStore'; +import { expect } from '@jest/globals'; +import detectorResponseMock from '../../test/mocks/Detectors/containers/Detectors/DetectorResponse.mock'; +import browserHistoryMock from '../../test/mocks/services/browserHistory.mock'; +import { CreateDetectorState } from '../pages/CreateDetector/containers/CreateDetector'; +import DetectorMock from '../../test/mocks/Detectors/containers/Detectors/Detector.mock'; +describe('Detectors store specs', () => { + Object.assign(services, { + detectorService: { + getRules: () => Promise.resolve(detectorResponseMock), + deleteRule: () => Promise.resolve(true), + }, + }); + + DataStore.init(services, notificationsStartMock); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('detectors store should be created', () => { + expect(DataStore.detectors instanceof DetectorsStore).toBe(true); + }); + + it('should handle the state', () => { + DataStore.detectors.setState( + { + pendingRequests: [Promise.resolve()], + detectorState: { + detector: { detector_type: 'test_detector_type' } as typeof DetectorMock, + } as CreateDetectorState, + }, + browserHistoryMock + ); + + let state = DataStore.detectors.getState(); + expect(state?.detectorState?.detector.detector_type).toBe('test_detector_type'); + + DataStore.detectors.deleteState(); + state = DataStore.detectors.getState(); + expect(state).toBe(undefined); + }); + + it('should get successful pending state', async () => { + DataStore.detectors.setState( + { + pendingRequests: [ + Promise.resolve({ + ok: true, + }), + Promise.resolve({ + ok: true, + response: { + _id: '', + detector: { + detector_type: '', + inputs: [ + { + detector_input: { + indices: [], + }, + }, + ], + }, + }, + }), + ], + detectorState: { + detector: { detector_type: 'test_detector_type' } as typeof DetectorMock, + } as CreateDetectorState, + }, + browserHistoryMock + ); + const pending = await DataStore.detectors.getPendingState(); + expect(pending.ok).toBe(true); + }); + + it('should get failed pending state', async () => { + DataStore.detectors.setState( + { + pendingRequests: [ + Promise.resolve({ + ok: false, + }), + ], + detectorState: { + detector: { detector_type: 'test_detector_type' } as typeof DetectorMock, + } as CreateDetectorState, + }, + browserHistoryMock + ); + const pending = await DataStore.detectors.getPendingState(); + expect(pending.ok).toBe(false); + }); +}); diff --git a/public/store/DetectorsStore.tsx b/public/store/DetectorsStore.tsx index 829318de6..ef0ba8cdb 100644 --- a/public/store/DetectorsStore.tsx +++ b/public/store/DetectorsStore.tsx @@ -206,6 +206,7 @@ export class DetectorsStore implements IDetectorsStore { dashboardId?: string; ok: boolean; }> => { + debugger; if (this.state?.pendingRequests) { const [mappingsResponse, detectorResponse] = (await Promise.all( this.state?.pendingRequests diff --git a/test/mocks/services/browserHistory.mock.ts b/test/mocks/services/browserHistory.mock.ts index a40a1a2ff..9af44b50c 100644 --- a/test/mocks/services/browserHistory.mock.ts +++ b/test/mocks/services/browserHistory.mock.ts @@ -9,4 +9,5 @@ export default ({ location: { pathname: '', }, + push: jest.fn(), } as unknown) as History; diff --git a/test/mocks/services/notifications/NotificationsStart.mock.ts b/test/mocks/services/notifications/NotificationsStart.mock.ts index 73f62608c..f1afac33f 100644 --- a/test/mocks/services/notifications/NotificationsStart.mock.ts +++ b/test/mocks/services/notifications/NotificationsStart.mock.ts @@ -8,5 +8,8 @@ import { NotificationsStart } from 'opensearch-dashboards/public'; export default ({ toasts: { addDanger: jest.fn(), + addWarning: jest.fn(), + addSuccess: jest.fn(), + addInfo: jest.fn(), }, } as unknown) as NotificationsStart;