Skip to content

Commit

Permalink
More and better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Jul 8, 2020
1 parent 4f70e5e commit 52d44d4
Showing 1 changed file with 156 additions and 1 deletion.
157 changes: 156 additions & 1 deletion src/plugins/usage_collection/server/collector/collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { loggingSystemMock } from '../../../../core/server/mocks';
import { Collector } from './collector';
import { UsageCollector } from './usage_collector';

const logger = loggingSystemMock.createLogger();

Expand All @@ -31,7 +32,110 @@ describe('collector', () => {
);
});

it('should fail if init is not a function', () => {
expect(
() =>
new Collector(logger, {
type: 'my_test_collector',
// @ts-expect-error
init: 1,
})
).toThrowError(
'If init property is passed, Collector must be instantiated with a options.init as a function property'
);
});

it('should fail if fetch is not defined', () => {
expect(
() =>
// @ts-expect-error
new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
})
).toThrowError('Collector must be instantiated with a options.fetch function property');
});

it('should fail if fetch is not a function', () => {
expect(
() =>
new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
// @ts-expect-error
fetch: 1,
})
).toThrowError('Collector must be instantiated with a options.fetch function property');
});

it('should be OK with all mandatory properties', () => {
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
fetch: () => ({ testPass: 100 }),
});
expect(collector).toBeDefined();
});

it('should fallback when isReady is not provided', () => {
const fetchOutput = { testPass: 100 };
// @ts-expect-error not providing isReady to test the logic fallback
const collector = new Collector(logger, {
type: 'my_test_collector',
fetch: () => fetchOutput,
});
expect(collector.isReady()).toBe(true);
});
});

describe('formatForBulkUpload', () => {
it('should use the default formatter', () => {
const fetchOutput = { testPass: 100 };
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
fetch: () => fetchOutput,
});
expect(collector.formatForBulkUpload(fetchOutput)).toStrictEqual({
type: 'my_test_collector',
payload: fetchOutput,
});
});

it('should use a custom formatter', () => {
const fetchOutput = { testPass: 100 };
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
fetch: () => fetchOutput,
formatForBulkUpload: (a) => ({ type: 'other_value', payload: { nested: a } }),
});
expect(collector.formatForBulkUpload(fetchOutput)).toStrictEqual({
type: 'other_value',
payload: { nested: fetchOutput },
});
});

it("should use UsageCollector's default formatter", () => {
const fetchOutput = { testPass: 100 };
const collector = new UsageCollector(logger, {
type: 'my_test_collector',
isReady: () => false,
fetch: () => fetchOutput,
});
expect(collector.formatForBulkUpload(fetchOutput)).toStrictEqual({
type: 'kibana_stats',
payload: { usage: { my_test_collector: fetchOutput } },
});
});
});

describe('schema TS validations', () => {
// These tests below are used to ensure types inference is working as expected.
// We don't intend to test any logic as such, just the relation between the types in `fetch` and `schema`.
// Using ts-expect-error when an error is expected will fail the compilation if there is not such error.

test('when fetch returns a simple object', () => {
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
Expand All @@ -43,7 +147,7 @@ describe('collector', () => {
expect(collector).toBeDefined();
});

it('should be OK with arrays returned by fetch but object-schema', () => {
test('when fetch returns array-properties and schema', () => {
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
Expand All @@ -54,5 +158,56 @@ describe('collector', () => {
});
expect(collector).toBeDefined();
});

test('TS should complain when schema is missing some properties', () => {
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
fetch: () => ({ testPass: [{ name: 'a', value: 100 }], otherProp: 1 }),
// @ts-expect-error
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
},
});
expect(collector).toBeDefined();
});

test('TS complains if schema misses any of the optional properties', () => {
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
// Need to be explicit with the returned type because TS struggles to identify it
fetch: (): { testPass?: Array<{ name: string; value: number }>; otherProp?: number } => {
if (Math.random() > 0.5) {
return { testPass: [{ name: 'a', value: 100 }] };
}
return { otherProp: 1 };
},
// @ts-expect-error
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
},
});
expect(collector).toBeDefined();
});

test('schema defines all the optional properties', () => {
const collector = new Collector(logger, {
type: 'my_test_collector',
isReady: () => false,
// Need to be explicit with the returned type because TS struggles to identify it
fetch: (): { testPass?: Array<{ name: string; value: number }>; otherProp?: number } => {
if (Math.random() > 0.5) {
return { testPass: [{ name: 'a', value: 100 }] };
}
return { otherProp: 1 };
},
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
otherProp: { type: 'long' },
},
});
expect(collector).toBeDefined();
});
});
});

0 comments on commit 52d44d4

Please sign in to comment.