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 @@ -10,7 +10,13 @@ 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 { mockAllValues, mockAllActions, setMockValues, setMockActions } from './kea.mock';
export {
mockAllValues,
mockAllActions,
setMockValues,
setMockActions,
LogicMounter,
} from './kea.mock';

export { mountAsync } from './mount_async.mock';
export { mountWithIntl } from './mount_with_i18n.mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ jest.mock('kea', () => ({
}));

/**
* React component helpers
*
* Call this function to override a specific set of Kea values while retaining all other defaults
* Example usage within a component test:
*
* import '../../../__mocks__/kea';
* import { setMockValues } from ''../../../__mocks__';
* Example usage:
*
* import { setMockValues } from '../../../__mocks__/kea.mock';
* import { SomeComponent } from './';
*
* it('some test', () => {
* setMockValues({ someValue: 'hello' });
* shallow(<SomeComponent />);
* });
*/
import { useValues, useActions } from 'kea';
Expand All @@ -58,3 +62,62 @@ export const setMockValues = (values: object) => {
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 { SomeLogic } from './';
*
* const { mount, unmount } = new LogicMounter(SomeLogic);
*
* it('some test', () => {
* mount({ someValue: 'hello' });
* unmount();
* });
*/
import { resetContext, Logic, LogicInput } from 'kea';

interface LogicFile {
inputs: Array<LogicInput<Logic>>;
mount(): Function;
}
export class LogicMounter {
private logicFile: LogicFile;
private unmountFn!: Function;

constructor(logicFile: LogicFile) {
this.logicFile = logicFile;
}

// Reset context with optional default value overrides
public resetContext = (values?: object) => {
if (!values) {
resetContext({});
} else {
const path = this.logicFile.inputs[0].path as string[]; // example: ['x', 'y', 'z']
const defaults = path.reduceRight((value: object, key: string) => ({ [key]: value }), values); // example: { x: { y: { z: values } } }
resetContext({ defaults });
}
};

// Automatically reset context & mount the logic file
public mount = (values?: object) => {
this.resetContext(values);
const unmount = this.logicFile.mount();
this.unmountFn = unmount;
return unmount; // Keep Kea behavior of returning an unmount fn from mount
};

// Also add unmount as a class method that can be destructured on init without becoming stale later
public unmount = () => {
this.unmountFn();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../__mocks__/kea.mock';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
Expand Down Expand Up @@ -57,24 +57,7 @@ describe('CredentialsLogic', () => {
fullEngineAccessChecked: false,
};

const mount = (defaults?: object) => {
if (!defaults) {
resetContext({});
} else {
resetContext({
defaults: {
enterprise_search: {
app_search: {
credentials_logic: {
...defaults,
},
},
},
},
});
}
CredentialsLogic.mount();
};
const { mount } = new LogicMounter(CredentialsLogic);

const newToken = {
id: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../__mocks__/kea.mock';

import dedent from 'dedent';

jest.mock('./utils', () => ({
Expand Down Expand Up @@ -39,24 +40,7 @@ describe('DocumentCreationLogic', () => {
};
const mockFile = new File(['mockFile'], 'mockFile.json');

const mount = (defaults?: object) => {
if (!defaults) {
resetContext({});
} else {
resetContext({
defaults: {
enterprise_search: {
app_search: {
document_creation_logic: {
...defaults,
},
},
},
},
});
}
DocumentCreationLogic.mount();
};
const { mount } = new LogicMounter(DocumentCreationLogic);

beforeEach(() => {
jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../__mocks__/kea.mock';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
Expand Down Expand Up @@ -36,24 +36,7 @@ describe('DocumentDetailLogic', () => {
fields: [],
};

const mount = (defaults?: object) => {
if (!defaults) {
resetContext({});
} else {
resetContext({
defaults: {
enterprise_search: {
app_search: {
document_detail_logic: {
...defaults,
},
},
},
},
});
}
DocumentDetailLogic.mount();
};
const { mount } = new LogicMounter(DocumentDetailLogic);

beforeEach(() => {
jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../__mocks__/kea.mock';

import { DocumentsLogic } from './documents_logic';

Expand All @@ -13,24 +13,7 @@ describe('DocumentsLogic', () => {
isDocumentCreationOpen: false,
};

const mount = (defaults?: object) => {
if (!defaults) {
resetContext({});
} else {
resetContext({
defaults: {
enterprise_search: {
app_search: {
documents_logic: {
...defaults,
},
},
},
},
});
}
DocumentsLogic.mount();
};
const { mount } = new LogicMounter(DocumentsLogic);

describe('actions', () => {
describe('openDocumentCreation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../__mocks__/kea.mock';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
Expand Down Expand Up @@ -46,24 +46,7 @@ describe('EngineLogic', () => {
engineNotFound: false,
};

const mount = (values?: object) => {
if (!values) {
resetContext({});
} else {
resetContext({
defaults: {
enterprise_search: {
app_search: {
engine_logic: {
...values,
},
},
},
},
});
}
EngineLogic.mount();
};
const { mount } = new LogicMounter(EngineLogic);

beforeEach(() => {
jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../__mocks__/kea.mock';

import { mockHttpValues } from '../../../__mocks__';
jest.mock('../../../shared/http', () => ({
Expand Down Expand Up @@ -48,10 +48,7 @@ describe('EngineOverviewLogic', () => {
timeoutId: null,
};

const mount = () => {
resetContext({});
EngineOverviewLogic.mount();
};
const { mount, unmount } = new LogicMounter(EngineOverviewLogic);

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -141,12 +138,9 @@ describe('EngineOverviewLogic', () => {
});

describe('unmount', () => {
let unmount: Function;

beforeEach(() => {
jest.useFakeTimers();
resetContext({});
unmount = EngineOverviewLogic.mount();
mount();
});

it('clears existing polling timeouts on unmount', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';
import { LogicMounter } from '../../../../__mocks__/kea.mock';

import { mockHttpValues } from '../../../../__mocks__';
jest.mock('../../../../shared/http', () => ({
Expand Down Expand Up @@ -53,24 +53,7 @@ describe('LogRetentionLogic', () => {
isLogRetentionUpdating: false,
};

const mount = (defaults?: object) => {
if (!defaults) {
resetContext({});
} else {
resetContext({
defaults: {
enterprise_search: {
app_search: {
log_retention_logic: {
...defaults,
},
},
},
},
});
}
LogRetentionLogic.mount();
};
const { mount } = new LogicMounter(LogRetentionLogic);

beforeEach(() => {
jest.clearAllMocks();
Expand Down