-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enterprise Search] Convert IndexingStatus to use logic for fetching (#…
…84710) (#84825) * Add IndexingStatusLogic * Replace IndexingStatusFetcher with logic * Refactor out unnecessary conditional onComplete is not optional so these if blocks can be consolidated * Misc styling - destructuring and typing Co-authored-by: Constance <[email protected]> * Misc styling - imports Co-authored-by: Constance <[email protected]> * Remove div * Refactor test * Replace method with string for statusPath In ent-search, we use Rails helpers to generate paths. These were in the form of routes.whateverPath(). We passed these method to the IndexingStatus component to generate the app-specific rotues in the shared component. In Kibana, we will not have these generators and should instead pass the path strings directly Co-authored-by: Constance <[email protected]> Co-authored-by: Constance <[email protected]>
- Loading branch information
1 parent
9d71a8a
commit b9c3a96
Showing
5 changed files
with
247 additions
and
99 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
64 changes: 0 additions & 64 deletions
64
.../enterprise_search/public/applications/shared/indexing_status/indexing_status_fetcher.tsx
This file was deleted.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
...nterprise_search/public/applications/shared/indexing_status/indexing_status_logic.test.ts
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,110 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { resetContext } from 'kea'; | ||
|
||
jest.mock('../http', () => ({ | ||
HttpLogic: { | ||
values: { http: { get: jest.fn() } }, | ||
}, | ||
})); | ||
import { HttpLogic } from '../http'; | ||
|
||
jest.mock('../flash_messages', () => ({ | ||
flashAPIErrors: jest.fn(), | ||
})); | ||
import { flashAPIErrors } from '../flash_messages'; | ||
|
||
import { IndexingStatusLogic } from './indexing_status_logic'; | ||
|
||
describe('IndexingStatusLogic', () => { | ||
let unmount: any; | ||
|
||
const mockStatusResponse = { | ||
percentageComplete: 50, | ||
numDocumentsWithErrors: 3, | ||
activeReindexJobId: 1, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
resetContext({}); | ||
unmount = IndexingStatusLogic.mount(); | ||
}); | ||
|
||
it('has expected default values', () => { | ||
expect(IndexingStatusLogic.values).toEqual({ | ||
percentageComplete: 100, | ||
numDocumentsWithErrors: 0, | ||
}); | ||
}); | ||
|
||
describe('setIndexingStatus', () => { | ||
it('sets reducers', () => { | ||
IndexingStatusLogic.actions.setIndexingStatus(mockStatusResponse); | ||
|
||
expect(IndexingStatusLogic.values.percentageComplete).toEqual( | ||
mockStatusResponse.percentageComplete | ||
); | ||
expect(IndexingStatusLogic.values.numDocumentsWithErrors).toEqual( | ||
mockStatusResponse.numDocumentsWithErrors | ||
); | ||
}); | ||
}); | ||
|
||
describe('fetchIndexingStatus', () => { | ||
jest.useFakeTimers(); | ||
const statusPath = '/api/workplace_search/path/123'; | ||
const onComplete = jest.fn(); | ||
const TIMEOUT = 3000; | ||
|
||
it('calls API and sets values', async () => { | ||
const setIndexingStatusSpy = jest.spyOn(IndexingStatusLogic.actions, 'setIndexingStatus'); | ||
const promise = Promise.resolve(mockStatusResponse); | ||
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise); | ||
|
||
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete }); | ||
jest.advanceTimersByTime(TIMEOUT); | ||
|
||
expect(HttpLogic.values.http.get).toHaveBeenCalledWith(statusPath); | ||
await promise; | ||
|
||
expect(setIndexingStatusSpy).toHaveBeenCalledWith(mockStatusResponse); | ||
}); | ||
|
||
it('handles error', async () => { | ||
const promise = Promise.reject('An error occured'); | ||
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise); | ||
|
||
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete }); | ||
jest.advanceTimersByTime(TIMEOUT); | ||
|
||
try { | ||
await promise; | ||
} catch { | ||
// Do nothing | ||
} | ||
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured'); | ||
}); | ||
|
||
it('handles indexing complete state', async () => { | ||
const promise = Promise.resolve({ ...mockStatusResponse, percentageComplete: 100 }); | ||
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise); | ||
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete }); | ||
jest.advanceTimersByTime(TIMEOUT); | ||
|
||
await promise; | ||
|
||
expect(clearInterval).toHaveBeenCalled(); | ||
expect(onComplete).toHaveBeenCalledWith(mockStatusResponse.numDocumentsWithErrors); | ||
}); | ||
|
||
it('handles unmounting', async () => { | ||
unmount(); | ||
expect(clearInterval).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.