Skip to content

Commit 281718c

Browse files
[embeddable] remove EmbeddableRenderer from embeddable factory tests (#233923)
Part of effort to merge `presentationPanel` plugin into `embeddable` plugin Embeddable factory tests should not use `EmbeddableRenderer` and as a by product, need to call `setStubKibanaServices`. Instead, these tests should just call `buildEmbeddable` directly to better test the embeddable factory without other dependencies. --------- Co-authored-by: kibanamachine <[email protected]>
1 parent 82ab129 commit 281718c

File tree

4 files changed

+113
-143
lines changed

4 files changed

+113
-143
lines changed

src/platform/plugins/private/links/public/embeddable/links_embeddable.test.tsx

Lines changed: 86 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
*/
99

1010
import React from 'react';
11-
import { render, screen, waitFor } from '@testing-library/react';
12-
import { embeddablePluginMock } from '@kbn/embeddable-plugin/public/mocks';
13-
import { setStubKibanaServices } from '@kbn/presentation-panel-plugin/public/mocks';
11+
import { render, screen } from '@testing-library/react';
1412
import { EuiThemeProvider } from '@elastic/eui';
1513
import { getLinksEmbeddableFactory } from './links_embeddable';
1614
import type { LinksEmbeddableState } from '../../common';
1715
import { LINKS_EMBEDDABLE_TYPE } from '../../common';
1816
import type { Link } from '../../server';
19-
import { EmbeddableRenderer } from '@kbn/embeddable-plugin/public';
20-
import type { LinksApi, LinksParentApi, ResolvedLink } from '../types';
17+
import type { LinksApi, ResolvedLink } from '../types';
2118
import { linksClient } from '../content_management';
2219
import { getMockLinksParentApi } from '../mocks';
2320

@@ -118,144 +115,128 @@ jest.mock('../content_management/load_from_library', () => {
118115
};
119116
});
120117

121-
const renderEmbeddable = (
122-
parent: LinksParentApi,
123-
overrides?: {
124-
onApiAvailable: (api: LinksApi) => void;
125-
}
126-
) => {
127-
return render(
128-
<EuiThemeProvider>
129-
<EmbeddableRenderer<LinksEmbeddableState, LinksApi>
130-
type={LINKS_EMBEDDABLE_TYPE}
131-
onApiAvailable={jest.fn()}
132-
getParentApi={jest.fn().mockReturnValue(parent)}
133-
{...overrides}
134-
/>
135-
</EuiThemeProvider>
136-
);
137-
};
138-
139-
describe('getLinksEmbeddableFactory', () => {
118+
async function buildLinksEmbeddable(state: LinksEmbeddableState) {
140119
const factory = getLinksEmbeddableFactory();
141-
142-
beforeAll(() => {
143-
const embeddable = embeddablePluginMock.createSetupContract();
144-
embeddable.registerReactEmbeddableFactory(LINKS_EMBEDDABLE_TYPE, async () => {
145-
return factory;
146-
});
147-
setStubKibanaServices();
120+
const parentApi = getMockLinksParentApi(state);
121+
const uuid = '1234';
122+
return await factory.buildEmbeddable({
123+
initialState: {
124+
rawState: state,
125+
},
126+
finalizeApi: (api) => {
127+
return {
128+
...api,
129+
uuid,
130+
parentApi,
131+
type: LINKS_EMBEDDABLE_TYPE,
132+
} as LinksApi;
133+
},
134+
parentApi,
135+
uuid,
148136
});
137+
}
149138

139+
describe('getLinksEmbeddableFactory', () => {
150140
describe('by reference embeddable', () => {
151-
const parent = getMockLinksParentApi({
141+
const byRefState: LinksEmbeddableState = {
152142
title: 'my links',
153143
description: 'just a few links',
154144
hidePanelTitles: false,
155145
savedObjectId: '123',
156-
});
146+
};
157147

158148
test('component renders', async () => {
159-
renderEmbeddable(parent);
149+
const { Component } = await buildLinksEmbeddable(byRefState);
150+
render(
151+
<EuiThemeProvider>
152+
<Component />
153+
</EuiThemeProvider>
154+
);
160155
expect(await screen.findByTestId('links--component')).toBeInTheDocument();
161156
});
162157

163158
test('api methods', async () => {
164-
const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>;
165-
renderEmbeddable(parent, { onApiAvailable });
166-
167-
await waitFor(async () => {
168-
const api = onApiAvailable.mock.calls[0][0];
169-
expect(api.serializeState()).toEqual({
170-
rawState: {
171-
title: 'my links',
172-
description: 'just a few links',
173-
hidePanelTitles: false,
174-
savedObjectId: '123',
175-
},
176-
});
177-
expect(await api.canUnlinkFromLibrary()).toBe(true);
178-
expect(api.defaultTitle$?.value).toBe('links 001');
179-
expect(api.defaultDescription$?.value).toBe('some links');
159+
const { api } = await buildLinksEmbeddable(byRefState);
160+
expect(api.serializeState()).toEqual({
161+
rawState: {
162+
title: 'my links',
163+
description: 'just a few links',
164+
hidePanelTitles: false,
165+
savedObjectId: '123',
166+
},
180167
});
168+
expect(await api.canUnlinkFromLibrary()).toBe(true);
169+
expect(api.defaultTitle$?.value).toBe('links 001');
170+
expect(api.defaultDescription$?.value).toBe('some links');
181171
});
182172

183173
test('unlink from library', async () => {
184-
const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>;
185-
renderEmbeddable(parent, { onApiAvailable });
186-
187-
await waitFor(async () => {
188-
const api = onApiAvailable.mock.calls[0][0];
189-
expect(api.getSerializedStateByValue()).toEqual({
190-
rawState: {
191-
title: 'my links',
192-
description: 'just a few links',
193-
hidePanelTitles: false,
194-
links: getLinks(),
195-
layout: 'vertical',
196-
},
197-
});
174+
const { api } = await buildLinksEmbeddable(byRefState);
175+
expect(api.getSerializedStateByValue()).toEqual({
176+
rawState: {
177+
title: 'my links',
178+
description: 'just a few links',
179+
hidePanelTitles: false,
180+
links: getLinks(),
181+
layout: 'vertical',
182+
},
198183
});
199184
});
200185
});
201186

202187
describe('by value embeddable', () => {
203-
const parent = getMockLinksParentApi({
188+
const byValueState: LinksEmbeddableState = {
204189
description: 'just a few links',
205190
title: 'my links',
206191
hidePanelTitles: true,
207192
links: getLinks(),
208193
layout: 'horizontal',
209-
});
194+
};
210195

211196
test('component renders', async () => {
212-
renderEmbeddable(parent);
197+
const { Component } = await buildLinksEmbeddable(byValueState);
198+
render(
199+
<EuiThemeProvider>
200+
<Component />
201+
</EuiThemeProvider>
202+
);
213203

214204
expect(await screen.findByTestId('links--component')).toBeInTheDocument();
215205
});
216206

217207
test('api methods', async () => {
218-
const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>;
219-
renderEmbeddable(parent, { onApiAvailable });
220-
221-
await waitFor(async () => {
222-
const api = onApiAvailable.mock.calls[0][0];
223-
expect(api.serializeState()).toEqual({
224-
rawState: {
225-
title: 'my links',
226-
description: 'just a few links',
227-
hidePanelTitles: true,
228-
links: getLinks(),
229-
layout: 'horizontal',
230-
},
231-
});
232-
233-
expect(await api.canLinkToLibrary()).toBe(true);
208+
const { api } = await buildLinksEmbeddable(byValueState);
209+
expect(api.serializeState()).toEqual({
210+
rawState: {
211+
title: 'my links',
212+
description: 'just a few links',
213+
hidePanelTitles: true,
214+
links: getLinks(),
215+
layout: 'horizontal',
216+
},
234217
});
218+
219+
expect(await api.canLinkToLibrary()).toBe(true);
235220
});
236-
test('save to library', async () => {
237-
const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>;
238-
renderEmbeddable(parent, { onApiAvailable });
239221

240-
await waitFor(async () => {
241-
const api = onApiAvailable.mock.calls[0][0];
242-
const newId = await api.saveToLibrary('some new title');
243-
expect(linksClient.create).toHaveBeenCalledWith({
244-
data: {
245-
title: 'some new title',
246-
links: getLinks(),
247-
layout: 'horizontal',
248-
},
249-
});
250-
expect(newId).toBe('333');
251-
expect(api.getSerializedStateByReference(newId)).toEqual({
252-
rawState: {
253-
title: 'my links',
254-
description: 'just a few links',
255-
hidePanelTitles: true,
256-
savedObjectId: '333',
257-
},
258-
});
222+
test('save to library', async () => {
223+
const { api } = await buildLinksEmbeddable(byValueState);
224+
const newId = await api.saveToLibrary('some new title');
225+
expect(linksClient.create).toHaveBeenCalledWith({
226+
data: {
227+
title: 'some new title',
228+
links: getLinks(),
229+
layout: 'horizontal',
230+
},
231+
});
232+
expect(newId).toBe('333');
233+
expect(api.getSerializedStateByReference(newId)).toEqual({
234+
rawState: {
235+
title: 'my links',
236+
description: 'just a few links',
237+
hidePanelTitles: true,
238+
savedObjectId: '333',
239+
},
259240
});
260241
});
261242
});

src/platform/plugins/private/links/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@kbn/presentation-containers",
3737
"@kbn/presentation-publishing",
3838
"@kbn/react-kibana-context-render",
39-
"@kbn/presentation-panel-plugin",
4039
"@kbn/share-plugin",
4140
"@kbn/es-query",
4241
"@kbn/presentation-util"

x-pack/platform/plugins/shared/ml/public/embeddables/anomaly_swimlane/anomaly_swimlane_embeddable_factory.test.tsx

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
import { coreMock } from '@kbn/core/public/mocks';
99
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
1010
import { chartPluginMock } from '@kbn/charts-plugin/public/mocks';
11-
import { EmbeddableRenderer } from '@kbn/embeddable-plugin/public';
12-
import { setStubKibanaServices } from '@kbn/presentation-panel-plugin/public/mocks';
1311
import { render, waitFor, screen } from '@testing-library/react';
1412
import React from 'react';
1513
import { of } from 'rxjs';
1614
import { ANOMALY_SWIMLANE_EMBEDDABLE_TYPE } from '../constants';
1715
import { getAnomalySwimLaneEmbeddableFactory } from './anomaly_swimlane_embeddable_factory';
1816
import type { AnomalySwimLaneEmbeddableApi, AnomalySwimLaneEmbeddableState } from './types';
19-
import { embeddablePluginMock } from '@kbn/embeddable-plugin/public/mocks';
2017

2118
// Mock dependencies
2219
const pluginStartDeps = {
@@ -81,44 +78,38 @@ jest.mock('../../application/services/anomaly_timeline_service', () => {
8178
describe('getAnomalySwimLaneEmbeddableFactory', () => {
8279
const factory = getAnomalySwimLaneEmbeddableFactory(getStartServices);
8380

84-
beforeAll(() => {
85-
const embeddable = embeddablePluginMock.createSetupContract();
86-
embeddable.registerReactEmbeddableFactory(ANOMALY_SWIMLANE_EMBEDDABLE_TYPE, async () => {
87-
return factory;
88-
});
89-
setStubKibanaServices();
90-
});
91-
9281
it('should init embeddable api based on provided state', async () => {
93-
const rawState = { jobIds: ['my-job'], viewBy: 'overall' } as AnomalySwimLaneEmbeddableState;
94-
95-
const onApiAvailable = jest.fn() as jest.MockedFunction<
96-
(api: AnomalySwimLaneEmbeddableApi) => void
97-
>;
82+
const uuid = '1234';
83+
const parentApi = {
84+
executionContext: {
85+
type: 'dashboard',
86+
id: 'dashboard-id',
87+
},
88+
};
89+
const { api, Component } = await factory.buildEmbeddable({
90+
initialState: {
91+
rawState: { jobIds: ['my-job'], viewBy: 'overall' } as AnomalySwimLaneEmbeddableState,
92+
},
93+
finalizeApi: (preFinalizeApi) => {
94+
return {
95+
...preFinalizeApi,
96+
uuid,
97+
parentApi,
98+
type: ANOMALY_SWIMLANE_EMBEDDABLE_TYPE,
99+
} as AnomalySwimLaneEmbeddableApi;
100+
},
101+
parentApi,
102+
uuid,
103+
});
98104

99-
render(
100-
<EmbeddableRenderer<AnomalySwimLaneEmbeddableState, AnomalySwimLaneEmbeddableApi>
101-
maybeId={'maybe_id'}
102-
type={ANOMALY_SWIMLANE_EMBEDDABLE_TYPE}
103-
onApiAvailable={onApiAvailable}
104-
getParentApi={() => ({
105-
getSerializedStateForChild: () => ({ rawState }),
106-
executionContext: {
107-
type: 'dashboard',
108-
id: 'dashboard-id',
109-
},
110-
})}
111-
/>
112-
);
105+
render(<Component />);
113106

114107
await waitFor(() => {
115-
const resultApi = onApiAvailable.mock.calls[0][0];
116-
117-
expect(resultApi.dataLoading$?.value).toEqual(false);
118-
expect(resultApi.jobIds.value).toEqual(['my-job']);
119-
expect(resultApi.viewBy.value).toEqual('overall');
108+
expect(api.dataLoading$?.value).toEqual(false);
109+
expect(api.jobIds.value).toEqual(['my-job']);
110+
expect(api.viewBy.value).toEqual('overall');
120111

121-
expect(screen.getByTestId<HTMLElement>('mlSwimLaneEmbeddable_maybe_id')).toBeInTheDocument();
112+
expect(screen.getByTestId<HTMLElement>('mlSwimLaneEmbeddable_1234')).toBeInTheDocument();
122113
});
123114
});
124115
});

x-pack/platform/plugins/shared/ml/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
"@kbn/monaco",
101101
"@kbn/observability-ai-assistant-plugin",
102102
"@kbn/presentation-containers",
103-
"@kbn/presentation-panel-plugin",
104103
"@kbn/presentation-publishing",
105104
"@kbn/presentation-util-plugin",
106105
"@kbn/react-kibana-context-render",

0 commit comments

Comments
 (0)