Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Jul 6, 2021
1 parent aa648ec commit 82401dc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
34 changes: 34 additions & 0 deletions src/plugins/embeddable/common/lib/get_all_migrations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { getAllMigrations } from './get_all_migrations';

describe('embeddable getAllMigratons', () => {
const factories = [{ migrations: { '7.11.0': (state: any) => state } }];
const enhacements = [{ migrations: { '7.12.0': (state: any) => state } }];
const migrateFn = jest.fn();

test('returns base migrations', () => {
expect(getAllMigrations([], [], migrateFn)).toEqual({});
});

test('returns embeddable factory migrations', () => {
expect(getAllMigrations(factories as any, [], migrateFn)).toHaveProperty(['7.11.0']);
});

test('returns enhancement migrations', () => {
const migrations = getAllMigrations([], enhacements as any, migrateFn);
expect(migrations).toHaveProperty(['7.12.0']);
});

test('returns all migrations', () => {
const migrations = getAllMigrations(factories as any, enhacements as any, migrateFn);
expect(migrations).toHaveProperty(['7.11.0']);
expect(migrations).toHaveProperty(['7.12.0']);
});
});
20 changes: 16 additions & 4 deletions src/plugins/embeddable/common/lib/get_all_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@
*/

import { baseEmbeddableMigrations } from './migrate_base_input';
import { MigrateFunctionsObject } from '../../../kibana_utils/common/persistable_state';
import {
MigrateFunctionsObject,
PersistableState,
PersistableStateMigrateFn,
} from '../../../kibana_utils/common/persistable_state';

export const getAllMigrations = (factories: any, enhancements: any, migrateFn: any) => {
export const getAllMigrations = (
factories: unknown[],
enhancements: unknown[],
migrateFn: PersistableStateMigrateFn
) => {
const uniqueVersions = new Set<string>();
for (const baseMigrationVersion of Object.keys(baseEmbeddableMigrations)) {
uniqueVersions.add(baseMigrationVersion);
}
for (const factory of factories) {
Object.keys(factory.migrations).forEach((version) => uniqueVersions.add(version));
Object.keys((factory as PersistableState).migrations).forEach((version) =>
uniqueVersions.add(version)
);
}
for (const enhancement of enhancements) {
Object.keys(enhancement.migrations).forEach((version) => uniqueVersions.add(version));
Object.keys((enhancement as PersistableState).migrations).forEach((version) =>
uniqueVersions.add(version)
);
}

const migrations: MigrateFunctionsObject = {};
Expand Down
33 changes: 32 additions & 1 deletion src/plugins/embeddable/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,34 @@ describe('embeddable factory', () => {
my: 'state',
} as any;

const containerEmbeddableFactoryId = 'CONTAINER';
const containerEmbeddableFactory = {
type: containerEmbeddableFactoryId,
create: jest.fn(),
getDisplayName: () => 'Container',
isContainer: true,
isEditable: () => Promise.resolve(true),
extract: jest.fn().mockImplementation((state) => ({ state, references: [] })),
inject: jest.fn().mockImplementation((state) => state),
telemetry: jest.fn().mockResolvedValue({}),
migrations: { '7.12.0': jest.fn().mockImplementation((state) => state) },
};

const containerState = {
id: containerEmbeddableFactoryId,
type: containerEmbeddableFactoryId,
some: 'state',
panels: [
{
...embeddableState,
},
],
} as any;

setup.registerEmbeddableFactory(embeddableFactoryId, embeddableFactory);
setup.registerEmbeddableFactory(containerEmbeddableFactoryId, containerEmbeddableFactory);

test('cannot register embeddable factory with the same ID', async () => {
setup.registerEmbeddableFactory(embeddableFactoryId, embeddableFactory);
expect(() =>
setup.registerEmbeddableFactory(embeddableFactoryId, embeddableFactory)
).toThrowError(
Expand All @@ -134,6 +160,11 @@ describe('embeddable factory', () => {
start.getAllMigrations!()['7.11.0']!(embeddableState);
expect(embeddableFactory.migrations['7.11.0']).toBeCalledWith(embeddableState);
});

test('panels inside container get automatically migrated when migrating conta1iner', () => {
start.getAllMigrations!()['7.11.0']!(containerState);
expect(embeddableFactory.migrations['7.11.0']).toBeCalledWith(embeddableState);
});
});

describe('embeddable enhancements', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/embeddable/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ export class EmbeddablePublicPlugin implements Plugin<EmbeddableSetup, Embeddabl
inject: getInjectFunction(commonContract),
getAllMigrations: () =>
getAllMigrations(
this.embeddableFactories.values(),
this.enhancements.values(),
Array.from(this.embeddableFactories.values()),
Array.from(this.enhancements.values()),
getMigrateFunction(commonContract)
),
};
Expand Down

0 comments on commit 82401dc

Please sign in to comment.