Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Jul 16, 2024
1 parent 0dafc71 commit e312823
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 { DefaultControlApi } from '../types';
import { initControlsManager } from './init_controls_manager';

jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('delta'),
}));

describe('PresentationContainer api', () => {
test('addNewPanel should add control at end of controls', async () => {
const controlsManager = initControlsManager({
alpha: { type: 'whatever', order: 0 },
bravo: { type: 'whatever', order: 1 },
charlie: { type: 'whatever', order: 2 },
});
const addNewPanelPromise = controlsManager.api.addNewPanel({
panelType: 'whatever',
initialState: {},
});
controlsManager.setControlApi('delta', {} as unknown as DefaultControlApi);
await addNewPanelPromise;
expect(controlsManager.controlsInOrder$.value.map((element) => element.id)).toEqual([
'alpha',
'bravo',
'charlie',
'delta',
]);
});

test('removePanel should remove control', () => {
const controlsManager = initControlsManager({
alpha: { type: 'whatever', order: 0 },
bravo: { type: 'whatever', order: 1 },
charlie: { type: 'whatever', order: 2 },
});
controlsManager.api.removePanel('bravo');
expect(controlsManager.controlsInOrder$.value.map((element) => element.id)).toEqual([
'alpha',
'charlie',
]);
});

test('replacePanel should replace control', async () => {
const controlsManager = initControlsManager({
alpha: { type: 'whatever', order: 0 },
bravo: { type: 'whatever', order: 1 },
charlie: { type: 'whatever', order: 2 },
});
const replacePanelPromise = controlsManager.api.replacePanel('bravo', {
panelType: 'whatever',
initialState: {},
});
controlsManager.setControlApi('delta', {} as unknown as DefaultControlApi);
await replacePanelPromise;
expect(controlsManager.controlsInOrder$.value.map((element) => element.id)).toEqual([
'alpha',
'delta',
'charlie',
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export function initControlsManager(initialControlPanelsState: ControlPanelsStat
}

// control removed before the control finished loading.
const controlState = controlsInOrder$.value.find(
(controlPanelState) => controlPanelState.id === id
);
const controlState = controlsInOrder$.value.find((element) => element.id === id);
if (!controlState) {
subscription.unsubscribe();
resolve(undefined);
Expand All @@ -65,16 +63,17 @@ export function initControlsManager(initialControlPanelsState: ControlPanelsStat
return children$.value[controlUuid];
}

async function addNewPanel({ panelType, initialState }: PanelPackage<DefaultControlState>) {
async function addNewPanel(
{ panelType, initialState }: PanelPackage<DefaultControlState>,
index: number
) {
const id = generateId();
const controlsInOrder = controlsInOrder$.getValue();
controlsInOrder$.next([
...controlsInOrder,
{
id,
type: panelType,
},
]);
const nextControlsInOrder = [...controlsInOrder$.value];
nextControlsInOrder.splice(index, 0, {
id,
type: panelType,
});
controlsInOrder$.next(nextControlsInOrder);
controlsPanelState[id] = initialState ?? {};
return await untilControlLoaded(id);
}
Expand Down Expand Up @@ -141,11 +140,17 @@ export function initControlsManager(initialControlPanelsState: ControlPanelsStat
getPanelCount: () => {
return controlsInOrder$.value.length;
},
addNewPanel,
addNewPanel: async (panel: PanelPackage<DefaultControlState>) => {
return addNewPanel(panel, controlsInOrder$.value.length);
},
removePanel,
replacePanel: async (panelId, newPanel) => {
const index = controlsInOrder$.value.findIndex(({ id }) => id === panelId);
removePanel(panelId);
const controlApi = await addNewPanel(newPanel);
const controlApi = await addNewPanel(
newPanel,
index >= 0 ? index : controlsInOrder$.value.length
);
return controlApi ? controlApi.uuid : '';
},
} as PresentationContainer & HasSerializedChildState<ControlPanelState>,
Expand Down

0 comments on commit e312823

Please sign in to comment.