Skip to content

Commit

Permalink
[FEATURE] Create global state object for async requests opensearch-pr…
Browse files Browse the repository at this point in the history
…oject#491

Signed-off-by: Jovan Cvetkovic <[email protected]>
  • Loading branch information
jovancvetkovic3006 committed Mar 26, 2023
1 parent 9daf6d9 commit bb04a08
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ exports[`<Alerts /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -138,6 +141,7 @@ exports[`<Alerts /> spec renders the component 1`] = `
"location": Object {
"pathname": "",
},
"push": [MockFunction],
"replace": [MockFunction],
}
}
Expand All @@ -158,6 +162,9 @@ exports[`<Alerts /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ exports[`<DetectorRulesView /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ exports[`<UpdateAlertConditions /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -999,6 +1002,9 @@ exports[`<UpdateAlertConditions /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ exports[`<UpdateDetectorBasicDetails /> spec renders the component 1`] = `
"location": Object {
"pathname": "",
},
"push": [MockFunction],
"replace": [MockFunction] {
"calls": Array [
Array [
Expand Down Expand Up @@ -576,6 +577,9 @@ exports[`<UpdateDetectorBasicDetails /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -904,6 +908,9 @@ exports[`<UpdateDetectorBasicDetails /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ exports[`<AlertTriggersView /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
"location": Object {
"pathname": "",
},
"push": [MockFunction],
"replace": [MockFunction],
}
}
Expand All @@ -207,6 +208,9 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -887,6 +891,7 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
"location": Object {
"pathname": "",
},
"push": [MockFunction],
"replace": [MockFunction],
}
}
Expand All @@ -901,6 +906,9 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -1293,6 +1301,7 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
"location": Object {
"pathname": "",
},
"push": [MockFunction],
"replace": [MockFunction],
}
}
Expand All @@ -1307,6 +1316,9 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -2648,6 +2660,7 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
"location": Object {
"pathname": "",
},
"push": [MockFunction],
"replace": [MockFunction],
}
}
Expand All @@ -2662,6 +2675,9 @@ exports[`<DetectorDetails /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ exports[`<DetectorDetailsView /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -377,6 +380,9 @@ exports[`<DetectorDetailsView /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down Expand Up @@ -1517,6 +1523,9 @@ exports[`<DetectorDetailsView /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ exports[`<Detectors /> spec renders the component 1`] = `
Object {
"toasts": Object {
"addDanger": [MockFunction],
"addInfo": [MockFunction],
"addSuccess": [MockFunction],
"addWarning": [MockFunction],
},
}
}
Expand Down
103 changes: 103 additions & 0 deletions public/store/DetectorsStore.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions public/store/DetectorsStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/mocks/services/browserHistory.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default ({
location: {
pathname: '',
},
push: jest.fn(),
} as unknown) as History;
3 changes: 3 additions & 0 deletions test/mocks/services/notifications/NotificationsStart.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit bb04a08

Please sign in to comment.