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

Fix test mocks + add some plugin async helpers where needed #6

Merged
merged 1 commit into from
Jun 5, 2020
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
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at the setupReady and startReady methods, and thought, reportingCore can have methods like that using the internal state it already has. I think these break out of ReportingCore's centralized state.

  public async setupReady() {
    return this.reportingCore.pluginHasSetUp(); // need to implement
  }

  public async startReady() {
    return this.reportingCore.pluginHasStarted(); // this already exists
  }

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this.reportingCore could be undefined because it depends on config..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++ agreed, will move in a follow up PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nm, I took a look at shoving the implementation details into core, but because core setup is async there's still a lot of detail in plugin, so I think it makes sense to keep it this way until we can refactor things out in a more sync-ish way.

}

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