-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathuseBarChartDataSettingsWithCustomUnits.test.ts
More file actions
145 lines (132 loc) · 5.06 KB
/
useBarChartDataSettingsWithCustomUnits.test.ts
File metadata and controls
145 lines (132 loc) · 5.06 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import { renderHook } from 'wrappedTestingLibrary/hooks';
import { act } from '@testing-library/react-hooks';
import useFeature from 'hooks/useFeature';
import useWidgetUnits from 'views/components/visualizations/hooks/useWidgetUnits';
import { asMock } from 'helpers/mocking';
import useChartDataSettingsWithCustomUnits from 'views/components/visualizations/hooks/useChartDataSettingsWithCustomUnits';
import AggregationWidgetConfig from 'views/logic/aggregationbuilder/AggregationWidgetConfig';
import Series from 'views/logic/aggregationbuilder/Series';
import SeriesConfig from 'views/logic/aggregationbuilder/SeriesConfig';
import UnitsConfig from 'views/logic/aggregationbuilder/UnitsConfig';
import FieldUnit from 'views/logic/aggregationbuilder/FieldUnit';
import useBarChartDataSettingsWithCustomUnits from 'views/components/visualizations/hooks/useBarChartDataSettingsWithCustomUnits';
import getFieldNameFromTrace from 'views/components/visualizations/utils/getFieldNameFromTrace';
import * as chartLayoutGenerators from 'views/components/visualizations/utils/chartLayoutGenerators';
import type { MappersForYAxis } from 'views/components/visualizations/utils/chartLayoutGenerators';
jest.mock('views/components/visualizations/hooks/useChartDataSettingsWithCustomUnits');
jest.mock('hooks/useFeature');
jest.mock('views/components/visualizations/hooks/useWidgetUnits');
jest.mock('views/components/visualizations/utils/getFieldNameFromTrace');
jest.mock('views/components/visualizations/utils/chartLayoutGenerators');
const testConfig: AggregationWidgetConfig = AggregationWidgetConfig.builder()
.series([
Series.create('avg', 'fieldTime')
.toBuilder()
.config(SeriesConfig.empty().toBuilder().name('Name1').build())
.build(),
Series.create('avg', 'fieldSize')
.toBuilder()
.config(SeriesConfig.empty().toBuilder().name('Name2').build())
.build(),
Series.create('avg', 'fieldPercent')
.toBuilder()
.config(SeriesConfig.empty().toBuilder().name('Name3').build())
.build(),
Series.create('count'),
])
.build();
const units: UnitsConfig = UnitsConfig.empty()
.toBuilder()
.setFieldUnit('fieldTime', new FieldUnit('time', 'ms'))
.setFieldUnit('fieldSize', new FieldUnit('size', 'kb'))
.setFieldUnit('fieldPercent', new FieldUnit('percent', '%'))
.build();
describe('useBarChartDataSettingsWithCustomUnits', () => {
beforeEach(() => {
asMock(useChartDataSettingsWithCustomUnits).mockReturnValue(
jest.fn(() => ({
y: [1, 2, 3],
yaxis: 'y1',
})),
);
asMock(useFeature).mockReturnValue(true);
asMock(useWidgetUnits).mockReturnValue(units);
asMock(getFieldNameFromTrace).mockReturnValue('fieldTime');
const mappers: MappersForYAxis = {
fieldNameToAxisCountMapper: {
fieldTime: 1,
fieldSize: 2,
fieldPercent: 3,
no_field_name_series: 4,
},
unitTypeMapper: {
time: {
axisCount: 1,
axisKeyName: 'yaxis',
},
size: {
axisCount: 2,
axisKeyName: 'yaxis2',
},
percent: {
axisCount: 3,
axisKeyName: 'yaxis3',
},
withoutUnit: {
axisCount: 4,
axisKeyName: 'yaxis4',
},
},
yAxisMapper: {},
mapperAxisNumber: {},
fieldNameToAxisNameMapper: {},
seriesUnitMapper: {},
};
asMock(chartLayoutGenerators.generateMappersForYAxis).mockReturnValue(mappers);
asMock(chartLayoutGenerators.getBarChartTraceOffsetGroup).mockReturnValue(1);
});
it('Runs all related functions and return combined result from them', async () => {
const { result } = renderHook(() =>
useBarChartDataSettingsWithCustomUnits({
config: testConfig,
barmode: 'group',
}),
);
let barChartDataSettingsWithCustomUnits;
act(() => {
barChartDataSettingsWithCustomUnits = result.current({
originalName: 'Name1',
name: 'Name1',
fullPath: 'Name1',
values: [1000, 2000, 3000],
idx: 1,
total: 4,
xAxisItemsLength: 10,
});
});
expect(useChartDataSettingsWithCustomUnits).toHaveBeenCalledWith({ config: testConfig });
expect(chartLayoutGenerators.getBarChartTraceOffsetGroup).toHaveBeenCalledWith('group', 'y1', 1);
expect(barChartDataSettingsWithCustomUnits).toEqual({
fullPath: 'Name1',
offsetgroup: 1,
y: [1, 2, 3],
yaxis: 'y1',
});
});
});