-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
VisualizeModal_spec.jsx
361 lines (331 loc) · 11.4 KB
/
VisualizeModal_spec.jsx
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import React from 'react';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { Modal } from 'react-bootstrap';
import { shallow } from 'enzyme';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';
import $ from 'jquery';
import shortid from 'shortid';
import { queries } from './fixtures';
import { sqlLabReducer } from '../../../javascripts/SqlLab/reducers';
import * as actions from '../../../javascripts/SqlLab/actions';
import { VISUALIZE_VALIDATION_ERRORS } from '../../../javascripts/SqlLab/constants';
import VisualizeModal from '../../../javascripts/SqlLab/components/VisualizeModal';
import * as exploreUtils from '../../../javascripts/explore/exploreUtils';
global.notify = {
info: () => {},
error: () => {},
};
describe('VisualizeModal', () => {
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
const initialState = sqlLabReducer({}, {});
initialState.common = {
conf: { SUPERSET_WEBSERVER_TIMEOUT: 45 },
};
const store = mockStore(initialState);
const mockedProps = {
show: true,
query: queries[0],
};
const mockColumns = {
ds: {
is_date: true,
is_dim: false,
name: 'ds',
type: 'STRING',
},
gender: {
is_date: false,
is_dim: true,
name: 'gender',
type: 'STRING',
},
};
const mockChartTypeBarChart = {
label: 'Distribution - Bar Chart',
requiresTime: false,
value: 'dist_bar',
};
const mockChartTypeTB = {
label: 'Time Series - Bar Chart',
requiresTime: true,
value: 'bar',
};
const mockEvent = {
target: {
value: 'mock event value',
},
};
const getVisualizeModalWrapper = () => (
shallow(<VisualizeModal {...mockedProps} />, {
context: { store },
}).dive());
it('renders', () => {
expect(React.isValidElement(<VisualizeModal />)).to.equal(true);
});
it('renders with props', () => {
expect(
React.isValidElement(<VisualizeModal {...mockedProps} />),
).to.equal(true);
});
it('renders a Modal', () => {
const wrapper = getVisualizeModalWrapper();
expect(wrapper.find(Modal)).to.have.length(1);
});
describe('getColumnFromProps', () => {
it('should require valid query parameter in props', () => {
const emptyQuery = {
show: true,
query: {},
};
const wrapper = shallow(<VisualizeModal {...emptyQuery} />, {
context: { store },
}).dive();
expect(wrapper.state().columns).to.deep.equal({});
});
it('should set columns state', () => {
const wrapper = getVisualizeModalWrapper();
expect(wrapper.state().columns).to.deep.equal(mockColumns);
});
it('should not change columns state when closing Modal', () => {
const wrapper = getVisualizeModalWrapper();
expect(wrapper.state().columns).to.deep.equal(mockColumns);
// first change columns state
const newColumns = {
ds: {
is_date: true,
is_dim: false,
name: 'ds',
type: 'STRING',
},
name: {
is_date: false,
is_dim: true,
name: 'name',
type: 'STRING',
},
};
wrapper.instance().setState({ columns: newColumns });
// then close Modal
wrapper.setProps({ show: false });
expect(wrapper.state().columns).to.deep.equal(newColumns);
});
});
describe('datasourceName', () => {
const wrapper = getVisualizeModalWrapper();
let stub;
beforeEach(() => {
stub = sinon.stub(shortid, 'generate').returns('abcd');
});
afterEach(() => {
stub.restore();
});
it('should generate data source name from query', () => {
const sampleQuery = queries[0];
const name = wrapper.instance().datasourceName();
expect(name).to.equal(`${sampleQuery.user}-${sampleQuery.db}-${sampleQuery.tab}-abcd`);
});
it('should generate data source name with empty query', () => {
wrapper.setProps({ query: {} });
const name = wrapper.instance().datasourceName();
expect(name).to.equal('undefined-abcd');
});
});
describe('mergedColumns', () => {
const wrapper = getVisualizeModalWrapper();
const oldColumns = {
ds: 1,
gender: 2,
};
it('should merge by column name', () => {
wrapper.setState({ columns: {} });
const mc = wrapper.instance().mergedColumns();
expect(mc).to.deep.equal(mockColumns);
});
it('should not override current state', () => {
wrapper.setState({ columns: oldColumns });
const mc = wrapper.instance().mergedColumns();
expect(mc.ds).to.equal(oldColumns.ds);
expect(mc.gender).to.equal(oldColumns.gender);
});
});
describe('validate', () => {
const wrapper = getVisualizeModalWrapper();
let columnsStub;
beforeEach(() => {
columnsStub = sinon.stub(wrapper.instance(), 'mergedColumns');
});
afterEach(() => {
columnsStub.restore();
});
it('should validate column name', () => {
columnsStub.returns(mockColumns);
wrapper.instance().validate();
expect(wrapper.state().hints).to.have.length(0);
wrapper.instance().mergedColumns.restore();
});
it('should hint invalid column name', () => {
columnsStub.returns({
'&': 1,
});
wrapper.instance().validate();
expect(wrapper.state().hints).to.have.length(1);
wrapper.instance().mergedColumns.restore();
});
it('should hint empty chartType', () => {
columnsStub.returns(mockColumns);
wrapper.setState({ chartType: null });
wrapper.instance().validate();
expect(wrapper.state().hints).to.have.length(1);
expect(wrapper.state().hints[0])
.to.have.string(VISUALIZE_VALIDATION_ERRORS.REQUIRE_CHART_TYPE);
});
it('should check time series', () => {
columnsStub.returns(mockColumns);
wrapper.setState({ chartType: mockChartTypeTB });
wrapper.instance().validate();
expect(wrapper.state().hints).to.have.length(0);
// no is_date columns
columnsStub.returns({
ds: {
is_date: false,
is_dim: false,
name: 'ds',
type: 'STRING',
},
gender: {
is_date: false,
is_dim: true,
name: 'gender',
type: 'STRING',
},
});
wrapper.setState({ chartType: mockChartTypeTB });
wrapper.instance().validate();
expect(wrapper.state().hints).to.have.length(1);
expect(wrapper.state().hints[0]).to.have.string(VISUALIZE_VALIDATION_ERRORS.REQUIRE_TIME);
});
it('should validate after change checkbox', () => {
const spy = sinon.spy(wrapper.instance(), 'validate');
columnsStub.returns(mockColumns);
wrapper.instance().changeCheckbox('is_dim', 'gender', mockEvent);
expect(spy.callCount).to.equal(1);
spy.restore();
});
it('should validate after change Agg function', () => {
const spy = sinon.spy(wrapper.instance(), 'validate');
columnsStub.returns(mockColumns);
wrapper.instance().changeAggFunction('num', { label: 'MIN(x)', value: 'min' });
expect(spy.callCount).to.equal(1);
spy.restore();
});
});
it('should validate after change chart type', () => {
const wrapper = getVisualizeModalWrapper();
wrapper.setState({ chartType: mockChartTypeTB });
const spy = sinon.spy(wrapper.instance(), 'validate');
wrapper.instance().changeChartType(mockChartTypeBarChart);
expect(spy.callCount).to.equal(1);
expect(wrapper.state().chartType).to.equal(mockChartTypeBarChart);
});
it('should validate after change datasource name', () => {
const wrapper = getVisualizeModalWrapper();
const spy = sinon.spy(wrapper.instance(), 'validate');
wrapper.instance().changeDatasourceName(mockEvent);
expect(spy.callCount).to.equal(1);
expect(wrapper.state().datasourceName).to.equal(mockEvent.target.value);
});
it('should build viz options', () => {
const wrapper = getVisualizeModalWrapper();
wrapper.setState({ chartType: mockChartTypeTB });
const spy = sinon.spy(wrapper.instance(), 'buildVizOptions');
wrapper.instance().buildVizOptions();
expect(spy.returnValues[0]).to.deep.equal({
chartType: wrapper.state().chartType.value,
datasourceName: wrapper.state().datasourceName,
columns: wrapper.state().columns,
sql: wrapper.instance().props.query.sql,
dbId: wrapper.instance().props.query.dbId,
});
});
it('should build visualize advise for long query', () => {
const longQuery = Object.assign({}, queries[0], { endDttm: 1476910666798 });
const props = {
show: true,
query: longQuery,
};
const longQueryWrapper = shallow(<VisualizeModal {...props} />, {
context: { store },
}).dive();
const alertWrapper = shallow(longQueryWrapper.instance().buildVisualizeAdvise());
expect(alertWrapper.hasClass('alert')).to.equal(true);
expect(alertWrapper.text()).to.contain(
'This query took 101 seconds to run, and the explore view times out at 45 seconds');
});
it('should not build visualize advise', () => {
const wrapper = getVisualizeModalWrapper();
expect(wrapper.instance().buildVisualizeAdvise()).to.be.a('undefined');
});
describe('visualize', () => {
const wrapper = getVisualizeModalWrapper();
const mockOptions = { attr: 'mockOptions' };
wrapper.setState({
chartType: mockChartTypeBarChart,
columns: mockColumns,
datasourceName: 'mockDatasourceName',
});
let ajaxSpy;
let datasourceSpy;
beforeEach(() => {
ajaxSpy = sinon.spy($, 'ajax');
sinon.stub(JSON, 'parse').callsFake(() => ({ table_id: 107 }));
sinon.stub(exploreUtils, 'getExploreUrl').callsFake(() => ('mockURL'));
sinon.stub(wrapper.instance(), 'buildVizOptions').callsFake(() => (mockOptions));
sinon.spy(window, 'open');
datasourceSpy = sinon.stub(actions, 'createDatasource');
});
afterEach(() => {
ajaxSpy.restore();
JSON.parse.restore();
exploreUtils.getExploreUrl.restore();
wrapper.instance().buildVizOptions.restore();
window.open.restore();
datasourceSpy.restore();
});
it('should build request', () => {
wrapper.instance().visualize();
expect(ajaxSpy.callCount).to.equal(1);
const spyCall = ajaxSpy.getCall(0);
expect(spyCall.args[0].type).to.equal('POST');
expect(spyCall.args[0].url).to.equal('/superset/sqllab_viz/');
expect(spyCall.args[0].data.data).to.equal(JSON.stringify(mockOptions));
});
it('should open new window', () => {
datasourceSpy.callsFake(() => {
const d = $.Deferred();
d.resolve('done');
return d.promise();
});
wrapper.setProps({ actions: { createDatasource: datasourceSpy } });
wrapper.instance().visualize();
expect(exploreUtils.getExploreUrl.callCount).to.equal(1);
expect(exploreUtils.getExploreUrl.getCall(0).args[0].datasource).to.equal('107__table');
expect(window.open.callCount).to.equal(1);
});
it('should notify error', () => {
datasourceSpy.callsFake(() => {
const d = $.Deferred();
d.reject('error message');
return d.promise();
});
wrapper.setProps({ actions: { createDatasource: datasourceSpy } });
sinon.spy(notify, 'error');
wrapper.instance().visualize();
expect(window.open.callCount).to.equal(0);
expect(notify.error.callCount).to.equal(1);
});
});
});