Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ export const mockFlashMessagesActions = {
setQueuedMessages: jest.fn(),
clearQueuedMessages: jest.fn(),
};

export const mockFlashMessageHelpers = {
flashAPIErrors: jest.fn(),
setSuccessMessage: jest.fn(),
setErrorMessage: jest.fn(),
setQueuedSuccessMessage: jest.fn(),
setQueuedErrorMessage: jest.fn(),
clearFlashMessages: jest.fn(),
};

jest.mock('../shared/flash_messages', () => ({
...(jest.requireActual('../shared/flash_messages') as object),
...mockFlashMessageHelpers,
FlashMessagesLogic: {
values: mockFlashMessagesValues,
actions: mockFlashMessagesActions,
},
}));
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export const mockHttpValues = {
errorConnecting: false,
readOnlyMode: false,
};

jest.mock('../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export { mockKibanaValues } from './kibana_logic.mock';
export { mockLicensingValues } from './licensing_logic.mock';
export { mockHttpValues } from './http_logic.mock';
export { mockTelemetryActions } from './telemetry_logic.mock';
export { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
export {
mockFlashMessagesValues,
mockFlashMessagesActions,
mockFlashMessageHelpers,
} from './flash_messages_logic.mock';
export {
mockAllValues,
mockAllActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ export const mockKibanaValues = {
setDocTitle: jest.fn(),
renderHeaderActions: jest.fn(),
};

jest.mock('../shared/kibana', () => ({
KibanaLogic: { values: mockKibanaValues },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ export const mockLicensingValues = {
hasPlatinumLicense: false,
hasGoldLicense: false,
};

jest.mock('../shared/licensing', () => ({
LicensingLogic: { values: mockLicensingValues },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ export const mockTelemetryActions = {
sendAppSearchTelemetry: jest.fn(),
sendWorkplaceSearchTelemetry: jest.fn(),
};

jest.mock('../shared/telemetry', () => ({
...(jest.requireActual('../shared/telemetry') as object),
TelemetryLogic: { actions: mockTelemetryActions },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter, expectedAsyncError } from '../../../__mocks__';

jest.mock('../../../shared/kibana', () => ({
KibanaLogic: { values: { history: { location: { search: '' } } } },
}));
import { KibanaLogic } from '../../../shared/kibana';

jest.mock('../../../shared/http', () => ({
HttpLogic: { values: { http: { get: jest.fn() } } },
}));
import { HttpLogic } from '../../../shared/http';

jest.mock('../../../shared/flash_messages', () => ({
flashAPIErrors: jest.fn(),
}));
import { flashAPIErrors } from '../../../shared/flash_messages';
import {
LogicMounter,
mockKibanaValues,
mockHttpValues,
mockFlashMessageHelpers,
expectedAsyncError,
} from '../../../__mocks__';

jest.mock('../engine', () => ({
EngineLogic: { values: { engineName: 'test-engine' } },
Expand All @@ -28,6 +19,11 @@ jest.mock('../engine', () => ({
import { AnalyticsLogic } from './';

describe('AnalyticsLogic', () => {
const { mount } = new LogicMounter(AnalyticsLogic);
const { history } = mockKibanaValues;
const { http } = mockHttpValues;
const { flashAPIErrors } = mockFlashMessageHelpers;

const DEFAULT_VALUES = {
dataLoading: true,
analyticsUnavailable: false,
Expand Down Expand Up @@ -88,11 +84,9 @@ describe('AnalyticsLogic', () => {
topClicksForQuery: MOCK_TOP_CLICKS,
};

const { mount } = new LogicMounter(AnalyticsLogic);

beforeEach(() => {
jest.clearAllMocks();
KibanaLogic.values.history.location.search = '';
history.location.search = '';
});

it('has expected default values', () => {
Expand Down Expand Up @@ -158,14 +152,14 @@ describe('AnalyticsLogic', () => {

it('should make an API call and set state based on the response', async () => {
const promise = Promise.resolve(MOCK_ANALYTICS_RESPONSE);
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
http.get.mockReturnValueOnce(promise);
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsDataLoad');

AnalyticsLogic.actions.loadAnalyticsData();
await promise;

expect(HttpLogic.values.http.get).toHaveBeenCalledWith(
expect(http.get).toHaveBeenCalledWith(
'/api/app_search/engines/test-engine/analytics/queries',
{
query: { size: 20 },
Expand All @@ -177,14 +171,13 @@ describe('AnalyticsLogic', () => {
});

it('parses and passes the current search query string', async () => {
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce({});
KibanaLogic.values.history.location.search =
'?start=1970-01-01&end=1970-01-02&&tag=some_tag';
(http.get as jest.Mock).mockReturnValueOnce({});
history.location.search = '?start=1970-01-01&end=1970-01-02&&tag=some_tag';
mount();

AnalyticsLogic.actions.loadAnalyticsData();

expect(HttpLogic.values.http.get).toHaveBeenCalledWith(
expect(http.get).toHaveBeenCalledWith(
'/api/app_search/engines/test-engine/analytics/queries',
{
query: {
Expand All @@ -199,7 +192,7 @@ describe('AnalyticsLogic', () => {

it('calls onAnalyticsUnavailable if analyticsUnavailable is in response', async () => {
const promise = Promise.resolve({ analyticsUnavailable: true });
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
http.get.mockReturnValueOnce(promise);
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');

Expand All @@ -211,7 +204,7 @@ describe('AnalyticsLogic', () => {

it('handles errors', async () => {
const promise = Promise.reject('error');
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
http.get.mockReturnValueOnce(promise);
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');

Expand All @@ -237,29 +230,28 @@ describe('AnalyticsLogic', () => {

it('should make an API call and set state based on the response', async () => {
const promise = Promise.resolve(MOCK_QUERY_RESPONSE);
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
http.get.mockReturnValueOnce(promise);
mount();
jest.spyOn(AnalyticsLogic.actions, 'onQueryDataLoad');

AnalyticsLogic.actions.loadQueryData('some-query');
await promise;

expect(HttpLogic.values.http.get).toHaveBeenCalledWith(
expect(http.get).toHaveBeenCalledWith(
'/api/app_search/engines/test-engine/analytics/queries/some-query',
expect.any(Object) // empty query obj
);
expect(AnalyticsLogic.actions.onQueryDataLoad).toHaveBeenCalledWith(MOCK_QUERY_RESPONSE);
});

it('parses and passes the current search query string', async () => {
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce({});
KibanaLogic.values.history.location.search =
'?start=1970-12-30&end=1970-12-31&&tag=another_tag';
(http.get as jest.Mock).mockReturnValueOnce({});
history.location.search = '?start=1970-12-30&end=1970-12-31&&tag=another_tag';
mount();

AnalyticsLogic.actions.loadQueryData('some-query');

expect(HttpLogic.values.http.get).toHaveBeenCalledWith(
expect(http.get).toHaveBeenCalledWith(
'/api/app_search/engines/test-engine/analytics/queries/some-query',
{
query: {
Expand All @@ -273,7 +265,7 @@ describe('AnalyticsLogic', () => {

it('calls onAnalyticsUnavailable if analyticsUnavailable is in response', async () => {
const promise = Promise.resolve({ analyticsUnavailable: true });
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
http.get.mockReturnValueOnce(promise);
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');

Expand All @@ -285,7 +277,7 @@ describe('AnalyticsLogic', () => {

it('handles errors', async () => {
const promise = Promise.reject('error');
(HttpLogic.values.http.get as jest.Mock).mockReturnValueOnce(promise);
http.get.mockReturnValueOnce(promise);
mount();
jest.spyOn(AnalyticsLogic.actions, 'onAnalyticsUnavailable');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LogicMounter, mockHttpValues, expectedAsyncError } from '../../../__mocks__';

jest.mock('../../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
const { http } = mockHttpValues;

jest.mock('../../../shared/flash_messages', () => ({
FlashMessagesLogic: { actions: { clearFlashMessages: jest.fn() } },
setSuccessMessage: jest.fn(),
flashAPIErrors: jest.fn(),
}));
import {
FlashMessagesLogic,
setSuccessMessage,
flashAPIErrors,
} from '../../../shared/flash_messages';
LogicMounter,
mockFlashMessageHelpers,
mockHttpValues,
expectedAsyncError,
} from '../../../__mocks__';

jest.mock('../../app_logic', () => ({
AppLogic: {
Expand All @@ -34,6 +23,10 @@ import { ApiTokenTypes } from './constants';
import { CredentialsLogic } from './credentials_logic';

describe('CredentialsLogic', () => {
const { mount } = new LogicMounter(CredentialsLogic);
const { http } = mockHttpValues;
const { clearFlashMessages, setSuccessMessage, flashAPIErrors } = mockFlashMessageHelpers;

const DEFAULT_VALUES = {
activeApiToken: {
name: '',
Expand All @@ -56,8 +49,6 @@ describe('CredentialsLogic', () => {
fullEngineAccessChecked: false,
};

const { mount } = new LogicMounter(CredentialsLogic);

const newToken = {
id: 1,
name: 'myToken',
Expand Down Expand Up @@ -955,7 +946,7 @@ describe('CredentialsLogic', () => {
describe('listener side-effects', () => {
it('should clear flashMessages whenever the credentials form flyout is opened', () => {
CredentialsLogic.actions.showCredentialsForm();
expect(FlashMessagesLogic.actions.clearFlashMessages).toHaveBeenCalled();
expect(clearFlashMessages).toHaveBeenCalled();
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ApiTokenTypes, CREATE_MESSAGE, UPDATE_MESSAGE, DELETE_MESSAGE } from '.

import { HttpLogic } from '../../../shared/http';
import {
FlashMessagesLogic,
clearFlashMessages,
setSuccessMessage,
flashAPIErrors,
} from '../../../shared/flash_messages';
Expand Down Expand Up @@ -227,7 +227,7 @@ export const CredentialsLogic = kea<CredentialsLogicType>({
}),
listeners: ({ actions, values }) => ({
showCredentialsForm: () => {
FlashMessagesLogic.actions.clearFlashMessages();
clearFlashMessages();
},
initializeCredentialsData: () => {
actions.fetchCredentials();
Expand Down
Loading