Skip to content

Commit

Permalink
Merge pull request #6 from joelgriffith/reporting/setup-nsyncified
Browse files Browse the repository at this point in the history
Fix test mocks + add some plugin async helpers where needed
  • Loading branch information
tsullivan authored Jun 5, 2020
2 parents 02e78d6 + 10ccc38 commit 7571f94
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
6 changes: 5 additions & 1 deletion x-pack/plugins/reporting/server/config/create_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ describe('Reporting server createConfig$', () => {
mockInitContext = makeMockInitContext({
kibanaServer: {},
});
mockLogger = ({ warn: jest.fn(), debug: jest.fn() } as unknown) as LevelLogger;
mockLogger = ({
warn: jest.fn(),
debug: jest.fn(),
clone: jest.fn().mockImplementation(() => mockLogger),
} as unknown) as LevelLogger;
});

afterEach(() => {
Expand Down
19 changes: 19 additions & 0 deletions x-pack/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ export class ReportingPlugin
private logger: LevelLogger;
private reportingCore?: ReportingCore;

private setupDone: (val?: unknown) => void = () => {};
private startDone: (val?: unknown) => void = () => {};
private setupPromise: Promise<unknown>;
private startPromise: Promise<unknown>;

constructor(context: PluginInitializerContext<ReportingConfigType>) {
this.logger = new LevelLogger(context.logger.get());
this.initializerContext = context;

// Setup some promises for modules that need to await setup/start
this.setupPromise = new Promise((r) => (this.setupDone = r));
this.startPromise = new Promise((r) => (this.startDone = r));
}

public async setupReady() {
return this.setupPromise;
}

public async startReady() {
return this.startPromise;
}

public setup(core: CoreSetup, plugins: ReportingSetupDeps) {
Expand All @@ -49,6 +66,7 @@ export class ReportingPlugin
this.reportingCore = reportingCore;

this.logger.debug('Setup complete');
this.setupDone();
});

return {};
Expand Down Expand Up @@ -81,6 +99,7 @@ export class ReportingPlugin
runValidations(config, elasticsearch, browserDriverFactory, this.logger);

this.logger.debug('Start complete');
this.startDone();
});

return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import { Page } from 'puppeteer';
import * as Rx from 'rxjs';
import { HeadlessChromiumDriver, HeadlessChromiumDriverFactory } from '../browsers';
import { chromium } from '../browsers';
import { chromium, HeadlessChromiumDriver, HeadlessChromiumDriverFactory } from '../browsers';
import * as contexts from '../export_types/common/lib/screenshots/constants';
import { LevelLogger } from '../lib';
import { CaptureConfig, ElementsPositionAndAttribute } from '../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@
jest.mock('../routes');
jest.mock('../usage');
jest.mock('../browsers');
jest.mock('../browsers');
jest.mock('../lib/create_queue');
jest.mock('../lib/enqueue_job');
jest.mock('../lib/validate');

import { of } from 'rxjs';
import { coreMock } from 'src/core/server/mocks';
import {
initializeBrowserDriverFactory,
HeadlessChromiumDriverFactory,
chromium,
} from '../browsers';
import { ReportingConfig, ReportingCore } from '../';
import { ReportingInternalSetup } from '../core';
import { ReportingPlugin } from '../plugin';
import { ReportingSetupDeps, ReportingStartDeps } from '../types';

(initializeBrowserDriverFactory as jest.Mock<
Promise<HeadlessChromiumDriverFactory>
>).mockImplementation(() => Promise.resolve({} as HeadlessChromiumDriverFactory));

(chromium as any).createDriverFactory.mockImplementation(() => ({}));

const createMockSetupDeps = (setupMock?: any): ReportingSetupDeps => {
return {
security: setupMock.security,
Expand Down Expand Up @@ -57,8 +67,10 @@ const createMockReportingPlugin = async (config: ReportingConfig): Promise<Repor
data: { fieldFormats: {} },
};

await plugin.setup(setupMock, createMockSetupDeps(setupMock));
await plugin.start(startMock, createMockStartDeps(startMock));
plugin.setup(setupMock, createMockSetupDeps(setupMock));
await plugin.setupReady();
plugin.start(startMock, createMockStartDeps(startMock));
await plugin.startReady();

return plugin;
};
Expand Down

0 comments on commit 7571f94

Please sign in to comment.