Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('initializeControlGroupManager', () => {

describe('get state for control group', () => {
it('should return default state when no initial state is provided', () => {
const { internalApi } = initializeControlGroupManager(undefined, getReferences);
const { internalApi } = initializeControlGroupManager(undefined, getReferences, 'view');
const state = internalApi.getStateForControlGroup();
expect(state.rawState).toBeDefined();
expect(state.rawState.controls).toEqual([]);
Expand All @@ -43,15 +43,25 @@ describe('initializeControlGroupManager', () => {
},
labelPosition: 'oneLine',
};
const { internalApi } = initializeControlGroupManager(initialState, getReferences);
const { internalApi } = initializeControlGroupManager(initialState, getReferences, 'view');
const state = internalApi.getStateForControlGroup();
expect(state.rawState).toEqual(initialState);
});
});

describe('initialization', () => {
it('isFetchPaused$ should emit false before the control group is set when the Dashboard is in print mode.', async () => {
const { api } = initializeControlGroupManager(undefined, getReferences, 'print');
const firstEmit = await firstValueFrom(api.isFetchPaused$);
expect(firstEmit).toBe(true);

const lastEmitPromise = lastValueFrom(api.isFetchPaused$.pipe(take(2)));
const lastEmit = await lastEmitPromise;
expect(lastEmit).toBe(false);
});

it('isFetchPaused$ should emit true initially, then false after controls are initialized', async () => {
const { api, internalApi } = initializeControlGroupManager(undefined, getReferences);
const { api, internalApi } = initializeControlGroupManager(undefined, getReferences, 'view');
const mockControlGroupApi = {
untilFiltersPublished: jest.fn().mockResolvedValue(undefined),
} as unknown as ControlGroupApi;
Expand All @@ -67,7 +77,7 @@ describe('initializeControlGroupManager', () => {
});

it('should publish controlGroupApi when it becomes available', async () => {
const { api, internalApi } = initializeControlGroupManager(undefined, getReferences);
const { api, internalApi } = initializeControlGroupManager(undefined, getReferences, 'view');
const mockControlGroupApi = {
untilFiltersPublished: jest.fn().mockResolvedValue(undefined),
} as unknown as ControlGroupApi;
Expand All @@ -83,7 +93,7 @@ describe('initializeControlGroupManager', () => {

describe('serialize control group state', () => {
it('should serialize control group state', () => {
const { internalApi } = initializeControlGroupManager(undefined, getReferences);
const { internalApi } = initializeControlGroupManager(undefined, getReferences, 'view');
const mockSerializedState = {
rawState: { controls: [{ id: '1' }] },
references: [{ id: 'ref1', name: 'refName' }],
Expand All @@ -102,7 +112,7 @@ describe('initializeControlGroupManager', () => {
});

it('should return empty state when serializing with no control group api', () => {
const { internalApi } = initializeControlGroupManager(undefined, getReferences);
const { internalApi } = initializeControlGroupManager(undefined, getReferences, 'view');
const serialized = internalApi.serializeControlGroup();
expect(serialized.controlGroupInput).toBeUndefined();
expect(serialized.controlGroupReferences).toEqual([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
*/

import type { Reference } from '@kbn/content-management-utils';
import type { ControlsGroupState } from '@kbn/controls-schemas';
import type { ControlGroupApi } from '@kbn/controls-plugin/public';
import type { ControlsGroupState } from '@kbn/controls-schemas';
import type { ViewMode } from '@kbn/presentation-publishing';
import { BehaviorSubject, first, from, skipWhile, startWith, switchMap } from 'rxjs';

export const CONTROL_GROUP_EMBEDDABLE_ID = 'CONTROL_GROUP_EMBEDDABLE_ID';

export function initializeControlGroupManager(
initialState: ControlsGroupState | undefined,
getReferences: (id: string) => Reference[]
getReferences: (id: string) => Reference[],
initialViewMode: ViewMode
) {
const controlGroupApi$ = new BehaviorSubject<ControlGroupApi | undefined>(undefined);

async function untilControlsInitialized(): Promise<void> {
if (initialViewMode === 'print') return; // Controls are never initialized in print mode.
return new Promise((resolve) => {
controlGroupApi$
.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export function getDashboardApi({
incomingControlGroup
);

const controlGroupManager = initializeControlGroupManager(mergedControlGroupState, getReferences);
const controlGroupManager = initializeControlGroupManager(
mergedControlGroupState,
getReferences,
viewModeManager.api.viewMode$.value
);

const dataLoadingManager = initializeDataLoadingManager(layoutManager.api.children$);
const dataViewsManager = initializeDataViewsManager(
Expand Down