Skip to content

Commit

Permalink
improve types and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Dec 7, 2021
1 parent 2f6f372 commit 8312b75
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
67 changes: 67 additions & 0 deletions x-pack/plugins/screenshotting/server/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

jest.mock('./browsers/install');

import type { ScreenshotModePluginSetup } from 'src/plugins/screenshot_mode/server';
import { CoreSetup, CoreStart, PluginInitializerContext } from 'kibana/server';
import { coreMock } from 'src/core/server/mocks';
import { ScreenshottingPlugin } from './plugin';
import { install } from './browsers/install';

let initContext: PluginInitializerContext;
let coreSetup: CoreSetup;
let coreStart: CoreStart;
let setupDeps: {
screenshotMode: ScreenshotModePluginSetup;
};

beforeEach(() => {
const configSchema = {
browser: { chromium: { disableSandbox: false } },
};
initContext = coreMock.createPluginInitializerContext(configSchema);
coreSetup = coreMock.createSetup({});
coreStart = coreMock.createStart();
setupDeps = {
screenshotMode: {} as ScreenshotModePluginSetup,
};
});

test('sets up and starts properly', async () => {
const plugin = new ScreenshottingPlugin(initContext);
const setupContract = plugin.setup(coreSetup, setupDeps);
expect(setupContract).toMatchInlineSnapshot(`Object {}`);

await coreSetup.getStartServices();

const startContract = plugin.start(coreStart);
expect(startContract).toMatchInlineSnapshot(`
Object {
"diagnose": [Function],
"getScreenshots": [Function],
}
`);
});

test('handles setup issues', async () => {
const plugin = new ScreenshottingPlugin(initContext);
(install as jest.Mock).mockRejectedValue(`Unsupported platform!!!`);

const setupContract = plugin.setup(coreSetup, setupDeps);
expect(setupContract).toMatchInlineSnapshot(`Object {}`);

await coreSetup.getStartServices();

const startContract = plugin.start(coreStart);
expect(startContract).toMatchInlineSnapshot(`
Object {
"diagnose": [Function],
"getScreenshots": [Function],
}
`);
});
32 changes: 20 additions & 12 deletions x-pack/plugins/screenshotting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export class ScreenshottingPlugin implements Plugin<void, ScreenshottingStart, S
private config: ConfigType;
private logger: Logger;
private screenshotMode!: ScreenshotModePluginSetup;
private browserDriverFactory!: Promise<HeadlessChromiumDriverFactory>;
private browserDriverFactory!: Promise<HeadlessChromiumDriverFactory | undefined>;
private error?: Error;

constructor(context: PluginInitializerContext<ConfigType>) {
this.logger = context.logger.get();
Expand All @@ -65,25 +66,32 @@ export class ScreenshottingPlugin implements Plugin<void, ScreenshottingStart, S

return new HeadlessChromiumDriverFactory(this.screenshotMode, config, logger, binaryPath);
} catch (error) {
this.error = error;
this.logger.error('Error in screenshotting setup, it may not function properly.');
this.logger.error(error);
}
})() as Promise<HeadlessChromiumDriverFactory>;
})();

return {};
}

start({}: CoreStart): ScreenshottingStart {
return (
this.browserDriverFactory && {
diagnose: () =>
from(this.browserDriverFactory).pipe(switchMap((factory) => factory.diagnose())),
getScreenshots: (options) =>
from(this.browserDriverFactory).pipe(
switchMap((factory) => getScreenshots(factory, this.logger.get('screenshot'), options))
),
}
);
return {
diagnose: () => {
if (this.error) {
throw this.error;
}
return from(this.browserDriverFactory).pipe(switchMap((factory) => factory!.diagnose()));
},
getScreenshots: (options) => {
if (this.error) {
throw this.error;
}
return from(this.browserDriverFactory).pipe(
switchMap((factory) => getScreenshots(factory!, this.logger.get('screenshot'), options))
);
},
};
}

stop() {}
Expand Down

0 comments on commit 8312b75

Please sign in to comment.