Skip to content

Commit

Permalink
updated reporting to pass in data plugin to getSharingData, also upda…
Browse files Browse the repository at this point in the history
…tes jest tests
  • Loading branch information
jloleysens committed Sep 21, 2021
1 parent a8bff04 commit 606d1b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { CoreStart } from 'src/core/public';
import type { SearchSource } from 'src/plugins/data/common';
import type { SavedSearch } from 'src/plugins/discover/public';
import { coreMock } from '../../../../../src/core/public/mocks';
import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';
import type { ILicense, LicensingPluginSetup } from '../../../licensing/public';
import { ReportingAPIClient } from '../lib/reporting_api_client';
import type { ReportingPublicPluginStartDendencies } from '../plugin';
import type { ActionContext } from './get_csv_panel_action';
import { ReportingCsvPanelAction } from './get_csv_panel_action';

Expand All @@ -25,8 +27,8 @@ describe('GetCsvReportPanelAction', () => {
let context: ActionContext;
let mockLicense$: (state?: LicenseResults) => Rx.Observable<ILicense>;
let mockSearchSource: SearchSource;
let mockStartServicesPayload: [CoreStart, object, unknown];
let mockStartServices$: Rx.Subject<typeof mockStartServicesPayload>;
let mockStartServicesPayload: [CoreStart, ReportingPublicPluginStartDendencies, unknown];
let mockStartServices$: Rx.Observable<typeof mockStartServicesPayload>;

beforeAll(() => {
if (typeof window.URL.revokeObjectURL === 'undefined') {
Expand All @@ -48,14 +50,17 @@ describe('GetCsvReportPanelAction', () => {
}) as unknown as LicensingPluginSetup['license$'];
};

mockStartServices$ = new Rx.Subject<[CoreStart, object, unknown]>();
mockStartServicesPayload = [
{
...core,
application: { capabilities: { dashboard: { downloadCsv: true } } },
} as unknown as CoreStart,
{},
{
data: dataPluginMock.createStartContract(),
} as ReportingPublicPluginStartDendencies,
null,
];
mockStartServices$ = Rx.from(Promise.resolve(mockStartServicesPayload));

mockSearchSource = {
createCopy: () => mockSearchSource,
Expand Down Expand Up @@ -93,7 +98,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand Down Expand Up @@ -130,7 +135,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -153,7 +158,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -169,7 +174,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -187,7 +192,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -204,14 +209,13 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);

await mockStartServices$.pipe(first()).toPromise();
await licenseMock$.pipe(first()).toPromise();

expect(await plugin.isCompatible(context)).toEqual(false);
});

it('sets a display and icon type', () => {
it('sets a display and icon type', async () => {
const panel = new ReportingCsvPanelAction({
core,
apiClient,
Expand All @@ -220,33 +224,36 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

expect(panel.getIconType()).toMatchInlineSnapshot(`"document"`);
expect(panel.getDisplayName()).toMatchInlineSnapshot(`"Download CSV"`);
});

describe('Application UI Capabilities', () => {
it(`doesn't allow downloads when UI capability is not enabled`, async () => {
mockStartServicesPayload = [
{ application: { capabilities: {} } } as unknown as CoreStart,
{
data: dataPluginMock.createStartContract(),
} as ReportingPublicPluginStartDendencies,
null,
];
const startServices$ = Rx.from(Promise.resolve(mockStartServicesPayload));
const plugin = new ReportingCsvPanelAction({
core,
apiClient,
license$: mockLicense$(),
startServices$: mockStartServices$,
startServices$,
usesUiCapabilities: true,
});

mockStartServices$.next([
{ application: { capabilities: {} } } as unknown as CoreStart,
{},
null,
]);
await startServices$.pipe(first()).toPromise();

expect(await plugin.isCompatible(context)).toEqual(false);
});

it(`allows downloads when license is valid and UI capability is enabled`, async () => {
mockStartServices$ = new Rx.Subject();
const plugin = new ReportingCsvPanelAction({
core,
apiClient,
Expand All @@ -255,7 +262,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

expect(await plugin.isCompatible(context)).toEqual(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import { i18n } from '@kbn/i18n';
import * as Rx from 'rxjs';
import type { CoreSetup, IUiSettingsClient, NotificationsSetup } from 'src/core/public';
import { first } from 'rxjs/operators';
import type { CoreSetup, NotificationsSetup } from 'src/core/public';
import { CoreStart } from 'src/core/public';
import type { ISearchEmbeddable, SavedSearch } from '../../../../../src/plugins/discover/public';
import {
Expand All @@ -22,6 +23,7 @@ import type { LicensingPluginSetup } from '../../../licensing/public';
import { CSV_REPORTING_ACTION } from '../../common/constants';
import { checkLicense } from '../lib/license_check';
import { ReportingAPIClient } from '../lib/reporting_api_client';
import type { ReportingPublicPluginStartDendencies } from '../plugin';

function isSavedSearchEmbeddable(
embeddable: IEmbeddable | ISearchEmbeddable
Expand All @@ -36,7 +38,7 @@ export interface ActionContext {
interface Params {
apiClient: ReportingAPIClient;
core: CoreSetup;
startServices$: Rx.Observable<[CoreStart, object, unknown]>;
startServices$: Rx.Observable<[CoreStart, ReportingPublicPluginStartDendencies, unknown]>;
license$: LicensingPluginSetup['license$'];
usesUiCapabilities: boolean;
}
Expand All @@ -47,16 +49,16 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
public readonly id = CSV_REPORTING_ACTION;
private licenseHasDownloadCsv: boolean = false;
private capabilityHasDownloadCsv: boolean = false;
private uiSettings: IUiSettingsClient;
private notifications: NotificationsSetup;
private apiClient: ReportingAPIClient;
private startServices$: Params['startServices$'];

constructor({ core, startServices$, license$, usesUiCapabilities, apiClient }: Params) {
this.isDownloading = false;

this.uiSettings = core.uiSettings;
this.notifications = core.notifications;
this.apiClient = apiClient;
this.startServices$ = startServices$;

license$.subscribe((license) => {
const results = license.check('reporting', 'basic');
Expand All @@ -65,7 +67,7 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
});

if (usesUiCapabilities) {
startServices$.subscribe(([{ application }]) => {
this.startServices$.subscribe(([{ application }]) => {
this.capabilityHasDownloadCsv = application.capabilities.dashboard?.downloadCsv === true;
});
} else {
Expand All @@ -84,11 +86,12 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
}

public async getSearchSource(savedSearch: SavedSearch, embeddable: ISearchEmbeddable) {
const [{ uiSettings }, { data }] = await this.startServices$.pipe(first()).toPromise();
const { getSharingData } = await loadSharingDataHelpers();
return await getSharingData(
savedSearch.searchSource,
savedSearch, // TODO: get unsaved state (using embeddale.searchScope): https://github.com/elastic/kibana/issues/43977
this.uiSettings
savedSearch, // TODO: get unsaved state (using embeddable.searchScope): https://github.com/elastic/kibana/issues/43977
{ uiSettings, data }
);
}

Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/reporting/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';
import * as Rx from 'rxjs';
import { catchError, filter, map, mergeMap, takeUntil } from 'rxjs/operators';
import type { DataPublicPluginStart } from 'src/plugins/data/public';
import {
CoreSetup,
CoreStart,
Expand Down Expand Up @@ -77,6 +78,7 @@ export interface ReportingPublicPluginSetupDendencies {

export interface ReportingPublicPluginStartDendencies {
home: HomePublicPluginStart;
data: DataPublicPluginStart;
management: ManagementStart;
licensing: LicensingPluginStart;
uiActions: UiActionsStart;
Expand Down Expand Up @@ -134,7 +136,10 @@ export class ReportingPublicPlugin
return this.contract;
}

public setup(core: CoreSetup, setupDeps: ReportingPublicPluginSetupDendencies) {
public setup(
core: CoreSetup<ReportingPublicPluginStartDendencies>,
setupDeps: ReportingPublicPluginSetupDendencies
) {
const { getStartServices, uiSettings } = core;
const {
home,
Expand Down

0 comments on commit 606d1b8

Please sign in to comment.