-
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.
Merge branch 'master' into ml-feature-importance-functional-tests
- Loading branch information
Showing
84 changed files
with
3,108 additions
and
470 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
Empty file.
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
163 changes: 163 additions & 0 deletions
163
...h/public/applications/app_search/components/engine_overview/engine_overview_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,163 @@ | ||
/* | ||
* 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'; | ||
|
||
import { mockHttpValues } from '../../../__mocks__'; | ||
jest.mock('../../../shared/http', () => ({ | ||
HttpLogic: { values: mockHttpValues }, | ||
})); | ||
const { http } = mockHttpValues; | ||
|
||
jest.mock('../../../shared/flash_messages', () => ({ | ||
flashAPIErrors: jest.fn(), | ||
})); | ||
import { flashAPIErrors } from '../../../shared/flash_messages'; | ||
|
||
jest.mock('../engine', () => ({ | ||
EngineLogic: { values: { engineName: 'some-engine' } }, | ||
})); | ||
|
||
import { EngineOverviewLogic } from './'; | ||
|
||
describe('EngineOverviewLogic', () => { | ||
const mockEngineMetrics = { | ||
apiLogsUnavailable: true, | ||
documentCount: 10, | ||
startDate: '1970-01-30', | ||
endDate: '1970-01-31', | ||
operationsPerDay: [0, 0, 0, 0, 0, 0, 0], | ||
queriesPerDay: [0, 0, 0, 0, 0, 25, 50], | ||
totalClicks: 50, | ||
totalQueries: 75, | ||
}; | ||
|
||
const DEFAULT_VALUES = { | ||
dataLoading: true, | ||
apiLogsUnavailable: false, | ||
documentCount: 0, | ||
startDate: '', | ||
endDate: '', | ||
operationsPerDay: [], | ||
queriesPerDay: [], | ||
totalClicks: 0, | ||
totalQueries: 0, | ||
timeoutId: null, | ||
}; | ||
|
||
const mount = () => { | ||
resetContext({}); | ||
EngineOverviewLogic.mount(); | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('has expected default values', () => { | ||
mount(); | ||
expect(EngineOverviewLogic.values).toEqual(DEFAULT_VALUES); | ||
}); | ||
|
||
describe('actions', () => { | ||
describe('setPolledData', () => { | ||
it('should set all received data as top-level values and set dataLoading to false', () => { | ||
mount(); | ||
EngineOverviewLogic.actions.setPolledData(mockEngineMetrics); | ||
|
||
expect(EngineOverviewLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
...mockEngineMetrics, | ||
dataLoading: false, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('setTimeoutId', () => { | ||
describe('timeoutId', () => { | ||
it('should be set to the provided value', () => { | ||
mount(); | ||
EngineOverviewLogic.actions.setTimeoutId(123); | ||
|
||
expect(EngineOverviewLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
timeoutId: 123, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('pollForOverviewMetrics', () => { | ||
it('fetches data and calls onPollingSuccess', async () => { | ||
mount(); | ||
jest.spyOn(EngineOverviewLogic.actions, 'onPollingSuccess'); | ||
const promise = Promise.resolve(mockEngineMetrics); | ||
http.get.mockReturnValueOnce(promise); | ||
|
||
EngineOverviewLogic.actions.pollForOverviewMetrics(); | ||
await promise; | ||
|
||
expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine/overview'); | ||
expect(EngineOverviewLogic.actions.onPollingSuccess).toHaveBeenCalledWith( | ||
mockEngineMetrics | ||
); | ||
}); | ||
|
||
it('handles errors', async () => { | ||
mount(); | ||
const promise = Promise.reject('An error occurred'); | ||
http.get.mockReturnValue(promise); | ||
|
||
try { | ||
EngineOverviewLogic.actions.pollForOverviewMetrics(); | ||
await promise; | ||
} catch { | ||
// Do nothing | ||
} | ||
expect(flashAPIErrors).toHaveBeenCalledWith('An error occurred'); | ||
}); | ||
}); | ||
|
||
describe('onPollingSuccess', () => { | ||
it('starts a polling timeout and sets data', async () => { | ||
mount(); | ||
jest.useFakeTimers(); | ||
jest.spyOn(EngineOverviewLogic.actions, 'setTimeoutId'); | ||
jest.spyOn(EngineOverviewLogic.actions, 'setPolledData'); | ||
|
||
EngineOverviewLogic.actions.onPollingSuccess(mockEngineMetrics); | ||
|
||
expect(setTimeout).toHaveBeenCalledWith( | ||
EngineOverviewLogic.actions.pollForOverviewMetrics, | ||
5000 | ||
); | ||
expect(EngineOverviewLogic.actions.setTimeoutId).toHaveBeenCalledWith(expect.any(Number)); | ||
expect(EngineOverviewLogic.actions.setPolledData).toHaveBeenCalledWith(mockEngineMetrics); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('unmount', () => { | ||
let unmount: Function; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
resetContext({}); | ||
unmount = EngineOverviewLogic.mount(); | ||
}); | ||
|
||
it('clears existing polling timeouts on unmount', () => { | ||
EngineOverviewLogic.actions.setTimeoutId(123); | ||
unmount(); | ||
expect(clearTimeout).toHaveBeenCalled(); | ||
}); | ||
|
||
it("does not clear timeout if one hasn't been set", () => { | ||
unmount(); | ||
expect(clearTimeout).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.