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.
|
|
|
|
*/
|
2018-10-15 19:52:19 -04:00
|
|
|
import fetchMock from 'fetch-mock';
|
2017-08-10 17:21:45 -04:00
|
|
|
import sinon from 'sinon';
|
2018-10-15 19:52:19 -04:00
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
import * as chartlib from '@superset-ui/chart';
|
2020-05-07 17:18:10 -04:00
|
|
|
import { LOG_EVENT } from 'src/logger/actions';
|
|
|
|
import * as exploreUtils from 'src/explore/exploreUtils';
|
|
|
|
import * as actions from 'src/chart/chartAction';
|
2017-08-10 17:21:45 -04:00
|
|
|
|
|
|
|
describe('chart actions', () => {
|
2020-05-07 04:09:28 -04:00
|
|
|
const V1_URL = '/http//localhost/api/v1/chart/data';
|
2018-10-15 19:52:19 -04:00
|
|
|
const MOCK_URL = '/mockURL';
|
2017-08-10 17:21:45 -04:00
|
|
|
let dispatch;
|
|
|
|
let urlStub;
|
2020-05-07 04:09:28 -04:00
|
|
|
let metadataRegistryStub;
|
|
|
|
let buildQueryRegistryStub;
|
|
|
|
let fakeMetadata;
|
2018-10-15 19:52:19 -04:00
|
|
|
|
|
|
|
const setupDefaultFetchMock = () => {
|
|
|
|
fetchMock.post(MOCK_URL, { json: {} }, { overwriteRoutes: true });
|
2020-05-07 04:09:28 -04:00
|
|
|
fetchMock.post(V1_URL, { json: {} }, { overwriteRoutes: true });
|
2018-10-15 19:52:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
setupDefaultFetchMock();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(fetchMock.restore);
|
2017-08-10 17:21:45 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
dispatch = sinon.spy();
|
2018-10-15 19:52:19 -04:00
|
|
|
urlStub = sinon
|
2020-05-18 12:19:05 -04:00
|
|
|
.stub(exploreUtils, 'getExploreUrl')
|
|
|
|
.callsFake(() => MOCK_URL);
|
2020-05-07 04:09:28 -04:00
|
|
|
fakeMetadata = { useLegacyApi: true };
|
|
|
|
metadataRegistryStub = sinon
|
|
|
|
.stub(chartlib, 'getChartMetadataRegistry')
|
|
|
|
.callsFake(() => ({ get: () => fakeMetadata }));
|
|
|
|
buildQueryRegistryStub = sinon
|
|
|
|
.stub(chartlib, 'getChartBuildQueryRegistry')
|
|
|
|
.callsFake(() => ({ get: () => () => ({ some_param: 'fake query!' }) }));
|
2017-08-10 17:21:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
urlStub.restore();
|
2018-10-15 19:52:19 -04:00
|
|
|
fetchMock.resetHistory();
|
2020-05-07 04:09:28 -04:00
|
|
|
metadataRegistryStub.restore();
|
|
|
|
buildQueryRegistryStub.restore();
|
2018-10-15 19:52:19 -04:00
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
describe('v1 API', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
fakeMetadata = { useLegacyApi: false };
|
2018-10-15 19:52:19 -04:00
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should query with the built query', async () => {
|
|
|
|
const actionThunk = actions.postChartFormData({});
|
|
|
|
await actionThunk(dispatch);
|
2018-10-15 19:52:19 -04:00
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
expect(fetchMock.calls(V1_URL)).toHaveLength(1);
|
|
|
|
expect(fetchMock.calls(V1_URL)[0][1].body).toBe(
|
|
|
|
JSON.stringify({ some_param: 'fake query!' }),
|
|
|
|
);
|
|
|
|
expect(dispatch.args[0][0].type).toBe(actions.CHART_UPDATE_STARTED);
|
2018-10-15 19:52:19 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
describe('legacy API', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
fakeMetadata = { useLegacyApi: true };
|
2018-10-15 19:52:19 -04:00
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should dispatch CHART_UPDATE_STARTED action before the query', () => {
|
|
|
|
const actionThunk = actions.postChartFormData({});
|
2019-02-16 02:10:05 -05:00
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, success
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
expect(fetchMock.calls(MOCK_URL)).toHaveLength(1);
|
|
|
|
expect(dispatch.args[0][0].type).toBe(actions.CHART_UPDATE_STARTED);
|
|
|
|
});
|
2019-02-16 02:10:05 -05:00
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should dispatch TRIGGER_QUERY action with the query', () => {
|
|
|
|
const actionThunk = actions.postChartFormData({});
|
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, success
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
expect(fetchMock.calls(MOCK_URL)).toHaveLength(1);
|
|
|
|
expect(dispatch.args[1][0].type).toBe(actions.TRIGGER_QUERY);
|
|
|
|
});
|
2018-10-15 19:52:19 -04:00
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should dispatch UPDATE_QUERY_FORM_DATA action with the query', () => {
|
|
|
|
const actionThunk = actions.postChartFormData({});
|
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, success
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
expect(fetchMock.calls(MOCK_URL)).toHaveLength(1);
|
|
|
|
expect(dispatch.args[2][0].type).toBe(actions.UPDATE_QUERY_FORM_DATA);
|
|
|
|
});
|
2019-11-27 17:02:48 -05:00
|
|
|
});
|
2018-10-15 19:52:19 -04:00
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should dispatch logEvent async action', () => {
|
|
|
|
const actionThunk = actions.postChartFormData({});
|
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, success
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
expect(fetchMock.calls(MOCK_URL)).toHaveLength(1);
|
|
|
|
expect(typeof dispatch.args[3][0]).toBe('function');
|
|
|
|
|
|
|
|
dispatch.args[3][0](dispatch);
|
|
|
|
expect(dispatch.callCount).toBe(6);
|
|
|
|
expect(dispatch.args[5][0].type).toBe(LOG_EVENT);
|
|
|
|
});
|
2018-10-15 19:52:19 -04:00
|
|
|
});
|
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should dispatch CHART_UPDATE_SUCCEEDED action upon success', () => {
|
|
|
|
const actionThunk = actions.postChartFormData({});
|
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, success
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
expect(fetchMock.calls(MOCK_URL)).toHaveLength(1);
|
|
|
|
expect(dispatch.args[4][0].type).toBe(actions.CHART_UPDATE_SUCCEEDED);
|
|
|
|
});
|
|
|
|
});
|
2018-10-25 16:52:47 -04:00
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should CHART_UPDATE_TIMEOUT action upon query timeout', () => {
|
|
|
|
const unresolvingPromise = new Promise(() => {});
|
|
|
|
fetchMock.post(MOCK_URL, () => unresolvingPromise, {
|
|
|
|
overwriteRoutes: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const timeoutInSec = 1 / 1000;
|
|
|
|
const actionThunk = actions.postChartFormData({}, false, timeoutInSec);
|
|
|
|
|
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, fail
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
expect(dispatch.args[4][0].type).toBe(actions.CHART_UPDATE_TIMEOUT);
|
|
|
|
setupDefaultFetchMock();
|
|
|
|
});
|
|
|
|
});
|
2018-10-15 19:52:19 -04:00
|
|
|
|
2020-05-07 04:09:28 -04:00
|
|
|
it('should dispatch CHART_UPDATE_FAILED action upon non-timeout non-abort failure', () => {
|
|
|
|
fetchMock.post(
|
|
|
|
MOCK_URL,
|
|
|
|
{ throws: { statusText: 'misc error' } },
|
|
|
|
{ overwriteRoutes: true },
|
|
|
|
);
|
|
|
|
|
|
|
|
const timeoutInSec = 100; // Set to a time that is longer than the time this will take to fail
|
|
|
|
const actionThunk = actions.postChartFormData({}, false, timeoutInSec);
|
|
|
|
|
|
|
|
return actionThunk(dispatch).then(() => {
|
|
|
|
// chart update, trigger query, update form data, fail
|
|
|
|
expect(dispatch.callCount).toBe(5);
|
|
|
|
const updateFailedAction = dispatch.args[4][0];
|
|
|
|
expect(updateFailedAction.type).toBe(actions.CHART_UPDATE_FAILED);
|
|
|
|
expect(updateFailedAction.queryResponse.error).toBe('misc error');
|
|
|
|
|
|
|
|
setupDefaultFetchMock();
|
|
|
|
});
|
2017-12-16 19:10:45 -05:00
|
|
|
});
|
2017-08-10 17:21:45 -04:00
|
|
|
});
|
|
|
|
});
|