-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathclone_panel_action.test.tsx
268 lines (249 loc) · 10.7 KB
/
clone_panel_action.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import {
ContactCardEmbeddable,
ContactCardEmbeddableFactory,
ContactCardEmbeddableInput,
ContactCardEmbeddableOutput,
CONTACT_CARD_EMBEDDABLE,
} from '@kbn/embeddable-plugin/public/lib/test_samples/embeddables';
import { CoreStart } from '@kbn/core/public';
import { coreMock } from '@kbn/core/public/mocks';
import { embeddablePluginMock } from '@kbn/embeddable-plugin/public/mocks';
import { ErrorEmbeddable, IContainer, isErrorEmbeddable } from '@kbn/embeddable-plugin/public';
import { DashboardPanelState } from '../../common';
import { ClonePanelAction } from './clone_panel_action';
import { pluginServices } from '../services/plugin_services';
import { buildMockDashboard, getSampleDashboardPanel } from '../mocks';
import { DashboardContainer } from '../dashboard_container/embeddable/dashboard_container';
let container: DashboardContainer;
let byRefOrValEmbeddable: ContactCardEmbeddable;
let genericEmbeddable: ContactCardEmbeddable;
let coreStart: CoreStart;
beforeEach(async () => {
coreStart = coreMock.createStart();
coreStart.savedObjects.client = {
...coreStart.savedObjects.client,
get: jest.fn().mockImplementation(() => ({ attributes: { title: 'Holy moly' } })),
find: jest.fn().mockImplementation(() => ({ total: 15 })),
create: jest.fn().mockImplementation(() => ({ id: 'brandNewSavedObject' })),
};
const mockEmbeddableFactory = new ContactCardEmbeddableFactory((() => null) as any, {} as any);
pluginServices.getServices().embeddable.getEmbeddableFactory = jest
.fn()
.mockReturnValue(mockEmbeddableFactory);
container = buildMockDashboard({
panels: {
'123': getSampleDashboardPanel<ContactCardEmbeddableInput>({
explicitInput: { firstName: 'Kibanana', id: '123' },
type: CONTACT_CARD_EMBEDDABLE,
}),
},
});
const refOrValContactCardEmbeddable = await container.addNewEmbeddable<
ContactCardEmbeddableInput,
ContactCardEmbeddableOutput,
ContactCardEmbeddable
>(CONTACT_CARD_EMBEDDABLE, {
firstName: 'RefOrValEmbeddable',
});
const genericContactCardEmbeddable = await container.addNewEmbeddable<
ContactCardEmbeddableInput,
ContactCardEmbeddableOutput,
ContactCardEmbeddable
>(CONTACT_CARD_EMBEDDABLE, {
firstName: 'NotRefOrValEmbeddable',
});
if (
isErrorEmbeddable(refOrValContactCardEmbeddable) ||
isErrorEmbeddable(genericContactCardEmbeddable)
) {
throw new Error('Failed to create embeddables');
} else {
byRefOrValEmbeddable = embeddablePluginMock.mockRefOrValEmbeddable<
ContactCardEmbeddable,
ContactCardEmbeddableInput
>(refOrValContactCardEmbeddable, {
mockedByReferenceInput: {
savedObjectId: 'testSavedObjectId',
id: refOrValContactCardEmbeddable.id,
},
mockedByValueInput: { firstName: 'Kibanana', id: refOrValContactCardEmbeddable.id },
});
genericEmbeddable = genericContactCardEmbeddable;
}
});
test('Clone is incompatible with Error Embeddables', async () => {
const action = new ClonePanelAction(coreStart.savedObjects);
const errorEmbeddable = new ErrorEmbeddable('Wow what an awful error', { id: ' 404' }, container);
expect(await action.isCompatible({ embeddable: errorEmbeddable })).toBe(false);
});
test('Clone adds a new embeddable', async () => {
const dashboard = byRefOrValEmbeddable.getRoot() as IContainer;
const originalPanelCount = Object.keys(dashboard.getInput().panels).length;
const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels));
const action = new ClonePanelAction(coreStart.savedObjects);
await action.execute({ embeddable: byRefOrValEmbeddable });
expect(Object.keys(container.getInput().panels).length).toEqual(originalPanelCount + 1);
const newPanelId = Object.keys(container.getInput().panels).find(
(key) => !originalPanelKeySet.has(key)
);
expect(newPanelId).toBeDefined();
const newPanel = container.getInput().panels[newPanelId!];
expect(newPanel.type).toEqual('placeholder');
// let the placeholder load
await dashboard.untilEmbeddableLoaded(newPanelId!);
await new Promise((r) => process.nextTick(r)); // Allow the current loop of the event loop to run to completion
// now wait for the full embeddable to replace it
const loadedPanel = await dashboard.untilEmbeddableLoaded(newPanelId!);
expect(loadedPanel.type).toEqual(byRefOrValEmbeddable.type);
});
test('Clones a RefOrVal embeddable by value', async () => {
const dashboard = byRefOrValEmbeddable.getRoot() as IContainer;
const panel = dashboard.getInput().panels[byRefOrValEmbeddable.id] as DashboardPanelState;
const action = new ClonePanelAction(coreStart.savedObjects);
// @ts-ignore
const newPanel = await action.cloneEmbeddable(panel, byRefOrValEmbeddable);
expect(coreStart.savedObjects.client.get).toHaveBeenCalledTimes(0);
expect(coreStart.savedObjects.client.find).toHaveBeenCalledTimes(0);
expect(coreStart.savedObjects.client.create).toHaveBeenCalledTimes(0);
expect(newPanel.type).toEqual(byRefOrValEmbeddable.type);
});
test('Clones a non-RefOrVal embeddable by value if the panel does not have a savedObjectId', async () => {
const dashboard = genericEmbeddable.getRoot() as IContainer;
const panel = dashboard.getInput().panels[genericEmbeddable.id] as DashboardPanelState;
const action = new ClonePanelAction(coreStart.savedObjects);
// @ts-ignore
const newPanelWithoutId = await action.cloneEmbeddable(panel, genericEmbeddable);
expect(coreStart.savedObjects.client.get).toHaveBeenCalledTimes(0);
expect(coreStart.savedObjects.client.find).toHaveBeenCalledTimes(0);
expect(coreStart.savedObjects.client.create).toHaveBeenCalledTimes(0);
expect(newPanelWithoutId.type).toEqual(genericEmbeddable.type);
});
test('Clones a non-RefOrVal embeddable by reference if the panel has a savedObjectId', async () => {
const dashboard = genericEmbeddable.getRoot() as IContainer;
const panel = dashboard.getInput().panels[genericEmbeddable.id] as DashboardPanelState;
panel.explicitInput.savedObjectId = 'holySavedObjectBatman';
const action = new ClonePanelAction(coreStart.savedObjects);
// @ts-ignore
const newPanel = await action.cloneEmbeddable(panel, genericEmbeddable);
expect(coreStart.savedObjects.client.get).toHaveBeenCalledTimes(1);
expect(coreStart.savedObjects.client.find).toHaveBeenCalledTimes(1);
expect(coreStart.savedObjects.client.create).toHaveBeenCalledTimes(1);
expect(newPanel.type).toEqual(genericEmbeddable.type);
});
test('Gets a unique title from the saved objects library', async () => {
const dashboard = genericEmbeddable.getRoot() as IContainer;
const panel = dashboard.getInput().panels[genericEmbeddable.id] as DashboardPanelState;
panel.explicitInput.savedObjectId = 'holySavedObjectBatman';
coreStart.savedObjects.client.find = jest.fn().mockImplementation(({ search }) => {
if (search === '"testFirstClone"') {
return {
savedObjects: [
{
attributes: { title: 'testFirstClone' },
get: jest.fn().mockReturnValue('testFirstClone'),
},
],
total: 1,
};
} else if (search === '"testBeforePageLimit"') {
return {
savedObjects: [
{
attributes: { title: 'testBeforePageLimit (copy 9)' },
get: jest.fn().mockReturnValue('testBeforePageLimit (copy 9)'),
},
],
total: 10,
};
} else if (search === '"testMaxLogic"') {
return {
savedObjects: [
{
attributes: { title: 'testMaxLogic (copy 10000)' },
get: jest.fn().mockReturnValue('testMaxLogic (copy 10000)'),
},
],
total: 2,
};
} else if (search === '"testAfterPageLimit"') {
return { total: 11 };
}
});
const action = new ClonePanelAction(coreStart.savedObjects);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testFirstClone')).toEqual(
'testFirstClone (copy)'
);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testBeforePageLimit')).toEqual(
'testBeforePageLimit (copy 10)'
);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testBeforePageLimit (copy 9)')).toEqual(
'testBeforePageLimit (copy 10)'
);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testMaxLogic')).toEqual(
'testMaxLogic (copy 10001)'
);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testAfterPageLimit')).toEqual(
'testAfterPageLimit (copy 11)'
);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testAfterPageLimit (copy 10)')).toEqual(
'testAfterPageLimit (copy 11)'
);
// @ts-ignore
expect(await action.getCloneTitle(genericEmbeddable, 'testAfterPageLimit (copy 10000)')).toEqual(
'testAfterPageLimit (copy 11)'
);
});
test('Gets a unique title from the dashboard', async () => {
const dashboard = genericEmbeddable.getRoot() as DashboardContainer;
const action = new ClonePanelAction(coreStart.savedObjects);
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, '')).toEqual('');
dashboard.getPanelTitles = jest.fn().mockImplementation(() => {
return ['testDuplicateTitle', 'testDuplicateTitle (copy)', 'testUniqueTitle'];
});
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, 'testUniqueTitle')).toEqual(
'testUniqueTitle (copy)'
);
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, 'testDuplicateTitle')).toEqual(
'testDuplicateTitle (copy 1)'
);
dashboard.getPanelTitles = jest.fn().mockImplementation(() => {
return ['testDuplicateTitle', 'testDuplicateTitle (copy)'].concat(
Array.from([...Array(39)], (_, index) => `testDuplicateTitle (copy ${index + 1})`)
);
});
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, 'testDuplicateTitle')).toEqual(
'testDuplicateTitle (copy 40)'
);
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, 'testDuplicateTitle (copy 100)')).toEqual(
'testDuplicateTitle (copy 40)'
);
dashboard.getPanelTitles = jest.fn().mockImplementation(() => {
return ['testDuplicateTitle (copy 100)'];
});
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, 'testDuplicateTitle')).toEqual(
'testDuplicateTitle (copy 101)'
);
// @ts-ignore
expect(await action.getCloneTitle(byRefOrValEmbeddable, 'testDuplicateTitle (copy 100)')).toEqual(
'testDuplicateTitle (copy 101)'
);
});