Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Embeddables Rebuild] Publish Phase Events #184445

Merged
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 @@ -21,11 +21,11 @@ export interface PhaseEvent {
}

export interface PublishesPhaseEvents {
onPhaseChange: PublishingSubject<PhaseEvent | undefined>;
phase$: PublishingSubject<PhaseEvent | undefined>;
}

export const apiPublishesPhaseEvents = (
unknownApi: null | unknown
): unknownApi is PublishesPhaseEvents => {
return Boolean(unknownApi && (unknownApi as PublishesPhaseEvents)?.onPhaseChange !== undefined);
return Boolean(unknownApi && (unknownApi as PublishesPhaseEvents)?.phase$ !== undefined);
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const Item = React.forwardRef<HTMLDivElement, Props>(
showBorder: useMargins,
showNotifications: true,
showShadow: false,
onPanelStatusChange,
};

// render React embeddable
Expand All @@ -123,7 +124,6 @@ export const Item = React.forwardRef<HTMLDivElement, Props>(
<EmbeddablePanel
key={type}
index={index}
onPanelStatusChange={onPanelStatusChange}
embeddable={() => container.untilEmbeddableLoaded(id)}
{...panelProps}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const legacyEmbeddableToApi = (
/**
* Performance tracking
*/
const onPhaseChange = new BehaviorSubject<PhaseEvent | undefined>(undefined);
const phase$ = new BehaviorSubject<PhaseEvent | undefined>(undefined);

let loadingStartTime = 0;
subscriptions.add(
Expand Down Expand Up @@ -132,7 +132,7 @@ export const legacyEmbeddableToApi = (
})
)
.subscribe((statusOutput) => {
onPhaseChange.next(statusOutput);
phase$.next(statusOutput);
})
);

Expand Down Expand Up @@ -252,7 +252,7 @@ export const legacyEmbeddableToApi = (
dataLoading,
blockingError,

onPhaseChange,
phase$,

onEdit,
isEditingEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test('updating output state retains instance information', async () => {
test('fires phase events when output changes', async () => {
const phaseEventTest = new PhaseTestEmbeddable();
let phaseEventCount = 0;
phaseEventTest.onPhaseChange.subscribe((event) => {
phaseEventTest.phase$.subscribe((event) => {
if (event) {
phaseEventCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export abstract class Embeddable<
dataLoading: this.dataLoading,
filters$: this.filters$,
blockingError: this.blockingError,
onPhaseChange: this.onPhaseChange,
phase$: this.phase$,
setPanelTitle: this.setPanelTitle,
linkToLibrary: this.linkToLibrary,
hidePanelTitle: this.hidePanelTitle,
Expand Down Expand Up @@ -168,7 +168,7 @@ export abstract class Embeddable<
public panelTitle: LegacyEmbeddableAPI['panelTitle'];
public dataLoading: LegacyEmbeddableAPI['dataLoading'];
public filters$: LegacyEmbeddableAPI['filters$'];
public onPhaseChange: LegacyEmbeddableAPI['onPhaseChange'];
public phase$: LegacyEmbeddableAPI['phase$'];
public linkToLibrary: LegacyEmbeddableAPI['linkToLibrary'];
public blockingError: LegacyEmbeddableAPI['blockingError'];
public setPanelTitle: LegacyEmbeddableAPI['setPanelTitle'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@
*/
import { getMockPresentationContainer } from '@kbn/presentation-containers/mocks';
import { setStubKibanaServices as setupPresentationPanelServices } from '@kbn/presentation-panel-plugin/public/mocks';
import { render, waitFor, screen } from '@testing-library/react';
import { render, waitFor, screen, fireEvent } from '@testing-library/react';

import React from 'react';
import { BehaviorSubject } from 'rxjs';
import { registerReactEmbeddableFactory } from './react_embeddable_registry';
import { ReactEmbeddableRenderer } from './react_embeddable_renderer';
import { ReactEmbeddableFactory } from './types';

describe('react embeddable renderer', () => {
const testEmbeddableFactory: ReactEmbeddableFactory<{ name: string; bork: string }> = {
type: 'test',
deserializeState: jest.fn().mockImplementation((state) => state.rawState),
buildEmbeddable: async (state, registerApi) => {
const api = registerApi(
{
serializeState: () => ({
rawState: {
name: state.name,
bork: state.bork,
},
}),
},
{
name: [new BehaviorSubject<string>(state.name), () => {}],
bork: [new BehaviorSubject<string>(state.bork), () => {}],
}
);
return {
Component: () => (
<div data-test-subj="superTestEmbeddable">
SUPER TEST COMPONENT, name: {state.name} bork: {state.bork}
</div>
),
api,
};
},
};
const testEmbeddableFactory: ReactEmbeddableFactory<{ name: string; bork: string }> = {
type: 'test',
deserializeState: jest.fn().mockImplementation((state) => state.rawState),
buildEmbeddable: async (state, registerApi) => {
const api = registerApi(
{
serializeState: () => ({
rawState: {
name: state.name,
bork: state.bork,
},
}),
},
{
name: [new BehaviorSubject<string>(state.name), () => {}],
bork: [new BehaviorSubject<string>(state.bork), () => {}],
}
);
return {
Component: () => (
<div data-test-subj="superTestEmbeddable">
SUPER TEST COMPONENT, name: {state.name} bork: {state.bork}
</div>
),
api,
};
},
};

describe('react embeddable renderer', () => {
const getTestEmbeddableFactory = async () => {
return testEmbeddableFactory;
};
Expand Down Expand Up @@ -185,6 +185,7 @@ describe('react embeddable renderer', () => {
serializeState: expect.any(Function),
resetUnsavedChanges: expect.any(Function),
snapshotRuntimeState: expect.any(Function),
phase$: expect.any(Object),
})
);
});
Expand All @@ -209,3 +210,104 @@ describe('react embeddable renderer', () => {
);
});
});

describe('reactEmbeddable phase events', () => {
it('publishes rendered phase immediately when dataLoading is not defined', async () => {
const immediateLoadEmbeddableFactory: ReactEmbeddableFactory<{ name: string; bork: string }> = {
...testEmbeddableFactory,
type: 'immediateLoad',
};
registerReactEmbeddableFactory('immediateLoad', () =>
Promise.resolve(immediateLoadEmbeddableFactory)
);
setupPresentationPanelServices();

const renderedEvent = jest.fn();
render(
<ReactEmbeddableRenderer
type={'test'}
maybeId={'12345'}
onApiAvailable={(api) => {
api.phase$.subscribe((phase) => {
if (phase?.status === 'rendered') {
renderedEvent();
}
});
}}
getParentApi={() => ({
getSerializedStateForChild: () => ({
rawState: { name: 'Kuni Garu' },
}),
})}
/>
);
await waitFor(() => expect(renderedEvent).toHaveBeenCalled());
});

it('publishes rendered phase event when dataLoading is complete', async () => {
const dataLoadingEmbeddableFactory: ReactEmbeddableFactory<{ name: string; bork: string }> = {
...testEmbeddableFactory,
type: 'loadClicker',
buildEmbeddable: async (state, registerApi) => {
const dataLoading = new BehaviorSubject<boolean | undefined>(true);
const api = registerApi(
{
serializeState: () => ({
rawState: {
name: state.name,
bork: state.bork,
},
}),
dataLoading,
},
{
name: [new BehaviorSubject<string>(state.name), () => {}],
bork: [new BehaviorSubject<string>(state.bork), () => {}],
}
);
return {
Component: () => (
<>
<div data-test-subj="superTestEmbeddable">
SUPER TEST COMPONENT, name: {state.name} bork: {state.bork}
</div>
<button data-test-subj="clickToStopLoading" onClick={() => dataLoading.next(false)}>
Done loading
</button>
</>
),
api,
};
},
};
registerReactEmbeddableFactory('loadClicker', () =>
Promise.resolve(dataLoadingEmbeddableFactory)
);
setupPresentationPanelServices();

const phaseFn = jest.fn();
render(
<ReactEmbeddableRenderer
type={'loadClicker'}
maybeId={'12345'}
onApiAvailable={(api) => {
api.phase$.subscribe((phase) => {
phaseFn(phase);
});
}}
getParentApi={() => ({
getSerializedStateForChild: () => ({
rawState: { name: 'Kuni Garu' },
}),
})}
/>
);
await waitFor(() => {
expect(phaseFn).toHaveBeenCalledWith(expect.objectContaining({ status: 'loading' }));
});
await fireEvent.click(screen.getByTestId('clickToStopLoading'));
await waitFor(() => {
expect(phaseFn).toHaveBeenCalledWith(expect.objectContaining({ status: 'rendered' }));
});
});
});
Loading