Skip to content

Commit

Permalink
tabify.test.js -> tabify.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Feb 18, 2020
1 parent e4bb4eb commit 15a31e9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

import _ from 'lodash';
import { AggConfig, AggConfigOptions } from './agg_config';
import { AggConfig, AggConfigOptions, IAggConfig } from './agg_config';
import { Schema } from './schemas';
import { AggGroupNames } from './agg_groups';
import {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class AggConfigs {
public schemas: any;
public timeRange?: TimeRange;

aggs: AggConfig[];
aggs: IAggConfig[];

constructor(indexPattern: IndexPattern, configStates = [] as any, schemas?: any) {
configStates = AggConfig.ensureIds(configStates);
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/core_plugins/data/public/search/aggs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

export { aggTypes } from './agg_types';
export { AggType } from './agg_type';
export { AggConfig } from './agg_config';
export { AggConfigs } from './agg_configs';
export { AggConfig, IAggConfig } from './agg_config';
export { AggConfigs, IAggConfigs } from './agg_configs';
export { FieldParamType } from './param_types';
export { MetricAggType } from './metrics/metric_agg_type';
export { AggTypeFilters } from './filter';
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/core_plugins/data/public/search/tabify/buckets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import { get, isPlainObject, keys, findKey } from 'lodash';
import moment from 'moment';
import { AggConfig } from '../aggs/agg_config';
import { IAggConfig } from '../aggs';
import { TimeRange } from './types';
import { AggResponseBucket } from '../types';

type AggParams = AggConfig['params'] & {
type AggParams = IAggConfig['params'] & {
drop_partials: boolean;
ranges: TimeRange[];
};
Expand All @@ -35,7 +35,7 @@ export class TabifyBuckets {
_keys: any[] = [];

constructor(aggResp: any, aggParams?: AggParams, timeRange?: TimeRange) {
if (aggResp.buckets) {
if (aggResp && aggResp.buckets) {
this.buckets = aggResp.buckets;
} else if (aggResp) {
// Some Bucket Aggs only return a single bucket (like filter).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { groupBy } from 'lodash';
import { IAggConfig } from '../aggs/agg_config';
import { IAggConfig } from '../aggs';

export interface AggColumn {
aggConfig: IAggConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
*/

import { IAggConfigs } from '../aggs/agg_configs';
import { AggColumn } from './get_columns';
import { TabbedResponseWriterOptions } from './types';

export function tabifyAggResponse(
aggs: IAggConfigs,
esResponse: unknown,
respOpts?: Partial<TabbedResponseWriterOptions>
): Record<string, string>;
): {
columns: AggColumn[];
rows: Array<Record<string, any>>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@
* under the License.
*/

import fixtures from 'fixtures/fake_hierarchical_data';
import { cloneDeep } from 'lodash';
import { IndexPattern } from '../../../../../../plugins/data/public';
import { tabifyAggResponse } from './tabify';
import { AggConfigs, AggGroupNames, Schemas } from '../aggs';
import { IAggConfig, IAggConfigs, AggGroupNames, Schemas, AggConfigs } from '../aggs';

// @ts-ignore
import fixtures from '../../../../../../fixtures/fake_hierarchical_data';

jest.mock('ui/new_platform');

describe('tabifyAggResponse Integration', () => {
const createAggConfigs = (aggs = []) => {
const createAggConfigs = (aggs: IAggConfig[] = []) => {
const field = {
name: '@timestamp',
};

const indexPattern = {
const indexPattern = ({
id: '1234',
title: 'logstash-*',
fields: {
getByName: () => field,
filter: () => [field],
},
};
} as unknown) as IndexPattern;

return new AggConfigs(
indexPattern,
Expand All @@ -53,6 +55,8 @@ describe('tabifyAggResponse Integration', () => {
);
};

const mockAggConfig = (agg: any): IAggConfig => (agg as unknown) as IAggConfig;

test('transforms a simple response properly', () => {
const aggConfigs = createAggConfigs();

Expand All @@ -71,69 +75,71 @@ describe('tabifyAggResponse Integration', () => {
});

describe('transforms a complex response', () => {
let avg;
let ext;
let src;
let os;
let esResp;
let aggConfigs;
let avg: IAggConfig;
let ext: IAggConfig;
let src: IAggConfig;
let os: IAggConfig;
let esResp: typeof fixtures.threeTermBuckets;
let aggConfigs: IAggConfigs;

beforeEach(() => {
aggConfigs = createAggConfigs([
{ type: 'avg', schema: 'metric', params: { field: '@timestamp' } },
{ type: 'terms', schema: 'split', params: { field: '@timestamp' } },
{ type: 'terms', schema: 'segment', params: { field: '@timestamp' } },
{ type: 'terms', schema: 'segment', params: { field: '@timestamp' } },
mockAggConfig({ type: 'avg', schema: 'metric', params: { field: '@timestamp' } }),
mockAggConfig({ type: 'terms', schema: 'split', params: { field: '@timestamp' } }),
mockAggConfig({ type: 'terms', schema: 'segment', params: { field: '@timestamp' } }),
mockAggConfig({ type: 'terms', schema: 'segment', params: { field: '@timestamp' } }),
]);

avg = aggConfigs.aggs[0];
ext = aggConfigs.aggs[1];
src = aggConfigs.aggs[2];
os = aggConfigs.aggs[3];

esResp = cloneDeep(fixtures.threeTermBuckets);
// remove the buckets for css in MX
esResp = fixtures.threeTermBuckets;
esResp.aggregations.agg_2.buckets[1].agg_3.buckets[0].agg_4.buckets = [];
});

// check that the columns of a table are formed properly
function expectColumns(table, aggs) {
function expectColumns(table: ReturnType<typeof tabifyAggResponse>, aggs: IAggConfig[]) {
expect(table.columns).toHaveLength(aggs.length);

aggs.forEach(function(agg, i) {
aggs.forEach((agg, i) => {
expect(table.columns[i]).toHaveProperty('aggConfig', agg);
});
}

// check that a row has expected values
function expectRow(row, asserts) {
function expectRow(
row: Record<string, string>,
asserts: Array<(val: string | number) => void>
) {
expect(typeof row).toBe('object');

asserts.forEach(function(assert, i) {
asserts.forEach((assert, i: number) => {
if (row[`col-${i}`]) {
assert(row[`col-${i}`]);
}
});
}

// check for two character country code
function expectCountry(val) {
function expectCountry(val: string | number) {
expect(typeof val).toBe('string');
expect(val).toHaveLength(2);
}

// check for an OS term
function expectExtension(val) {
function expectExtension(val: string | number) {
expect(val).toMatch(/^(js|png|html|css|jpg)$/);
}

// check for an OS term
function expectOS(val) {
function expectOS(val: string | number) {
expect(val).toMatch(/^(win|mac|linux)$/);
}

// check for something like an average bytes result
function expectAvgBytes(val) {
function expectAvgBytes(val: string | number) {
expect(typeof val).toBe('number');
expect(val === 0 || val > 1000).toBeDefined();
}
Expand Down

0 comments on commit 15a31e9

Please sign in to comment.