Skip to content

Commit 82401dc

Browse files
committed
adding tests
1 parent aa648ec commit 82401dc

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { getAllMigrations } from './get_all_migrations';
10+
11+
describe('embeddable getAllMigratons', () => {
12+
const factories = [{ migrations: { '7.11.0': (state: any) => state } }];
13+
const enhacements = [{ migrations: { '7.12.0': (state: any) => state } }];
14+
const migrateFn = jest.fn();
15+
16+
test('returns base migrations', () => {
17+
expect(getAllMigrations([], [], migrateFn)).toEqual({});
18+
});
19+
20+
test('returns embeddable factory migrations', () => {
21+
expect(getAllMigrations(factories as any, [], migrateFn)).toHaveProperty(['7.11.0']);
22+
});
23+
24+
test('returns enhancement migrations', () => {
25+
const migrations = getAllMigrations([], enhacements as any, migrateFn);
26+
expect(migrations).toHaveProperty(['7.12.0']);
27+
});
28+
29+
test('returns all migrations', () => {
30+
const migrations = getAllMigrations(factories as any, enhacements as any, migrateFn);
31+
expect(migrations).toHaveProperty(['7.11.0']);
32+
expect(migrations).toHaveProperty(['7.12.0']);
33+
});
34+
});

src/plugins/embeddable/common/lib/get_all_migrations.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,30 @@
77
*/
88

99
import { baseEmbeddableMigrations } from './migrate_base_input';
10-
import { MigrateFunctionsObject } from '../../../kibana_utils/common/persistable_state';
10+
import {
11+
MigrateFunctionsObject,
12+
PersistableState,
13+
PersistableStateMigrateFn,
14+
} from '../../../kibana_utils/common/persistable_state';
1115

12-
export const getAllMigrations = (factories: any, enhancements: any, migrateFn: any) => {
16+
export const getAllMigrations = (
17+
factories: unknown[],
18+
enhancements: unknown[],
19+
migrateFn: PersistableStateMigrateFn
20+
) => {
1321
const uniqueVersions = new Set<string>();
1422
for (const baseMigrationVersion of Object.keys(baseEmbeddableMigrations)) {
1523
uniqueVersions.add(baseMigrationVersion);
1624
}
1725
for (const factory of factories) {
18-
Object.keys(factory.migrations).forEach((version) => uniqueVersions.add(version));
26+
Object.keys((factory as PersistableState).migrations).forEach((version) =>
27+
uniqueVersions.add(version)
28+
);
1929
}
2030
for (const enhancement of enhancements) {
21-
Object.keys(enhancement.migrations).forEach((version) => uniqueVersions.add(version));
31+
Object.keys((enhancement as PersistableState).migrations).forEach((version) =>
32+
uniqueVersions.add(version)
33+
);
2234
}
2335

2436
const migrations: MigrateFunctionsObject = {};

src/plugins/embeddable/public/plugin.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,34 @@ describe('embeddable factory', () => {
106106
my: 'state',
107107
} as any;
108108

109+
const containerEmbeddableFactoryId = 'CONTAINER';
110+
const containerEmbeddableFactory = {
111+
type: containerEmbeddableFactoryId,
112+
create: jest.fn(),
113+
getDisplayName: () => 'Container',
114+
isContainer: true,
115+
isEditable: () => Promise.resolve(true),
116+
extract: jest.fn().mockImplementation((state) => ({ state, references: [] })),
117+
inject: jest.fn().mockImplementation((state) => state),
118+
telemetry: jest.fn().mockResolvedValue({}),
119+
migrations: { '7.12.0': jest.fn().mockImplementation((state) => state) },
120+
};
121+
122+
const containerState = {
123+
id: containerEmbeddableFactoryId,
124+
type: containerEmbeddableFactoryId,
125+
some: 'state',
126+
panels: [
127+
{
128+
...embeddableState,
129+
},
130+
],
131+
} as any;
132+
133+
setup.registerEmbeddableFactory(embeddableFactoryId, embeddableFactory);
134+
setup.registerEmbeddableFactory(containerEmbeddableFactoryId, containerEmbeddableFactory);
135+
109136
test('cannot register embeddable factory with the same ID', async () => {
110-
setup.registerEmbeddableFactory(embeddableFactoryId, embeddableFactory);
111137
expect(() =>
112138
setup.registerEmbeddableFactory(embeddableFactoryId, embeddableFactory)
113139
).toThrowError(
@@ -134,6 +160,11 @@ describe('embeddable factory', () => {
134160
start.getAllMigrations!()['7.11.0']!(embeddableState);
135161
expect(embeddableFactory.migrations['7.11.0']).toBeCalledWith(embeddableState);
136162
});
163+
164+
test('panels inside container get automatically migrated when migrating conta1iner', () => {
165+
start.getAllMigrations!()['7.11.0']!(containerState);
166+
expect(embeddableFactory.migrations['7.11.0']).toBeCalledWith(embeddableState);
167+
});
137168
});
138169

139170
describe('embeddable enhancements', () => {

src/plugins/embeddable/public/plugin.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ export class EmbeddablePublicPlugin implements Plugin<EmbeddableSetup, Embeddabl
208208
inject: getInjectFunction(commonContract),
209209
getAllMigrations: () =>
210210
getAllMigrations(
211-
this.embeddableFactories.values(),
212-
this.enhancements.values(),
211+
Array.from(this.embeddableFactories.values()),
212+
Array.from(this.enhancements.values()),
213213
getMigrateFunction(commonContract)
214214
),
215215
};

0 commit comments

Comments
 (0)