Skip to content

Commit df7177b

Browse files
committed
Move Kea & logic mocks/helpers into kea_logic subfolder
- for organization NOTE: It can't be a plain kea/ folder because then Jest automatically mocks the `kea` module itself kea 🤦
1 parent ce903b8 commit df7177b

252 files changed

Lines changed: 418 additions & 336 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

x-pack/plugins/enterprise_search/public/applications/__mocks__/flash_messages_logic.mock.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/flash_messages_logic.mock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export const mockFlashMessageHelpers = {
2828
flashErrorToast: jest.fn(),
2929
};
3030

31-
jest.mock('../shared/flash_messages', () => ({
32-
...(jest.requireActual('../shared/flash_messages') as object),
31+
jest.mock('../../shared/flash_messages', () => ({
32+
...(jest.requireActual('../../shared/flash_messages') as object),
3333
...mockFlashMessageHelpers,
3434
FlashMessagesLogic: {
3535
values: mockFlashMessagesValues,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
/**
9+
* Combine all shared mock values/actions into a single obj
10+
*
11+
* NOTE: These variable names MUST start with 'mock*' in order for
12+
* Jest to accept its use within a jest.mock()
13+
*/
14+
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
15+
import { mockHttpValues } from './http_logic.mock';
16+
import { mockKibanaValues } from './kibana_logic.mock';
17+
import { mockLicensingValues } from './licensing_logic.mock';
18+
import { mockTelemetryActions } from './telemetry_logic.mock';
19+
20+
export const mockAllValues = {
21+
...mockKibanaValues,
22+
...mockLicensingValues,
23+
...mockHttpValues,
24+
...mockFlashMessagesValues,
25+
};
26+
export const mockAllActions = {
27+
...mockTelemetryActions,
28+
...mockFlashMessagesActions,
29+
};
30+
31+
/**
32+
* Import this file directly to mock useValues with a set of default values for all shared logic files.
33+
* Example usage:
34+
*
35+
* import '../../../__mocks__/kea_logic'; // Must come before kea's import, adjust relative path as needed
36+
*/
37+
jest.mock('kea', () => ({
38+
...(jest.requireActual('kea') as object),
39+
useValues: jest.fn(() => ({ ...mockAllValues })),
40+
useActions: jest.fn(() => ({ ...mockAllActions })),
41+
}));
42+
43+
/**
44+
* React component helpers
45+
*
46+
* Call this function to override a specific set of Kea values while retaining all other defaults
47+
*
48+
* Example usage:
49+
*
50+
* import { setMockValues } from '../../../__mocks__/kea_logic';
51+
* import { SomeComponent } from './';
52+
*
53+
* it('some test', () => {
54+
* setMockValues({ someValue: 'hello' });
55+
* shallow(<SomeComponent />);
56+
* });
57+
*/
58+
import { useValues, useActions } from 'kea';
59+
60+
export const setMockValues = (values: object) => {
61+
(useValues as jest.Mock).mockImplementation(() => ({ ...mockAllValues, ...values }));
62+
};
63+
export const setMockActions = (actions: object) => {
64+
(useActions as jest.Mock).mockImplementation(() => ({ ...mockAllActions, ...actions }));
65+
};

x-pack/plugins/enterprise_search/public/applications/__mocks__/http_logic.mock.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/http_logic.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ export const mockHttpValues = {
1313
readOnlyMode: false,
1414
};
1515

16-
jest.mock('../shared/http', () => ({
16+
jest.mock('../../shared/http', () => ({
1717
HttpLogic: { values: mockHttpValues },
1818
}));

x-pack/plugins/enterprise_search/public/applications/__mocks__/index.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ export {
1414
mockFlashMessagesActions,
1515
mockFlashMessageHelpers,
1616
} from './flash_messages_logic.mock';
17-
export {
18-
mockAllValues,
19-
mockAllActions,
20-
setMockValues,
21-
setMockActions,
22-
LogicMounter,
23-
} from './kea.mock';
17+
export { mockAllValues, mockAllActions, setMockValues, setMockActions } from './hooks.mock';
2418

25-
// Note: shallow_useeffect must be imported directly as a file
19+
export { LogicMounter } from './logic_mounter';

x-pack/plugins/enterprise_search/public/applications/__mocks__/kibana_logic.mock.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/kibana_logic.mock.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* 2.0.
66
*/
77

8-
import { chartPluginMock } from '../../../../../../src/plugins/charts/public/mocks';
8+
import { chartPluginMock } from '../../../../../../../src/plugins/charts/public/mocks';
99

10-
import { mockHistory } from './react_router/state.mock';
10+
import { mockHistory } from '../react_router/state.mock';
1111

1212
export const mockKibanaValues = {
1313
config: { host: 'http://localhost:3002' },
@@ -24,6 +24,6 @@ export const mockKibanaValues = {
2424
renderHeaderActions: jest.fn(),
2525
};
2626

27-
jest.mock('../shared/kibana', () => ({
27+
jest.mock('../../shared/kibana', () => ({
2828
KibanaLogic: { values: mockKibanaValues },
2929
}));

x-pack/plugins/enterprise_search/public/applications/__mocks__/licensing_logic.mock.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/licensing_logic.mock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
* 2.0.
66
*/
77

8-
import { licensingMock } from '../../../../licensing/public/mocks';
8+
import { licensingMock } from '../../../../../licensing/public/mocks';
99

1010
export const mockLicensingValues = {
1111
license: licensingMock.createLicense(),
1212
hasPlatinumLicense: false,
1313
hasGoldLicense: false,
1414
};
1515

16-
jest.mock('../shared/licensing', () => ({
16+
jest.mock('../../shared/licensing', () => ({
1717
LicensingLogic: { values: mockLicensingValues },
1818
}));

x-pack/plugins/enterprise_search/public/applications/__mocks__/kea.mock.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/logic_mounter.ts

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,14 @@
66
*/
77

88
/**
9-
* Combine all shared mock values/actions into a single obj
10-
*
11-
* NOTE: These variable names MUST start with 'mock*' in order for
12-
* Jest to accept its use within a jest.mock()
13-
*/
14-
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
15-
import { mockHttpValues } from './http_logic.mock';
16-
import { mockKibanaValues } from './kibana_logic.mock';
17-
import { mockLicensingValues } from './licensing_logic.mock';
18-
import { mockTelemetryActions } from './telemetry_logic.mock';
19-
20-
export const mockAllValues = {
21-
...mockKibanaValues,
22-
...mockLicensingValues,
23-
...mockHttpValues,
24-
...mockFlashMessagesValues,
25-
};
26-
export const mockAllActions = {
27-
...mockTelemetryActions,
28-
...mockFlashMessagesActions,
29-
};
30-
31-
/**
32-
* Import this file directly to mock useValues with a set of default values for all shared logic files.
33-
* Example usage:
34-
*
35-
* import '../../../__mocks__/kea'; // Must come before kea's import, adjust relative path as needed
36-
*/
37-
jest.mock('kea', () => ({
38-
...(jest.requireActual('kea') as object),
39-
useValues: jest.fn(() => ({ ...mockAllValues })),
40-
useActions: jest.fn(() => ({ ...mockAllActions })),
41-
}));
42-
43-
/**
44-
* React component helpers
45-
*
46-
* Call this function to override a specific set of Kea values while retaining all other defaults
47-
*
48-
* Example usage:
49-
*
50-
* import { setMockValues } from '../../../__mocks__/kea.mock';
51-
* import { SomeComponent } from './';
52-
*
53-
* it('some test', () => {
54-
* setMockValues({ someValue: 'hello' });
55-
* shallow(<SomeComponent />);
56-
* });
57-
*/
58-
import { useValues, useActions } from 'kea';
59-
60-
export const setMockValues = (values: object) => {
61-
(useValues as jest.Mock).mockImplementation(() => ({ ...mockAllValues, ...values }));
62-
};
63-
export const setMockActions = (actions: object) => {
64-
(useActions as jest.Mock).mockImplementation(() => ({ ...mockAllActions, ...actions }));
65-
};
66-
67-
/**
68-
* Kea logic helpers
69-
*
709
* Call this function to mount a logic file and optionally override default values.
7110
* Automatically DRYs out a lot of cruft for us, such as resetting context, creating the
7211
* nested defaults path obj (see https://kea.js.org/docs/api/context#resetcontext), and
7312
* returning an unmount function
7413
*
7514
* Example usage:
7615
*
77-
* import { LogicMounter } from '../../../__mocks__/kea.mock';
16+
* import { LogicMounter } from '../../../__mocks__/kea_logic';
7817
* import { SomeLogic } from './';
7918
*
8019
* const { mount, unmount } = new LogicMounter(SomeLogic);
@@ -86,7 +25,7 @@ export const setMockActions = (actions: object) => {
8625
*/
8726
import { resetContext, LogicWrapper } from 'kea';
8827

89-
type LogicFile = LogicWrapper<any>;
28+
type LogicFile = LogicWrapper<unknown>;
9029

9130
export class LogicMounter {
9231
private logicFile: LogicFile;

x-pack/plugins/enterprise_search/public/applications/__mocks__/telemetry_logic.mock.ts renamed to x-pack/plugins/enterprise_search/public/applications/__mocks__/kea_logic/telemetry_logic.mock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const mockTelemetryActions = {
1212
sendWorkplaceSearchTelemetry: jest.fn(),
1313
};
1414

15-
jest.mock('../shared/telemetry', () => ({
16-
...(jest.requireActual('../shared/telemetry') as object),
15+
jest.mock('../../shared/telemetry', () => ({
16+
...(jest.requireActual('../../shared/telemetry') as object),
1717
TelemetryLogic: { actions: mockTelemetryActions },
1818
}));

x-pack/plugins/enterprise_search/public/applications/app_search/app_logic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { DEFAULT_INITIAL_APP_DATA } from '../../../common/__mocks__';
9-
import { LogicMounter } from '../__mocks__';
9+
import { LogicMounter } from '../__mocks__/kea_logic';
1010

1111
import { AppLogic } from './app_logic';
1212

x-pack/plugins/enterprise_search/public/applications/app_search/components/analytics/analytics_layout.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import '../../../__mocks__/shallow_useeffect.mock';
9-
import { mockKibanaValues, setMockValues, setMockActions } from '../../../__mocks__';
9+
import { mockKibanaValues, setMockValues, setMockActions } from '../../../__mocks__/kea_logic';
1010
import { mockUseParams } from '../../../__mocks__/react_router';
1111

1212
import React from 'react';

0 commit comments

Comments
 (0)