Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Apr 1, 2020
1 parent 1a81c89 commit 58c12cb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { CancellationToken } from '../../../common/cancellation_token';
import { fieldFormats } from '../../../../../../../src/plugins/data/server';
import { createMockReportingCore } from '../../../test_helpers';
import { LevelLogger } from '../../../server/lib/level_logger';
import { executeJobFactory } from './execute_job';
import { setFieldFormats } from '../../../server/services';
import { executeJobFactory } from './execute_job';

const delay = ms => new Promise(resolve => setTimeout(() => resolve(), ms));

Expand Down Expand Up @@ -58,15 +58,13 @@ describe('CSV Execute Job', function() {
});

beforeEach(async function() {
mockReportingPlugin = await createMockReportingCore();

configGetStub = sinon.stub();
configGetStub.withArgs('encryptionKey').returns(encryptionKey);
configGetStub.withArgs('csv', 'maxSizeBytes').returns(1024 * 1000); // 1mB
configGetStub.withArgs('csv', 'scroll').returns({});
mockReportingConfig = { get: configGetStub, kbnConfig: { get: configGetStub } };

mockReportingPlugin.getConfig = () => Promise.resolve(mockReportingConfig);
mockReportingPlugin = await createMockReportingCore(mockReportingConfig);
mockReportingPlugin.getUiSettingsServiceFactory = () => Promise.resolve(mockUiSettingsClient);
mockReportingPlugin.getElasticsearchService = () => Promise.resolve(mockElasticsearch);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { LevelLogger } from '../../../../server/lib';
jest.mock('../lib/generate_png', () => ({ generatePngObservableFactory: jest.fn() }));

let mockReporting;
let mockReportingConfig;

const cancellationToken = {
on: jest.fn(),
Expand All @@ -36,8 +35,6 @@ const encryptHeaders = async headers => {
};

beforeEach(async () => {
mockReporting = await createMockReportingCore();

const kbnConfig = {
'server.basePath': '/sbp',
};
Expand All @@ -47,14 +44,12 @@ beforeEach(async () => {
'kibanaServer.port': 5601,
'kibanaServer.protocol': 'http',
};

const mockGetConfig = jest.fn();
mockReportingConfig = {
const mockReportingConfig = {
get: (...keys) => reportingConfig[keys.join('.')],
kbnConfig: { get: (...keys) => kbnConfig[keys.join('.')] },
};
mockGetConfig.mockImplementation(() => Promise.resolve(mockReportingConfig));
mockReporting.getConfig = mockGetConfig;

mockReporting = await createMockReportingCore(mockReportingConfig);

const mockElasticsearch = {
dataClient: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { LevelLogger } from '../../../../server/lib';
jest.mock('../lib/generate_pdf', () => ({ generatePdfObservableFactory: jest.fn() }));

let mockReporting;
let mockReportingConfig;

const cancellationToken = {
on: jest.fn(),
Expand All @@ -36,8 +35,6 @@ const encryptHeaders = async headers => {
};

beforeEach(async () => {
mockReporting = await createMockReportingCore();

const kbnConfig = {
'server.basePath': '/sbp',
};
Expand All @@ -47,14 +44,12 @@ beforeEach(async () => {
'kibanaServer.port': 5601,
'kibanaServer.protocol': 'http',
};

const mockGetConfig = jest.fn();
mockReportingConfig = {
const mockReportingConfig = {
get: (...keys) => reportingConfig[keys.join('.')],
kbnConfig: { get: (...keys) => kbnConfig[keys.join('.')] },
};
mockGetConfig.mockImplementation(() => Promise.resolve(mockReportingConfig));
mockReporting.getConfig = mockGetConfig;

mockReporting = await createMockReportingCore(mockReportingConfig);

const mockElasticsearch = {
dataClient: {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ReportingPlugin
this.reportingCore.legacySetup(xpackMainLegacy, reportingLegacy, __LEGACY, plugins);

// Register a function with server to manage the collection of usage stats
registerReportingUsageCollector(this.reportingCore, config, plugins);
registerReportingUsageCollector(this.reportingCore, plugins);

// regsister setup internals
this.reportingCore.pluginSetup({ browserDriverFactory, elasticsearch });
Expand Down
45 changes: 7 additions & 38 deletions x-pack/legacy/plugins/reporting/server/routes/jobs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ beforeEach(async () => {
jobContentExtension: 'pdf',
});

mockReportingPlugin = await createMockReportingCore();
mockReportingPlugin.getExportTypesRegistry = () => exportTypesRegistry;

mockReportingConfig = { get: jest.fn(), kbnConfig: { get: jest.fn() } };
mockReportingPlugin = await createMockReportingCore(mockReportingConfig);
mockReportingPlugin.getExportTypesRegistry = () => exportTypesRegistry;
});

const mockPlugins = {
Expand All @@ -72,13 +71,7 @@ test(`returns 404 if job not found`, async () => {
callAsInternalUser: jest.fn().mockReturnValue(Promise.resolve(getHits())),
};

registerJobInfoRoutes(
mockReportingPlugin,
mockReportingConfig,
mockServer,
mockPlugins,
mockLogger
);
registerJobInfoRoutes(mockReportingPlugin, mockServer, mockPlugins, mockLogger);

const request = {
method: 'GET',
Expand All @@ -97,13 +90,7 @@ test(`returns 401 if not valid job type`, async () => {
.mockReturnValue(Promise.resolve(getHits({ jobtype: 'invalidJobType' }))),
};

registerJobInfoRoutes(
mockReportingPlugin,
mockReportingConfig,
mockServer,
mockPlugins,
mockLogger
);
registerJobInfoRoutes(mockReportingPlugin, mockServer, mockPlugins, mockLogger);

const request = {
method: 'GET',
Expand All @@ -124,13 +111,7 @@ describe(`when job is incomplete`, () => {
),
};

registerJobInfoRoutes(
mockReportingPlugin,
mockReportingConfig,
mockServer,
mockPlugins,
mockLogger
);
registerJobInfoRoutes(mockReportingPlugin, mockServer, mockPlugins, mockLogger);

const request = {
method: 'GET',
Expand Down Expand Up @@ -172,13 +153,7 @@ describe(`when job is failed`, () => {
callAsInternalUser: jest.fn().mockReturnValue(Promise.resolve(hits)),
};

registerJobInfoRoutes(
mockReportingPlugin,
mockReportingConfig,
mockServer,
mockPlugins,
mockLogger
);
registerJobInfoRoutes(mockReportingPlugin, mockServer, mockPlugins, mockLogger);

const request = {
method: 'GET',
Expand Down Expand Up @@ -223,13 +198,7 @@ describe(`when job is completed`, () => {
callAsInternalUser: jest.fn().mockReturnValue(Promise.resolve(hits)),
};

registerJobInfoRoutes(
mockReportingPlugin,
mockReportingConfig,
mockServer,
mockPlugins,
mockLogger
);
registerJobInfoRoutes(mockReportingPlugin, mockServer, mockPlugins, mockLogger);

const request = {
method: 'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ function getPluginsMock(
};
}

const getMockReportingConfig = () => ({
get: () => {},
kbnConfig: { get: () => '' },
});
const getResponseMock = (customization = {}) => customization;

describe('license checks', () => {
let mockConfig;
beforeAll(async () => {
const mockReporting = await createMockReportingCore();
mockConfig = mockReporting.getConfig();
mockConfig = getMockReportingConfig();
});

describe('with a basic license', () => {
Expand Down Expand Up @@ -150,8 +153,7 @@ describe('license checks', () => {

describe('data modeling', () => {
test('with normal looking usage data', async () => {
const mockReporting = await createMockReportingCore();
const mockConfig = mockReporting.getConfig();
const mockConfig = getMockReportingConfig();
const plugins = getPluginsMock();
const { fetch } = getReportingUsageCollector(mockConfig, plugins, exportTypesRegistry);
const callClusterMock = jest.fn(() =>
Expand Down Expand Up @@ -372,15 +374,15 @@ describe('data modeling', () => {

describe('Ready for collection observable', () => {
test('converts observable to promise', async () => {
const mockReporting = await createMockReportingCore();
const mockConfig = mockReporting.getConfig();
const mockConfig = getMockReportingConfig();
const mockReporting = await createMockReportingCore(mockConfig);

const usageCollection = getMockUsageCollection();
const makeCollectorSpy = sinon.spy();
usageCollection.makeUsageCollector = makeCollectorSpy;

const plugins = getPluginsMock({ usageCollection });
registerReportingUsageCollector(mockReporting, mockConfig, plugins);
registerReportingUsageCollector(mockReporting, plugins);

const [args] = makeCollectorSpy.firstCall.args;
expect(args).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export function getReportingUsageCollector(

export function registerReportingUsageCollector(
reporting: ReportingCore,
config: ReportingConfig,
plugins: ReportingSetupDeps
) {
const exportTypesRegistry = reporting.getExportTypesRegistry();
const collectionIsReady = reporting.pluginHasStarted.bind(reporting);
const config = reporting.getConfig();

const collector = getReportingUsageCollector(
config,
Expand Down

0 comments on commit 58c12cb

Please sign in to comment.