Skip to content

Commit 474af9f

Browse files
Constancekibanamachine
andauthored
[Enterprise Search] Add Kea test helper for directly accessing listeners (#89061)
* Add getListeners to Kea test helpers * Update TelemetryLogic to use new getListeners helper Co-authored-by: Kibana Machine <[email protected]>
1 parent e5588a1 commit 474af9f

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

x-pack/plugins/enterprise_search/public/applications/__mocks__/kea.mock.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,29 @@ export class LogicMounter {
120120
public unmount = () => {
121121
this.unmountFn();
122122
};
123+
124+
/**
125+
* Some tests (e.g. async tests, tests that expect thrown errors) need to access
126+
* listener functions directly instead of calling `SomeLogic.actions.someListener`,
127+
* due to how Kea invokes/wraps action fns by design.
128+
*
129+
* Example usage:
130+
*
131+
* const { mount, getListeners } = new LogicMounter(SomeLogic);
132+
*
133+
* it('some test', async () => {
134+
* mount();
135+
* const { someListener } = getListeners({ values: { someMockValue: false } });
136+
*
137+
* const mockBreakpoint = jest.fn();
138+
* await someListener({ someMockArgument: true }, mockBreakpoint);
139+
* });
140+
*/
141+
public getListeners = (listenersArgs: object = {}) => {
142+
const { listeners } = this.logicFile.inputs[0];
143+
144+
return typeof listeners === 'function'
145+
? (listeners as Function)(listenersArgs) // e.g., listeners({ values, actions, props }) => ({ ... })
146+
: listeners; // handles simpler logic files that just define listeners: { ... }
147+
};
123148
}

x-pack/plugins/enterprise_search/public/applications/shared/telemetry/telemetry_logic.test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { resetContext } from 'kea';
8-
97
import { JSON_HEADER as headers } from '../../../../common/constants';
10-
import { mockHttpValues } from '../../__mocks__/http_logic.mock';
8+
import { LogicMounter, mockHttpValues } from '../../__mocks__';
119

12-
import { TelemetryLogic } from './';
10+
import { TelemetryLogic } from './telemetry_logic';
1311

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

1716
beforeEach(() => {
1817
jest.clearAllMocks();
19-
resetContext({});
20-
TelemetryLogic.mount();
18+
mount();
2119
});
2220

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

3735
it('throws an error if the telemetry endpoint fails', async () => {
3836
http.put.mockImplementationOnce(() => Promise.reject());
39-
40-
// To capture thrown errors, we have to call the listener fn directly
41-
// instead of using `TelemetryLogic.actions.sendTelemetry` - this is
42-
// due to how Kea invokes/wraps action fns by design.
43-
const { sendTelemetry } = (TelemetryLogic.inputs[0] as any).listeners({ actions: {} });
37+
const { sendTelemetry } = getListeners();
4438

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

0 commit comments

Comments
 (0)