Skip to content

Commit 6a8fdaa

Browse files
authored
Migrated vislib tests to the NP (#70309)
* Migrated vislib tests to the NP * fixed tests * Fixed tests * Fixed test
1 parent 58cdbf0 commit 6a8fdaa

File tree

18 files changed

+743
-408
lines changed

18 files changed

+743
-408
lines changed

src/plugins/charts/public/services/colors/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ const colors = new ColorsService();
2424
colors.init(coreMock.createSetup().uiSettings);
2525

2626
export const colorsServiceMock: ColorsService = {
27-
createColorLookupFunction: jest.fn(colors.createColorLookupFunction),
27+
createColorLookupFunction: jest.fn(colors.createColorLookupFunction.bind(colors)),
2828
} as any;
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919

2020
import d3 from 'd3';
2121
import _ from 'lodash';
22-
import expect from '@kbn/expect';
22+
import {
23+
setHTMLElementClientSizes,
24+
setSVGElementGetBBox,
25+
setSVGElementGetComputedTextLength,
26+
} from '../../../../../test_utils/public';
2327

24-
import { ChartTitle } from '../../../../../../../plugins/vis_type_vislib/public/vislib/lib/chart_title';
25-
import { VisConfig } from '../../../../../../../plugins/vis_type_vislib/public/vislib/lib/vis_config';
26-
import { getMockUiState } from '../../../../../../../plugins/vis_type_vislib/public/fixtures/mocks';
28+
import { ChartTitle } from './chart_title';
29+
import { VisConfig } from './vis_config';
30+
import { getMockUiState } from '../../fixtures/mocks';
2731

2832
describe('Vislib ChartTitle Class Test Suite', function () {
2933
let mockUiState;
@@ -88,6 +92,16 @@ describe('Vislib ChartTitle Class Test Suite', function () {
8892
yAxisLabel: 'Count',
8993
};
9094

95+
let mockedHTMLElementClientSizes;
96+
let mockedSVGElementGetBBox;
97+
let mockedSVGElementGetComputedTextLength;
98+
99+
beforeAll(() => {
100+
mockedHTMLElementClientSizes = setHTMLElementClientSizes(512, 512);
101+
mockedSVGElementGetBBox = setSVGElementGetBBox(100);
102+
mockedSVGElementGetComputedTextLength = setSVGElementGetComputedTextLength(100);
103+
});
104+
91105
beforeEach(() => {
92106
mockUiState = getMockUiState();
93107
el = d3.select('body').append('div').attr('class', 'visWrapper').datum(data);
@@ -113,23 +127,29 @@ describe('Vislib ChartTitle Class Test Suite', function () {
113127
el.remove();
114128
});
115129

130+
afterAll(() => {
131+
mockedHTMLElementClientSizes.mockRestore();
132+
mockedSVGElementGetBBox.mockRestore();
133+
mockedSVGElementGetComputedTextLength.mockRestore();
134+
});
135+
116136
describe('render Method', function () {
117137
beforeEach(function () {
118138
chartTitle.render();
119139
});
120140

121-
it('should append an svg to div', function () {
122-
expect(el.select('.chart-title').selectAll('svg').length).to.be(1);
141+
test('should append an svg to div', function () {
142+
expect(el.select('.chart-title').selectAll('svg').length).toBe(1);
123143
});
124144

125-
it('should append text', function () {
126-
expect(!!el.select('.chart-title').selectAll('svg').selectAll('text')).to.be(true);
145+
test('should append text', function () {
146+
expect(!!el.select('.chart-title').selectAll('svg').selectAll('text')).toBe(true);
127147
});
128148
});
129149

130150
describe('draw Method', function () {
131-
it('should be a function', function () {
132-
expect(_.isFunction(chartTitle.draw())).to.be(true);
151+
test('should be a function', function () {
152+
expect(_.isFunction(chartTitle.draw())).toBe(true);
133153
});
134154
});
135155
});
Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@
1919

2020
import _ from 'lodash';
2121
import d3 from 'd3';
22-
import expect from '@kbn/expect';
22+
import {
23+
setHTMLElementClientSizes,
24+
setSVGElementGetBBox,
25+
setSVGElementGetComputedTextLength,
26+
} from '../../../../../test_utils/public';
2327

2428
// Data
25-
import data from '../../../../../../../plugins/vis_type_vislib/public/fixtures/mock_data/date_histogram/_series';
29+
import data from '../../fixtures/mock_data/date_histogram/_series';
2630

27-
import { getMockUiState } from '../../../../../../../plugins/vis_type_vislib/public/fixtures/mocks';
28-
import { getVis } from '../_vis_fixture';
31+
import { getMockUiState } from '../../fixtures/mocks';
32+
import { getVis } from '../visualizations/_vis_fixture';
33+
34+
let mockedHTMLElementClientSizes;
35+
let mockedSVGElementGetBBox;
36+
let mockedSVGElementGetComputedTextLength;
2937

3038
describe('Vislib Dispatch Class Test Suite', function () {
3139
function destroyVis(vis) {
@@ -36,6 +44,18 @@ describe('Vislib Dispatch Class Test Suite', function () {
3644
return d3.select(element).data(new Array(n)).enter().append(type);
3745
}
3846

47+
beforeAll(() => {
48+
mockedHTMLElementClientSizes = setHTMLElementClientSizes(512, 512);
49+
mockedSVGElementGetBBox = setSVGElementGetBBox(100);
50+
mockedSVGElementGetComputedTextLength = setSVGElementGetComputedTextLength(100);
51+
});
52+
53+
afterAll(() => {
54+
mockedHTMLElementClientSizes.mockRestore();
55+
mockedSVGElementGetBBox.mockRestore();
56+
mockedSVGElementGetComputedTextLength.mockRestore();
57+
});
58+
3959
describe('', function () {
4060
let vis;
4161
let mockUiState;
@@ -50,13 +70,13 @@ describe('Vislib Dispatch Class Test Suite', function () {
5070
destroyVis(vis);
5171
});
5272

53-
it('implements on, off, emit methods', function () {
73+
test('implements on, off, emit methods', function () {
5474
const events = _.map(vis.handler.charts, 'events');
55-
expect(events.length).to.be.above(0);
75+
expect(events.length).toBeGreaterThan(0);
5676
events.forEach(function (dispatch) {
57-
expect(dispatch).to.have.property('on');
58-
expect(dispatch).to.have.property('off');
59-
expect(dispatch).to.have.property('emit');
77+
expect(dispatch).toHaveProperty('on');
78+
expect(dispatch).toHaveProperty('off');
79+
expect(dispatch).toHaveProperty('emit');
6080
});
6181
});
6282
});
@@ -77,15 +97,15 @@ describe('Vislib Dispatch Class Test Suite', function () {
7797
});
7898

7999
describe('addEvent method', function () {
80-
it('returns a function that binds the passed event to a selection', function () {
100+
test('returns a function that binds the passed event to a selection', function () {
81101
const chart = _.first(vis.handler.charts);
82102
const apply = chart.events.addEvent('event', _.noop);
83-
expect(apply).to.be.a('function');
103+
expect(apply).toBeInstanceOf(Function);
84104

85105
const els = getEls(vis.element, 3, 'div');
86106
apply(els);
87107
els.each(function () {
88-
expect(d3.select(this).on('event')).to.be(_.noop);
108+
expect(d3.select(this).on('event')).toBe(_.noop);
89109
});
90110
});
91111
});
@@ -94,21 +114,21 @@ describe('Vislib Dispatch Class Test Suite', function () {
94114
// checking that they return function which bind the events expected
95115
function checkBoundAddMethod(name, event) {
96116
describe(name + ' method', function () {
97-
it('should be a function', function () {
117+
test('should be a function', function () {
98118
vis.handler.charts.forEach(function (chart) {
99-
expect(chart.events[name]).to.be.a('function');
119+
expect(chart.events[name]).toBeInstanceOf(Function);
100120
});
101121
});
102122

103-
it('returns a function that binds ' + event + ' events to a selection', function () {
123+
test('returns a function that binds ' + event + ' events to a selection', function () {
104124
const chart = _.first(vis.handler.charts);
105125
const apply = chart.events[name](chart.series[0].chartEl);
106-
expect(apply).to.be.a('function');
126+
expect(apply).toBeInstanceOf(Function);
107127

108128
const els = getEls(vis.element, 3, 'div');
109129
apply(els);
110130
els.each(function () {
111-
expect(d3.select(this).on(event)).to.be.a('function');
131+
expect(d3.select(this).on(event)).toBeInstanceOf(Function);
112132
});
113133
});
114134
});
@@ -119,83 +139,83 @@ describe('Vislib Dispatch Class Test Suite', function () {
119139
checkBoundAddMethod('addClickEvent', 'click');
120140

121141
describe('addMousePointer method', function () {
122-
it('should be a function', function () {
142+
test('should be a function', function () {
123143
vis.handler.charts.forEach(function (chart) {
124144
const pointer = chart.events.addMousePointer;
125145

126-
expect(_.isFunction(pointer)).to.be(true);
146+
expect(_.isFunction(pointer)).toBe(true);
127147
});
128148
});
129149
});
130150

131151
describe('clickEvent handler', () => {
132152
describe('for pie chart', () => {
133-
it('prepares data points', () => {
153+
test('prepares data points', () => {
134154
const expectedResponse = [{ column: 0, row: 0, table: {}, value: 0 }];
135155
const d = { rawData: { column: 0, row: 0, table: {}, value: 0 } };
136156
const chart = _.first(vis.handler.charts);
137157
const response = chart.events.clickEventResponse(d, { isSlices: true });
138-
expect(response.data).to.eql(expectedResponse);
158+
expect(response.data).toEqual(expectedResponse);
139159
});
140160

141-
it('remove invalid points', () => {
161+
test('remove invalid points', () => {
142162
const expectedResponse = [{ column: 0, row: 0, table: {}, value: 0 }];
143163
const d = {
144164
rawData: { column: 0, row: 0, table: {}, value: 0 },
145165
yRaw: { table: {}, value: 0 },
146166
};
147167
const chart = _.first(vis.handler.charts);
148168
const response = chart.events.clickEventResponse(d, { isSlices: true });
149-
expect(response.data).to.eql(expectedResponse);
169+
expect(response.data).toEqual(expectedResponse);
150170
});
151171
});
152172

153173
describe('for xy charts', () => {
154-
it('prepares data points', () => {
174+
test('prepares data points', () => {
155175
const expectedResponse = [{ column: 0, row: 0, table: {}, value: 0 }];
156176
const d = { xRaw: { column: 0, row: 0, table: {}, value: 0 } };
157177
const chart = _.first(vis.handler.charts);
158178
const response = chart.events.clickEventResponse(d, { isSlices: false });
159-
expect(response.data).to.eql(expectedResponse);
179+
expect(response.data).toEqual(expectedResponse);
160180
});
161181

162-
it('remove invalid points', () => {
182+
test('remove invalid points', () => {
163183
const expectedResponse = [{ column: 0, row: 0, table: {}, value: 0 }];
164184
const d = {
165185
xRaw: { column: 0, row: 0, table: {}, value: 0 },
166186
yRaw: { table: {}, value: 0 },
167187
};
168188
const chart = _.first(vis.handler.charts);
169189
const response = chart.events.clickEventResponse(d, { isSlices: false });
170-
expect(response.data).to.eql(expectedResponse);
190+
expect(response.data).toEqual(expectedResponse);
171191
});
172192
});
173193
});
174194
});
175195

176196
describe('Custom event handlers', function () {
177-
it('should attach whatever gets passed on vis.on() to chart.events', function (done) {
197+
test('should attach whatever gets passed on vis.on() to chart.events', function (done) {
178198
const vis = getVis();
179199
const mockUiState = getMockUiState();
180200
vis.on('someEvent', _.noop);
181201
vis.render(data, mockUiState);
182202

183203
vis.handler.charts.forEach(function (chart) {
184-
expect(chart.events.listenerCount('someEvent')).to.be(1);
204+
expect(chart.events.listenerCount('someEvent')).toBe(1);
185205
});
186206

187207
destroyVis(vis);
188208
done();
189209
});
190210

191-
it('can be added after rendering', function () {
211+
test('can be added after rendering', function () {
192212
const vis = getVis();
193213
const mockUiState = getMockUiState();
194214
vis.render(data, mockUiState);
195215
vis.on('someEvent', _.noop);
196216

197217
vis.handler.charts.forEach(function (chart) {
198-
expect(chart.events.listenerCount('someEvent')).to.be(1);
218+
expect(chart.events.listenerCount('someEvent')).toBe(1);
199219
});
200220

201221
destroyVis(vis);

0 commit comments

Comments
 (0)