superset/superset-frontend/spec/javascripts/explore/utils_spec.jsx

249 lines
7.0 KiB
React
Raw Normal View History

/**
* 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.
*/
import sinon from 'sinon';
2017-04-05 13:45:32 -04:00
import URI from 'urijs';
import {
buildV1ChartDataPayload,
getExploreUrl,
getExploreLongUrl,
shouldUseLegacyApi,
} from 'src/explore/exploreUtils';
import * as hostNamesConfig from 'src/utils/hostNamesConfig';
import { getChartMetadataRegistry } from '@superset-ui/chart';
2017-04-05 13:45:32 -04:00
describe('exploreUtils', () => {
const location = window.location;
2017-04-05 13:45:32 -04:00
const formData = {
datasource: '1__table',
};
const sFormData = JSON.stringify(formData);
function compareURI(uri1, uri2) {
[refactor] Migrate from Mocha+Chai to Jest (#6079) * [refactor] Migrate from Mocha+Chai to Jest This change migrates all the existing unit tests - to Jest's global expect and matchers from chai's imported expect, asserts and matchers. - to Jest's describe/test from mocha's describe/it The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks: 1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank. 2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :) This change also removes dependencies to Mocha and Chai. * Remove the test:one command as it now does the same thing as test. * Fixing lint errors. The diff looks large but is large done through `yarn run lint --fix` The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest. * Adding eslint-plugin-jest and further modify tests. - One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing. - Deletion of a duplicated test caught by eslint-plugin-jest. * - Make istanbul coverage work with Jest. - Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes! * Attempt to fix dynamic imports in tests. * run sequentially and log heap usage * - tweaking maxworkers for travis and specifying coverageDirectory for codecov - remove dynamic import in shim.js now that it is set in babelrc for tests only.
2018-10-15 16:10:18 -04:00
expect(uri1.toString()).toBe(uri2.toString());
2017-04-05 13:45:32 -04:00
}
describe('getExploreUrl', () => {
it('generates proper base url', () => {
// This assertion is to show clearly the value of location.href
// in the context of unit tests.
[refactor] Migrate from Mocha+Chai to Jest (#6079) * [refactor] Migrate from Mocha+Chai to Jest This change migrates all the existing unit tests - to Jest's global expect and matchers from chai's imported expect, asserts and matchers. - to Jest's describe/test from mocha's describe/it The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks: 1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank. 2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :) This change also removes dependencies to Mocha and Chai. * Remove the test:one command as it now does the same thing as test. * Fixing lint errors. The diff looks large but is large done through `yarn run lint --fix` The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest. * Adding eslint-plugin-jest and further modify tests. - One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing. - Deletion of a duplicated test caught by eslint-plugin-jest. * - Make istanbul coverage work with Jest. - Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes! * Attempt to fix dynamic imports in tests. * run sequentially and log heap usage * - tweaking maxworkers for travis and specifying coverageDirectory for codecov - remove dynamic import in shim.js now that it is set in babelrc for tests only.
2018-10-15 16:10:18 -04:00
expect(location.href).toBe('http://localhost/');
2017-04-05 13:45:32 -04:00
const url = getExploreUrl({
formData,
endpointType: 'base',
force: false,
curUrl: 'http://superset.com',
});
compareURI(URI(url), URI('/superset/explore/'));
});
it('generates proper json url', () => {
const url = getExploreUrl({
formData,
endpointType: 'json',
force: false,
curUrl: 'http://superset.com',
});
compareURI(URI(url), URI('/superset/explore_json/'));
});
it('generates proper json forced url', () => {
const url = getExploreUrl({
formData,
endpointType: 'json',
force: true,
curUrl: 'superset.com',
});
compareURI(
URI(url),
URI('/superset/explore_json/').search({ force: 'true' }),
);
});
it('generates proper csv URL', () => {
const url = getExploreUrl({
formData,
endpointType: 'csv',
force: false,
curUrl: 'superset.com',
});
compareURI(
URI(url),
URI('/superset/explore_json/').search({ csv: 'true' }),
);
});
it('generates proper standalone URL', () => {
const url = getExploreUrl({
formData,
endpointType: 'standalone',
force: false,
curUrl: 'superset.com',
});
compareURI(
URI(url),
URI('/superset/explore/').search({ standalone: 'true' }),
);
});
it('preserves main URLs params', () => {
const url = getExploreUrl({
formData,
endpointType: 'json',
force: false,
curUrl: 'superset.com?foo=bar',
});
compareURI(
URI(url),
URI('/superset/explore_json/').search({ foo: 'bar' }),
);
});
it('generate proper save slice url', () => {
const url = getExploreUrl({
formData,
endpointType: 'json',
force: false,
curUrl: 'superset.com?foo=bar',
});
compareURI(
URI(url),
URI('/superset/explore_json/').search({ foo: 'bar' }),
);
});
});
describe('domain sharding', () => {
let stub;
const availableDomains = [
'http://localhost/',
'domain1.com',
'domain2.com',
'domain3.com',
];
beforeEach(() => {
stub = sinon
.stub(hostNamesConfig, 'availableDomains')
.value(availableDomains);
});
afterEach(() => {
stub.restore();
});
it('generate url to different domains', () => {
let url = getExploreUrl({
formData,
endpointType: 'json',
allowDomainSharding: true,
});
// skip main domain for fetching chart if domain sharding is enabled
// to leave main domain free for other calls like fav star, save change, etc.
expect(url).toMatch(availableDomains[1]);
url = getExploreUrl({
formData,
endpointType: 'json',
allowDomainSharding: true,
});
expect(url).toMatch(availableDomains[2]);
url = getExploreUrl({
formData,
endpointType: 'json',
allowDomainSharding: true,
});
expect(url).toMatch(availableDomains[3]);
// circle back to first available domain
url = getExploreUrl({
formData,
endpointType: 'json',
allowDomainSharding: true,
});
expect(url).toMatch(availableDomains[1]);
});
it('not generate url to different domains without flag', () => {
let csvURL = getExploreUrl({
formData,
endpointType: 'csv',
});
expect(csvURL).toMatch(availableDomains[0]);
csvURL = getExploreUrl({
formData,
endpointType: 'csv',
});
expect(csvURL).toMatch(availableDomains[0]);
});
});
describe('getExploreLongUrl', () => {
it('generates proper base url with form_data', () => {
compareURI(
URI(getExploreLongUrl(formData, 'base')),
URI('/superset/explore/').search({ form_data: sFormData }),
);
});
2017-04-05 13:45:32 -04:00
});
describe('buildV1ChartDataPayload', () => {
it('generate valid request payload despite no registered buildQuery', () => {
const v1RequestPayload = buildV1ChartDataPayload({
formData: { ...formData, viz_type: 'my_custom_viz' },
});
expect(v1RequestPayload).hasOwnProperty('queries');
});
});
describe('shouldUseLegacyApi', () => {
beforeAll(() => {
getChartMetadataRegistry()
.registerValue('my_legacy_viz', { useLegacyApi: true })
.registerValue('my_v1_viz', { useLegacyApi: false });
});
afterAll(() => {
getChartMetadataRegistry().remove('my_legacy_viz').remove('my_v1_viz');
});
it('returns true for legacy viz', () => {
const useLegacyApi = shouldUseLegacyApi({
...formData,
viz_type: 'my_legacy_viz',
});
expect(useLegacyApi).toBe(true);
});
it('returns false for v1 viz', () => {
const useLegacyApi = shouldUseLegacyApi({
...formData,
viz_type: 'my_v1_viz',
});
expect(useLegacyApi).toBe(false);
});
it('returns false for formData with unregistered viz_type', () => {
const useLegacyApi = shouldUseLegacyApi({
...formData,
viz_type: 'undefined_viz',
});
expect(useLegacyApi).toBe(false);
});
it('returns false for formData without viz_type', () => {
const useLegacyApi = shouldUseLegacyApi(formData);
expect(useLegacyApi).toBe(false);
});
});
2017-04-05 13:45:32 -04:00
});