Skip to content

Commit

Permalink
Move Kea & logic mocks/helpers into kea_logic subfolder
Browse files Browse the repository at this point in the history
- for organization

NOTE: It can't be a plain kea/ folder because then Jest automatically mocks the `kea` module itself kea 🤦
  • Loading branch information
cee-chen committed Jun 1, 2021
1 parent ce903b8 commit df7177b
Show file tree
Hide file tree
Showing 252 changed files with 418 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const mockFlashMessageHelpers = {
flashErrorToast: jest.fn(),
};

jest.mock('../shared/flash_messages', () => ({
...(jest.requireActual('../shared/flash_messages') as object),
jest.mock('../../shared/flash_messages', () => ({
...(jest.requireActual('../../shared/flash_messages') as object),
...mockFlashMessageHelpers,
FlashMessagesLogic: {
values: mockFlashMessagesValues,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

/**
* Combine all shared mock values/actions into a single obj
*
* NOTE: These variable names MUST start with 'mock*' in order for
* Jest to accept its use within a jest.mock()
*/
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
import { mockHttpValues } from './http_logic.mock';
import { mockKibanaValues } from './kibana_logic.mock';
import { mockLicensingValues } from './licensing_logic.mock';
import { mockTelemetryActions } from './telemetry_logic.mock';

export const mockAllValues = {
...mockKibanaValues,
...mockLicensingValues,
...mockHttpValues,
...mockFlashMessagesValues,
};
export const mockAllActions = {
...mockTelemetryActions,
...mockFlashMessagesActions,
};

/**
* Import this file directly to mock useValues with a set of default values for all shared logic files.
* Example usage:
*
* import '../../../__mocks__/kea_logic'; // Must come before kea's import, adjust relative path as needed
*/
jest.mock('kea', () => ({
...(jest.requireActual('kea') as object),
useValues: jest.fn(() => ({ ...mockAllValues })),
useActions: jest.fn(() => ({ ...mockAllActions })),
}));

/**
* React component helpers
*
* Call this function to override a specific set of Kea values while retaining all other defaults
*
* Example usage:
*
* import { setMockValues } from '../../../__mocks__/kea_logic';
* import { SomeComponent } from './';
*
* it('some test', () => {
* setMockValues({ someValue: 'hello' });
* shallow(<SomeComponent />);
* });
*/
import { useValues, useActions } from 'kea';

export const setMockValues = (values: object) => {
(useValues as jest.Mock).mockImplementation(() => ({ ...mockAllValues, ...values }));
};
export const setMockActions = (actions: object) => {
(useActions as jest.Mock).mockImplementation(() => ({ ...mockAllActions, ...actions }));
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export const mockHttpValues = {
readOnlyMode: false,
};

jest.mock('../shared/http', () => ({
jest.mock('../../shared/http', () => ({
HttpLogic: { values: mockHttpValues },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ export {
mockFlashMessagesActions,
mockFlashMessageHelpers,
} from './flash_messages_logic.mock';
export {
mockAllValues,
mockAllActions,
setMockValues,
setMockActions,
LogicMounter,
} from './kea.mock';
export { mockAllValues, mockAllActions, setMockValues, setMockActions } from './hooks.mock';

// Note: shallow_useeffect must be imported directly as a file
export { LogicMounter } from './logic_mounter';
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { chartPluginMock } from '../../../../../../src/plugins/charts/public/mocks';
import { chartPluginMock } from '../../../../../../../src/plugins/charts/public/mocks';

import { mockHistory } from './react_router/state.mock';
import { mockHistory } from '../react_router/state.mock';

export const mockKibanaValues = {
config: { host: 'http://localhost:3002' },
Expand All @@ -24,6 +24,6 @@ export const mockKibanaValues = {
renderHeaderActions: jest.fn(),
};

jest.mock('../shared/kibana', () => ({
jest.mock('../../shared/kibana', () => ({
KibanaLogic: { values: mockKibanaValues },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* 2.0.
*/

import { licensingMock } from '../../../../licensing/public/mocks';
import { licensingMock } from '../../../../../licensing/public/mocks';

export const mockLicensingValues = {
license: licensingMock.createLicense(),
hasPlatinumLicense: false,
hasGoldLicense: false,
};

jest.mock('../shared/licensing', () => ({
jest.mock('../../shared/licensing', () => ({
LicensingLogic: { values: mockLicensingValues },
}));
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,14 @@
*/

/**
* Combine all shared mock values/actions into a single obj
*
* NOTE: These variable names MUST start with 'mock*' in order for
* Jest to accept its use within a jest.mock()
*/
import { mockFlashMessagesValues, mockFlashMessagesActions } from './flash_messages_logic.mock';
import { mockHttpValues } from './http_logic.mock';
import { mockKibanaValues } from './kibana_logic.mock';
import { mockLicensingValues } from './licensing_logic.mock';
import { mockTelemetryActions } from './telemetry_logic.mock';

export const mockAllValues = {
...mockKibanaValues,
...mockLicensingValues,
...mockHttpValues,
...mockFlashMessagesValues,
};
export const mockAllActions = {
...mockTelemetryActions,
...mockFlashMessagesActions,
};

/**
* Import this file directly to mock useValues with a set of default values for all shared logic files.
* Example usage:
*
* import '../../../__mocks__/kea'; // Must come before kea's import, adjust relative path as needed
*/
jest.mock('kea', () => ({
...(jest.requireActual('kea') as object),
useValues: jest.fn(() => ({ ...mockAllValues })),
useActions: jest.fn(() => ({ ...mockAllActions })),
}));

/**
* React component helpers
*
* Call this function to override a specific set of Kea values while retaining all other defaults
*
* Example usage:
*
* import { setMockValues } from '../../../__mocks__/kea.mock';
* import { SomeComponent } from './';
*
* it('some test', () => {
* setMockValues({ someValue: 'hello' });
* shallow(<SomeComponent />);
* });
*/
import { useValues, useActions } from 'kea';

export const setMockValues = (values: object) => {
(useValues as jest.Mock).mockImplementation(() => ({ ...mockAllValues, ...values }));
};
export const setMockActions = (actions: object) => {
(useActions as jest.Mock).mockImplementation(() => ({ ...mockAllActions, ...actions }));
};

/**
* Kea logic helpers
*
* Call this function to mount a logic file and optionally override default values.
* Automatically DRYs out a lot of cruft for us, such as resetting context, creating the
* nested defaults path obj (see https://kea.js.org/docs/api/context#resetcontext), and
* returning an unmount function
*
* Example usage:
*
* import { LogicMounter } from '../../../__mocks__/kea.mock';
* import { LogicMounter } from '../../../__mocks__/kea_logic';
* import { SomeLogic } from './';
*
* const { mount, unmount } = new LogicMounter(SomeLogic);
Expand All @@ -86,7 +25,7 @@ export const setMockActions = (actions: object) => {
*/
import { resetContext, LogicWrapper } from 'kea';

type LogicFile = LogicWrapper<any>;
type LogicFile = LogicWrapper<unknown>;

export class LogicMounter {
private logicFile: LogicFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const mockTelemetryActions = {
sendWorkplaceSearchTelemetry: jest.fn(),
};

jest.mock('../shared/telemetry', () => ({
...(jest.requireActual('../shared/telemetry') as object),
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 @@ -6,7 +6,7 @@
*/

import { DEFAULT_INITIAL_APP_DATA } from '../../../common/__mocks__';
import { LogicMounter } from '../__mocks__';
import { LogicMounter } from '../__mocks__/kea_logic';

import { AppLogic } from './app_logic';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import '../../../__mocks__/shallow_useeffect.mock';
import { mockKibanaValues, setMockValues, setMockActions } from '../../../__mocks__';
import { mockKibanaValues, setMockValues, setMockActions } from '../../../__mocks__/kea_logic';
import { mockUseParams } from '../../../__mocks__/react_router';

import React from 'react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
mockKibanaValues,
mockHttpValues,
mockFlashMessageHelpers,
} from '../../../__mocks__';
} from '../../../__mocks__/kea_logic';

jest.mock('../engine', () => ({
EngineLogic: { values: { engineName: 'test-engine' } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { mockKibanaValues } from '../../../../__mocks__';
import { mockKibanaValues } from '../../../../__mocks__/kea_logic';

import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues, mockKibanaValues } from '../../../../__mocks__';
import { setMockValues, mockKibanaValues } from '../../../../__mocks__/kea_logic';

import React, { ReactElement } from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { mockKibanaValues } from '../../../../__mocks__';
import { mockKibanaValues } from '../../../../__mocks__/kea_logic';
import '../../../../__mocks__/react_router';
import '../../../__mocks__/engine_logic.mock';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import '../../../../../__mocks__/kea.mock';
import '../../../../../__mocks__/kea_logic';
import '../../../../../__mocks__/react_router';
import '../../../../__mocks__/engine_logic.mock';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import '../../../../../__mocks__/kea.mock';
import '../../../../../__mocks__/kea_logic';
import '../../../../../__mocks__/react_router';
import '../../../../__mocks__/engine_logic.mock';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import '../../../../../__mocks__/kea.mock';
import '../../../../../__mocks__/kea_logic';
import '../../../../../__mocks__/react_router';
import '../../../../__mocks__/engine_logic.mock';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
mockHttpValues,
mockKibanaValues,
mockFlashMessageHelpers,
} from '../../../../../../__mocks__';
} from '../../../../../../__mocks__/kea_logic';
import '../../../../../__mocks__/engine_logic.mock';

import { ReactWrapper } from 'enzyme';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';
import '../../../__mocks__/engine_logic.mock';

import React from 'react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';
import { mockUseParams } from '../../../../__mocks__/react_router';

import React from 'react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';

import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';

import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';

import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';

import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues } from '../../../../__mocks__';
import { setMockValues } from '../../../../__mocks__/kea_logic';

import React from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { setMockValues, setMockActions } from '../../../../__mocks__';
import { setMockValues, setMockActions } from '../../../../__mocks__/kea_logic';
import { mockApiLog } from '../__mocks__/api_log.mock';

import React from 'react';
Expand Down
Loading

0 comments on commit df7177b

Please sign in to comment.