Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Feb 13, 2020
1 parent 25dc761 commit dd0fd40
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { createMockServer } from '../../../test_helpers';
import { createMockReportingPlugin, createMockServer } from '../../../test_helpers';
import { JobDocPayload } from '../../../types';
import { getConditionalHeaders } from './index';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { getConditionalHeaders, getCustomLogo } from './index';

let mockReportingPlugin: any;
let mockServer: any;
beforeEach(async () => {
mockReportingPlugin = createMockReportingPlugin();
mockServer = createMockServer('');
});

Expand Down Expand Up @@ -146,6 +149,87 @@ describe('conditions', () => {
});
});

test('uses basePath from job when creating saved object service', async () => {
const logo = 'custom-logo';
const mockUiSettingsClient = { get: () => Promise.resolve(logo) };
mockReportingPlugin.getUiSettingsServiceFactory = jest
.fn()
.mockResolvedValueOnce(mockUiSettingsClient);
mockReportingPlugin.getSavedObjectsClient = jest.fn();

const permittedHeaders = {
foo: 'bar',
baz: 'quix',
};
const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
filteredHeaders: permittedHeaders,
server: mockServer,
});
const jobBasePath = '/sbp/s/marketing';
await getCustomLogo({
reporting: mockReportingPlugin,
job: { basePath: jobBasePath } as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

const getBasePath = mockReportingPlugin.getSavedObjectsClient.mock.calls[0][0].getBasePath;
expect(getBasePath()).toBe(jobBasePath);
});

test(`uses basePath from server if job doesn't have a basePath when creating saved object service`, async () => {
const logo = 'custom-logo';
const mockUiSettingsClient = { get: () => Promise.resolve(logo) };
mockReportingPlugin.getUiSettingsServiceFactory = jest
.fn()
.mockResolvedValueOnce(mockUiSettingsClient);
mockReportingPlugin.getSavedObjectsClient = jest.fn();

const permittedHeaders = {
foo: 'bar',
baz: 'quix',
};
const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
filteredHeaders: permittedHeaders,
server: mockServer,
});

await getCustomLogo({
reporting: mockReportingPlugin,
job: {} as JobDocPayloadPDF,
conditionalHeaders,
server: mockServer,
});

const getBasePath = mockReportingPlugin.getSavedObjectsClient.mock.calls[0][0].getBasePath;
expect(getBasePath()).toBe(`/sbp`);
expect(mockReportingPlugin.getSavedObjectsClient.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"getBasePath": [Function],
"headers": Object {
"baz": "quix",
"foo": "bar",
},
"path": "/",
"raw": Object {
"req": Object {
"url": "/",
},
},
"route": Object {
"settings": Object {},
},
"url": Object {
"href": "/",
},
},
]
`);
});

describe('config formatting', () => {
test(`lowercases server.host`, async () => {
mockServer = createMockServer({ settings: { 'server.host': 'COOL-HOSTNAME' } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import * as Rx from 'rxjs';
import sinon from 'sinon';
import { getExportTypesRegistry } from '../lib/export_types_registry';
import { getReportingUsageCollector } from './reporting_usage_collector';
import {
getReportingUsageCollector,
registerReportingUsageCollector,
} from './reporting_usage_collector';

const exportTypesRegistry = getExportTypesRegistry();

Expand Down Expand Up @@ -317,94 +322,124 @@ describe('data modeling', () => {

const usageStats = await getReportingUsage(callClusterMock);
expect(usageStats).toMatchInlineSnapshot(`
Object {
"PNG": Object {
"available": true,
"total": 4,
},
"_all": 54,
"available": true,
"browser_type": undefined,
"csv": Object {
"available": true,
"total": 27,
},
"enabled": true,
"last7Days": Object {
"PNG": Object {
"available": true,
"total": 4,
},
"_all": 27,
"csv": Object {
"available": true,
"total": 10,
},
"printable_pdf": Object {
"app": Object {
"dashboard": 13,
"visualization": 0,
},
"available": true,
"layout": Object {
"preserve_layout": 3,
"print": 10,
},
"total": 13,
},
"status": Object {
"completed": 0,
"failed": 0,
"pending": 27,
},
},
"lastDay": Object {
"PNG": Object {
"available": true,
"total": 4,
},
"_all": 11,
"csv": Object {
"available": true,
"total": 5,
},
"printable_pdf": Object {
"app": Object {
"dashboard": 2,
"visualization": 0,
},
"available": true,
"layout": Object {
"preserve_layout": 0,
"print": 2,
},
"total": 2,
},
"status": Object {
"completed": 0,
"failed": 0,
"pending": 11,
},
},
"printable_pdf": Object {
"app": Object {
"dashboard": 23,
"visualization": 0,
},
"available": true,
"layout": Object {
"preserve_layout": 13,
"print": 10,
},
"total": 23,
},
"status": Object {
"completed": 20,
"failed": 0,
"pending": 33,
"processing": 1,
},
}
`);
Object {
"PNG": Object {
"available": true,
"total": 4,
},
"_all": 54,
"available": true,
"browser_type": undefined,
"csv": Object {
"available": true,
"total": 27,
},
"enabled": true,
"last7Days": Object {
"PNG": Object {
"available": true,
"total": 4,
},
"_all": 27,
"csv": Object {
"available": true,
"total": 10,
},
"printable_pdf": Object {
"app": Object {
"dashboard": 13,
"visualization": 0,
},
"available": true,
"layout": Object {
"preserve_layout": 3,
"print": 10,
},
"total": 13,
},
"status": Object {
"completed": 0,
"failed": 0,
"pending": 27,
},
},
"lastDay": Object {
"PNG": Object {
"available": true,
"total": 4,
},
"_all": 11,
"csv": Object {
"available": true,
"total": 5,
},
"printable_pdf": Object {
"app": Object {
"dashboard": 2,
"visualization": 0,
},
"available": true,
"layout": Object {
"preserve_layout": 0,
"print": 2,
},
"total": 2,
},
"status": Object {
"completed": 0,
"failed": 0,
"pending": 11,
},
},
"printable_pdf": Object {
"app": Object {
"dashboard": 23,
"visualization": 0,
},
"available": true,
"layout": Object {
"preserve_layout": 13,
"print": 10,
},
"total": 23,
},
"status": Object {
"completed": 20,
"failed": 0,
"pending": 33,
"processing": 1,
},
}
`);
});
});

describe('Ready for collection observable', () => {
test('converts observable to promise', async () => {
const serverWithBasicLicenseMock = getServerMock();
const makeCollectorSpy = sinon.spy();
const usageCollection = {
makeUsageCollector: makeCollectorSpy,
registerCollector: sinon.stub(),
};
const start$ = Rx.of({ start: true });
registerReportingUsageCollector(
serverWithBasicLicenseMock,
usageCollection,
start$,
exportTypesRegistry
);

const [args] = makeCollectorSpy.firstCall.args;
expect(args).toMatchInlineSnapshot(`
Object {
"fetch": [Function],
"formatForBulkUpload": [Function],
"isReady": [Function],
"type": "reporting",
}
`);

await expect(args.isReady()).resolves.toBe(true);
});
});

0 comments on commit dd0fd40

Please sign in to comment.