Skip to content

Commit

Permalink
[Enterprise Search] Add Kea test helper for directly accessing listen…
Browse files Browse the repository at this point in the history
…ers (#89061)

* Add getListeners to Kea test helpers

* Update TelemetryLogic to use new getListeners helper

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Constance and kibanamachine authored Jan 25, 2021
1 parent e5588a1 commit 474af9f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,29 @@ export class LogicMounter {
public unmount = () => {
this.unmountFn();
};

/**
* Some tests (e.g. async tests, tests that expect thrown errors) need to access
* listener functions directly instead of calling `SomeLogic.actions.someListener`,
* due to how Kea invokes/wraps action fns by design.
*
* Example usage:
*
* const { mount, getListeners } = new LogicMounter(SomeLogic);
*
* it('some test', async () => {
* mount();
* const { someListener } = getListeners({ values: { someMockValue: false } });
*
* const mockBreakpoint = jest.fn();
* await someListener({ someMockArgument: true }, mockBreakpoint);
* });
*/
public getListeners = (listenersArgs: object = {}) => {
const { listeners } = this.logicFile.inputs[0];

return typeof listeners === 'function'
? (listeners as Function)(listenersArgs) // e.g., listeners({ values, actions, props }) => ({ ... })
: listeners; // handles simpler logic files that just define listeners: { ... }
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';

import { JSON_HEADER as headers } from '../../../../common/constants';
import { mockHttpValues } from '../../__mocks__/http_logic.mock';
import { LogicMounter, mockHttpValues } from '../../__mocks__';

import { TelemetryLogic } from './';
import { TelemetryLogic } from './telemetry_logic';

describe('Telemetry logic', () => {
const { mount, getListeners } = new LogicMounter(TelemetryLogic);
const { http } = mockHttpValues;

beforeEach(() => {
jest.clearAllMocks();
resetContext({});
TelemetryLogic.mount();
mount();
});

describe('sendTelemetry', () => {
Expand All @@ -36,11 +34,7 @@ describe('Telemetry logic', () => {

it('throws an error if the telemetry endpoint fails', async () => {
http.put.mockImplementationOnce(() => Promise.reject());

// To capture thrown errors, we have to call the listener fn directly
// instead of using `TelemetryLogic.actions.sendTelemetry` - this is
// due to how Kea invokes/wraps action fns by design.
const { sendTelemetry } = (TelemetryLogic.inputs[0] as any).listeners({ actions: {} });
const { sendTelemetry } = getListeners();

await expect(sendTelemetry({ action: '', metric: '', product: '' })).rejects.toThrow(
'Unable to send telemetry'
Expand Down

0 comments on commit 474af9f

Please sign in to comment.