-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Create global state object for async requests opensearch-pr…
…oject#491 Signed-off-by: Jovan Cvetkovic <[email protected]>
- Loading branch information
1 parent
9daf6d9
commit bb04a08
Showing
12 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ export default ({ | |
location: { | ||
pathname: '', | ||
}, | ||
push: jest.fn(), | ||
} as unknown) as History; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters