forked from apache/superset
/
ResultSet_spec.jsx
109 lines (105 loc) · 3.79 KB
/
ResultSet_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
import React from 'react';
import { shallow } from 'enzyme';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';
import { Alert, ProgressBar } from 'react-bootstrap';
import FilterableTable from '../../../src/components/FilterableTable/FilterableTable';
import ExploreResultsButton from '../../../src/SqlLab/components/ExploreResultsButton';
import ResultSet from '../../../src/SqlLab/components/ResultSet';
import { queries, stoppedQuery, runningQuery, cachedQuery } from './fixtures';
describe('ResultSet', () => {
const clearQuerySpy = sinon.spy();
const fetchQuerySpy = sinon.spy();
const mockedProps = {
actions: {
clearQueryResults: clearQuerySpy,
fetchQueryResults: fetchQuerySpy,
},
cache: true,
query: queries[0],
height: 0,
};
const stoppedQueryProps = Object.assign({}, mockedProps, {
query: stoppedQuery,
});
const runningQueryProps = Object.assign({}, mockedProps, {
query: runningQuery,
});
const cachedQueryProps = Object.assign({}, mockedProps, {
query: cachedQuery,
});
const newProps = {
query: {
cached: false,
resultsKey: 'new key',
results: {
data: [{ a: 1 }],
},
},
};
it('is valid', () => {
expect(React.isValidElement(<ResultSet {...mockedProps} />)).to.equal(true);
});
it('renders a Table', () => {
const wrapper = shallow(<ResultSet {...mockedProps} />);
expect(wrapper.find(FilterableTable)).to.have.length(1);
});
describe('componentWillReceiveProps', () => {
const wrapper = shallow(<ResultSet {...mockedProps} />);
let spy;
beforeEach(() => {
clearQuerySpy.reset();
fetchQuerySpy.reset();
spy = sinon.spy(ResultSet.prototype, 'componentWillReceiveProps');
});
afterEach(() => {
spy.restore();
});
it('should update cached data', () => {
wrapper.setProps(newProps);
expect(wrapper.state().data).to.deep.equal(newProps.query.results.data);
expect(clearQuerySpy.callCount).to.equal(1);
expect(clearQuerySpy.getCall(0).args[0]).to.deep.equal(newProps.query);
expect(fetchQuerySpy.callCount).to.equal(1);
expect(fetchQuerySpy.getCall(0).args[0]).to.deep.equal(newProps.query);
});
});
describe('render', () => {
it('should render success query', () => {
const wrapper = shallow(<ResultSet {...mockedProps} />);
const filterableTable = wrapper.find(FilterableTable);
expect(filterableTable.props().data).to.equal(mockedProps.query.results.data);
expect(wrapper.find(ExploreResultsButton)).to.have.length(1);
});
it('should render empty results', () => {
const wrapper = shallow(<ResultSet {...mockedProps} />);
const emptyResults = Object.assign({}, queries[0], {
results: {
data: [],
},
});
wrapper.setProps({ query: emptyResults });
expect(wrapper.find(FilterableTable)).to.have.length(0);
expect(wrapper.find(Alert)).to.have.length(1);
expect(wrapper.find(Alert).shallow().text()).to.equal('The query returned no data');
});
it('should render cached query', () => {
const wrapper = shallow(<ResultSet {...cachedQueryProps} />);
const cachedData = [
{ col1: 'a', col2: 'b' },
];
wrapper.setState({ data: cachedData });
const filterableTable = wrapper.find(FilterableTable);
expect(filterableTable.props().data).to.equal(cachedData);
});
it('should render stopped query', () => {
const wrapper = shallow(<ResultSet {...stoppedQueryProps} />);
expect(wrapper.find(Alert)).to.have.length(1);
});
it('should render running/pending/fetching query', () => {
const wrapper = shallow(<ResultSet {...runningQueryProps} />);
expect(wrapper.find(ProgressBar)).to.have.length(1);
});
});
});