2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2016-10-25 19:44:32 -04:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
2017-06-15 16:51:09 -04:00
|
|
|
import sinon from 'sinon';
|
|
|
|
|
2018-08-02 13:52:38 -04:00
|
|
|
import { Alert, ProgressBar } from 'react-bootstrap';
|
2018-04-18 00:05:01 -04:00
|
|
|
import FilterableTable from '../../../src/components/FilterableTable/FilterableTable';
|
2018-08-02 13:52:38 -04:00
|
|
|
import ExploreResultsButton from '../../../src/SqlLab/components/ExploreResultsButton';
|
2018-04-18 00:05:01 -04:00
|
|
|
import ResultSet from '../../../src/SqlLab/components/ResultSet';
|
2017-06-15 16:51:09 -04:00
|
|
|
import { queries, stoppedQuery, runningQuery, cachedQuery } from './fixtures';
|
2017-04-13 18:04:09 -04:00
|
|
|
|
2016-10-25 19:44:32 -04:00
|
|
|
describe('ResultSet', () => {
|
2017-06-15 16:51:09 -04:00
|
|
|
const clearQuerySpy = sinon.spy();
|
|
|
|
const fetchQuerySpy = sinon.spy();
|
2016-10-25 19:44:32 -04:00
|
|
|
const mockedProps = {
|
2017-06-15 16:51:09 -04:00
|
|
|
actions: {
|
|
|
|
clearQueryResults: clearQuerySpy,
|
|
|
|
fetchQueryResults: fetchQuerySpy,
|
|
|
|
},
|
|
|
|
cache: true,
|
2016-10-25 19:44:32 -04:00
|
|
|
query: queries[0],
|
2017-04-30 13:58:09 -04:00
|
|
|
height: 0,
|
2016-10-25 19:44:32 -04:00
|
|
|
};
|
2017-06-15 16:51:09 -04:00
|
|
|
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 }],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-04-30 13:58:09 -04:00
|
|
|
it('is valid', () => {
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(React.isValidElement(<ResultSet {...mockedProps} />)).toBe(true);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
|
|
|
it('renders a Table', () => {
|
|
|
|
const wrapper = shallow(<ResultSet {...mockedProps} />);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.find(FilterableTable)).toHaveLength(1);
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|
2017-06-15 16:51:09 -04:00
|
|
|
describe('componentWillReceiveProps', () => {
|
|
|
|
const wrapper = shallow(<ResultSet {...mockedProps} />);
|
|
|
|
let spy;
|
|
|
|
beforeEach(() => {
|
|
|
|
clearQuerySpy.reset();
|
|
|
|
fetchQuerySpy.reset();
|
2019-09-03 17:13:58 -04:00
|
|
|
spy = sinon.spy(ResultSet.prototype, 'UNSAFE_componentWillReceiveProps');
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
it('should update cached data', () => {
|
|
|
|
wrapper.setProps(newProps);
|
|
|
|
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.state().data).toEqual(newProps.query.results.data);
|
|
|
|
expect(clearQuerySpy.callCount).toBe(1);
|
|
|
|
expect(clearQuerySpy.getCall(0).args[0]).toEqual(newProps.query);
|
|
|
|
expect(fetchQuerySpy.callCount).toBe(1);
|
|
|
|
expect(fetchQuerySpy.getCall(0).args[0]).toEqual(newProps.query);
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('render', () => {
|
|
|
|
it('should render success query', () => {
|
|
|
|
const wrapper = shallow(<ResultSet {...mockedProps} />);
|
|
|
|
const filterableTable = wrapper.find(FilterableTable);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(filterableTable.props().data).toBe(mockedProps.query.results.data);
|
|
|
|
expect(wrapper.find(ExploreResultsButton)).toHaveLength(1);
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
it('should render empty results', () => {
|
|
|
|
const wrapper = shallow(<ResultSet {...mockedProps} />);
|
|
|
|
const emptyResults = Object.assign({}, queries[0], {
|
|
|
|
results: {
|
|
|
|
data: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
wrapper.setProps({ query: emptyResults });
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.find(FilterableTable)).toHaveLength(0);
|
|
|
|
expect(wrapper.find(Alert)).toHaveLength(1);
|
|
|
|
expect(wrapper.find(Alert).shallow().text()).toBe('The query returned no data');
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
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);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(filterableTable.props().data).toBe(cachedData);
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
it('should render stopped query', () => {
|
|
|
|
const wrapper = shallow(<ResultSet {...stoppedQueryProps} />);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.find(Alert)).toHaveLength(1);
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
it('should render running/pending/fetching query', () => {
|
|
|
|
const wrapper = shallow(<ResultSet {...runningQueryProps} />);
|
2018-10-15 16:10:18 -04:00
|
|
|
expect(wrapper.find(ProgressBar)).toHaveLength(1);
|
2017-06-15 16:51:09 -04:00
|
|
|
});
|
|
|
|
});
|
2016-10-25 19:44:32 -04:00
|
|
|
});
|