-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathvis.test.ts
More file actions
106 lines (92 loc) · 3.29 KB
/
vis.test.ts
File metadata and controls
106 lines (92 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { Vis } from './vis';
import type { VisTypeDefinition } from './vis_types';
import { BaseVisType } from './vis_types';
jest.mock('./services', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { SearchSource } = require('@kbn/data-plugin/common/search/search_source');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const stubIndexPattern = require('@kbn/data-plugin/common/stubs');
return {
getAggs: () => ({
createAggConfigs: (indexPattern: any, cfg: any) => ({
aggs: cfg.map((aggConfig: any) => ({ ...aggConfig, serialize: () => aggConfig })),
}),
}),
getSearch: () => ({
searchSource: {
create: () => {
return new SearchSource({ index: stubIndexPattern.stubDataView });
},
},
}),
};
});
describe('Vis Class', function () {
let vis: Vis;
const stateFixture = {
type: 'pie',
title: 'pie',
data: {
aggs: [
{ type: 'avg' as any, schema: 'metric', params: { field: 'bytes' } },
{ type: 'terms' as any, schema: 'segment', params: { field: 'machine.os' } },
{ type: 'terms' as any, schema: 'segment', params: { field: 'geo.src' } },
],
searchSource: {
index: '123',
},
},
params: { isDonut: true },
};
beforeEach(async function () {
vis = new Vis(
new BaseVisType({
name: 'pie',
title: 'pie',
icon: 'pie-icon',
} as unknown as VisTypeDefinition<object>),
stateFixture as any
);
await vis.setState(stateFixture as any);
});
const verifyVis = function (visToVerify: Vis) {
expect(visToVerify).toHaveProperty('data');
expect(visToVerify.data).toHaveProperty('aggs');
expect(visToVerify.data.aggs!.aggs).toHaveLength(3);
expect(visToVerify).toHaveProperty('type');
expect(visToVerify).toHaveProperty('params');
expect(visToVerify.params).toHaveProperty('isDonut', true);
};
describe('initialization', function () {
it('should set the state', function () {
verifyVis(vis);
});
});
describe('getState()', function () {
it('should get a state that represents the... er... state', function () {
const state = vis.serialize();
expect(state).toHaveProperty('type', 'pie');
expect(state).toHaveProperty('params');
expect(state.params).toHaveProperty('isDonut', true);
expect(state.data).toHaveProperty('aggs');
expect(state.data.aggs).toHaveLength(3);
});
});
describe('isHierarchical()', function () {
it('should return false for non-hierarchical vis (like histogram)', function () {
expect(vis.isHierarchical()).toBe(false);
});
it('should return true for hierarchical vis (like pie)', function () {
(vis.type as any).hierarchicalData = true;
expect(vis.isHierarchical()).toBe(true);
});
});
});