Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VladLasitsa committed Apr 7, 2020
1 parent 359984e commit 497dfa2
Show file tree
Hide file tree
Showing 31 changed files with 304 additions and 106 deletions.
13 changes: 10 additions & 3 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,16 @@ export const npStart = {
aggs: {
calculateAutoTimeExpression: sinon.fake(),
createAggConfigs: (indexPattern, configStates = []) => {
return new AggConfigs(indexPattern, configStates, {
typesRegistry: aggTypesRegistry.start(),
});
return new AggConfigs(
indexPattern,
configStates,
{
typesRegistry: aggTypesRegistry.start(),
},
{
fieldFormats: getFieldFormatsRegistry(mockCoreStart),
}
);
},
types: aggTypesRegistry.start(),
},
Expand Down
88 changes: 71 additions & 17 deletions src/plugins/data/public/search/aggs/agg_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
import { identity } from 'lodash';

import { AggConfig, IAggConfig } from './agg_config';
import { AggConfigs, CreateAggConfigParams } from './agg_configs';
import { AggConfigs, CreateAggConfigParams, AggConfigsDependencies } from './agg_configs';
import { AggType } from './agg_type';
import { AggTypesRegistryStart } from './agg_types_registry';
import { mockDataServices, mockAggTypesRegistry } from './test_helpers';
import { Field as IndexPatternField, IndexPattern } from '../../index_patterns';
import { stubIndexPatternWithFields } from '../../../public/stubs';
import { fieldFormatsServiceMock } from '../../field_formats/mocks';

describe('AggConfig', () => {
let indexPattern: IndexPattern;
let typesRegistry: AggTypesRegistryStart;
const aggConfigsDependencies: AggConfigsDependencies = {
fieldFormats: fieldFormatsServiceMock.createStartContract(),
};

beforeEach(() => {
jest.restoreAllMocks();
Expand All @@ -40,7 +44,7 @@ describe('AggConfig', () => {

describe('#toDsl', () => {
it('calls #write()', () => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const ac = new AggConfigs(indexPattern, [], { typesRegistry }, aggConfigsDependencies);
const configStates = {
enabled: true,
type: 'date_histogram',
Expand All @@ -55,7 +59,7 @@ describe('AggConfig', () => {
});

it('uses the type name as the agg name', () => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const ac = new AggConfigs(indexPattern, [], { typesRegistry }, aggConfigsDependencies);
const configStates = {
enabled: true,
type: 'date_histogram',
Expand All @@ -70,7 +74,7 @@ describe('AggConfig', () => {
});

it('uses the params from #write() output as the agg params', () => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const ac = new AggConfigs(indexPattern, [], { typesRegistry }, aggConfigsDependencies);
const configStates = {
enabled: true,
type: 'date_histogram',
Expand Down Expand Up @@ -100,7 +104,12 @@ describe('AggConfig', () => {
params: {},
},
];
const ac = new AggConfigs(indexPattern, configStates, { typesRegistry });
const ac = new AggConfigs(
indexPattern,
configStates,
{ typesRegistry },
aggConfigsDependencies
);

const histoConfig = ac.byName('date_histogram')[0];
const avgConfig = ac.byName('avg')[0];
Expand Down Expand Up @@ -210,8 +219,18 @@ describe('AggConfig', () => {

testsIdentical.forEach((configState, index) => {
it(`identical aggregations (${index})`, () => {
const ac1 = new AggConfigs(indexPattern, configState, { typesRegistry });
const ac2 = new AggConfigs(indexPattern, configState, { typesRegistry });
const ac1 = new AggConfigs(
indexPattern,
configState,
{ typesRegistry },
aggConfigsDependencies
);
const ac2 = new AggConfigs(
indexPattern,
configState,
{ typesRegistry },
aggConfigsDependencies
);
expect(ac1.jsonDataEquals(ac2.aggs)).toBe(true);
});
});
Expand Down Expand Up @@ -251,8 +270,18 @@ describe('AggConfig', () => {

testsIdenticalDifferentOrder.forEach((test, index) => {
it(`identical aggregations (${index}) - init json is in different order`, () => {
const ac1 = new AggConfigs(indexPattern, test.config1, { typesRegistry });
const ac2 = new AggConfigs(indexPattern, test.config2, { typesRegistry });
const ac1 = new AggConfigs(
indexPattern,
test.config1,
{ typesRegistry },
aggConfigsDependencies
);
const ac2 = new AggConfigs(
indexPattern,
test.config2,
{ typesRegistry },
aggConfigsDependencies
);
expect(ac1.jsonDataEquals(ac2.aggs)).toBe(true);
});
});
Expand Down Expand Up @@ -316,16 +345,26 @@ describe('AggConfig', () => {

testsDifferent.forEach((test, index) => {
it(`different aggregations (${index})`, () => {
const ac1 = new AggConfigs(indexPattern, test.config1, { typesRegistry });
const ac2 = new AggConfigs(indexPattern, test.config2, { typesRegistry });
const ac1 = new AggConfigs(
indexPattern,
test.config1,
{ typesRegistry },
aggConfigsDependencies
);
const ac2 = new AggConfigs(
indexPattern,
test.config2,
{ typesRegistry },
aggConfigsDependencies
);
expect(ac1.jsonDataEquals(ac2.aggs)).toBe(false);
});
});
});

describe('#toJSON', () => {
it('includes the aggs id, params, type and schema', () => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const ac = new AggConfigs(indexPattern, [], { typesRegistry }, aggConfigsDependencies);
const configStates = {
enabled: true,
type: 'date_histogram',
Expand Down Expand Up @@ -356,8 +395,18 @@ describe('AggConfig', () => {
params: {},
},
];
const ac1 = new AggConfigs(indexPattern, configStates, { typesRegistry });
const ac2 = new AggConfigs(indexPattern, configStates, { typesRegistry });
const ac1 = new AggConfigs(
indexPattern,
configStates,
{ typesRegistry },
aggConfigsDependencies
);
const ac2 = new AggConfigs(
indexPattern,
configStates,
{ typesRegistry },
aggConfigsDependencies
);

// this relies on the assumption that js-engines consistently loop over properties in insertion order.
// most likely the case, but strictly speaking not guaranteed by the JS and JSON specifications.
Expand All @@ -369,7 +418,7 @@ describe('AggConfig', () => {
let aggConfig: AggConfig;

beforeEach(() => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const ac = new AggConfigs(indexPattern, [], { typesRegistry }, aggConfigsDependencies);
aggConfig = ac.createAggConfig({ type: 'count' } as CreateAggConfigParams);
});

Expand Down Expand Up @@ -398,7 +447,7 @@ describe('AggConfig', () => {

describe('#fieldFormatter - custom getFormat handler', () => {
it('returns formatter from getFormat handler', () => {
const ac = new AggConfigs(indexPattern, [], { typesRegistry });
const ac = new AggConfigs(indexPattern, [], { typesRegistry }, aggConfigsDependencies);
const configStates = {
enabled: true,
type: 'count',
Expand Down Expand Up @@ -439,7 +488,12 @@ describe('AggConfig', () => {
},
},
};
const ac = new AggConfigs(indexPattern, [configStates], { typesRegistry });
const ac = new AggConfigs(
indexPattern,
[configStates],
{ typesRegistry },
aggConfigsDependencies
);
aggConfig = ac.createAggConfig(configStates);
});

Expand Down
9 changes: 1 addition & 8 deletions src/plugins/data/public/search/aggs/agg_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { IAggConfigs } from './agg_configs';
import { FetchOptions } from '../fetch';
import { ISearchSource } from '../search_source';
import { FieldFormatsContentType, KBN_FIELD_TYPES } from '../../../common';
import { getFieldFormats } from '../../../public/services';
import { FieldFormatsStart } from '../../field_formats';

export interface AggConfigOptions {
Expand All @@ -40,12 +39,6 @@ export interface AggConfigDependencies {
fieldFormats: FieldFormatsStart;
}

const tempAggConfigDependencies = () => {
return {
fieldFormats: getFieldFormats(),
};
};

/**
* @name AggConfig
*
Expand Down Expand Up @@ -109,7 +102,7 @@ export class AggConfig {
constructor(
aggConfigs: IAggConfigs,
opts: AggConfigOptions,
{ fieldFormats }: AggConfigDependencies = tempAggConfigDependencies()
{ fieldFormats }: AggConfigDependencies
) {
this.aggConfigs = aggConfigs;
this.id = String(opts.id || AggConfig.nextId(aggConfigs.aggs as any));
Expand Down
Loading

0 comments on commit 497dfa2

Please sign in to comment.