2021-10-28 06:27:35 -04: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.
|
|
|
|
*/
|
|
|
|
|
2019-03-22 14:40:23 -04:00
|
|
|
import React from 'react';
|
2018-12-11 14:58:02 -05:00
|
|
|
import {
|
|
|
|
ChartPlugin,
|
|
|
|
ChartMetadata,
|
|
|
|
ChartProps,
|
|
|
|
BuildQueryFunction,
|
2019-03-22 14:40:23 -04:00
|
|
|
TransformProps,
|
2019-06-11 21:08:12 -04:00
|
|
|
getChartMetadataRegistry,
|
|
|
|
getChartComponentRegistry,
|
|
|
|
getChartTransformPropsRegistry,
|
|
|
|
getChartBuildQueryRegistry,
|
2019-08-14 13:21:17 -04:00
|
|
|
getChartControlPanelRegistry,
|
2022-01-07 20:16:24 -05:00
|
|
|
QueryFormData,
|
|
|
|
DatasourceType,
|
2022-05-04 08:55:22 -04:00
|
|
|
supersetTheme,
|
2022-01-07 20:16:24 -05:00
|
|
|
} from '@superset-ui/core';
|
2018-11-06 17:45:50 -05:00
|
|
|
|
|
|
|
describe('ChartPlugin', () => {
|
2019-03-22 14:40:23 -04:00
|
|
|
const FakeChart = () => <span>test</span>;
|
|
|
|
|
2018-12-11 14:58:02 -05:00
|
|
|
const metadata = new ChartMetadata({
|
|
|
|
name: 'test-chart',
|
|
|
|
thumbnail: '',
|
|
|
|
});
|
2018-11-06 17:45:50 -05:00
|
|
|
|
2019-06-11 21:08:12 -04:00
|
|
|
const buildQuery = () => ({
|
2019-03-22 14:40:23 -04:00
|
|
|
datasource: { id: 1, type: DatasourceType.Table },
|
|
|
|
queries: [{ granularity: 'day' }],
|
2020-05-06 12:32:29 -04:00
|
|
|
force: false,
|
2020-06-05 14:57:47 -04:00
|
|
|
result_format: 'json',
|
|
|
|
result_type: 'full',
|
2019-03-22 14:40:23 -04:00
|
|
|
});
|
|
|
|
|
2019-08-14 13:21:17 -04:00
|
|
|
const controlPanel = { abc: 1 };
|
|
|
|
|
2018-11-06 17:45:50 -05:00
|
|
|
it('exists', () => {
|
|
|
|
expect(ChartPlugin).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('new ChartPlugin()', () => {
|
2018-12-12 16:18:27 -05:00
|
|
|
const FORM_DATA = {
|
|
|
|
datasource: '1__table',
|
|
|
|
granularity: 'day',
|
|
|
|
viz_type: 'table',
|
|
|
|
};
|
2018-12-11 14:58:02 -05:00
|
|
|
|
2018-11-06 17:45:50 -05:00
|
|
|
it('creates a new plugin', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2019-03-22 14:40:23 -04:00
|
|
|
Chart: FakeChart,
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
expect(plugin).toBeInstanceOf(ChartPlugin);
|
|
|
|
});
|
|
|
|
describe('buildQuery', () => {
|
2019-03-19 17:58:20 -04:00
|
|
|
it('defaults to undefined', () => {
|
2018-11-06 17:45:50 -05:00
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
2018-12-11 14:58:02 -05:00
|
|
|
expect(plugin.loadBuildQuery).toBeUndefined();
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('uses loadBuildQuery field if specified', () => {
|
2020-05-06 12:32:29 -04:00
|
|
|
expect.assertions(2);
|
2018-11-06 17:45:50 -05:00
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
|
|
|
loadBuildQuery: () => buildQuery,
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
2020-01-28 18:15:31 -05:00
|
|
|
|
2021-11-09 07:42:28 -05:00
|
|
|
const fn =
|
|
|
|
plugin.loadBuildQuery!() as BuildQueryFunction<QueryFormData>;
|
2020-01-28 18:15:31 -05:00
|
|
|
expect(fn(FORM_DATA).queries[0]).toEqual({ granularity: 'day' });
|
2020-05-06 12:32:29 -04:00
|
|
|
expect(fn(FORM_DATA).force).toEqual(false);
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('uses buildQuery field if specified', () => {
|
2018-12-13 20:15:23 -05:00
|
|
|
expect.assertions(1);
|
2018-11-06 17:45:50 -05:00
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
|
|
|
buildQuery,
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
2020-01-28 18:15:31 -05:00
|
|
|
|
2021-11-09 07:42:28 -05:00
|
|
|
const fn =
|
|
|
|
plugin.loadBuildQuery!() as BuildQueryFunction<QueryFormData>;
|
2020-01-28 18:15:31 -05:00
|
|
|
expect(fn(FORM_DATA).queries[0]).toEqual({ granularity: 'day' });
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('Chart', () => {
|
|
|
|
it('uses loadChart if specified', () => {
|
2018-12-11 14:58:02 -05:00
|
|
|
const loadChart = () => FakeChart;
|
2018-11-06 17:45:50 -05:00
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
|
|
|
loadChart,
|
|
|
|
});
|
2019-03-22 14:40:23 -04:00
|
|
|
// the loader is sanitized, so assert on the value
|
|
|
|
expect(plugin.loadChart()).toBe(loadChart());
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('uses Chart field if specified', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
2018-12-11 14:58:02 -05:00
|
|
|
expect(plugin.loadChart()).toEqual(FakeChart);
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('throws an error if none of Chart or loadChart is specified', () => {
|
2020-01-28 18:15:31 -05:00
|
|
|
expect(() => new ChartPlugin({ metadata })).toThrow(Error);
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('transformProps', () => {
|
2018-12-11 14:58:02 -05:00
|
|
|
const PROPS = new ChartProps({
|
|
|
|
formData: FORM_DATA,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
2020-12-29 05:10:39 -05:00
|
|
|
queriesData: [{}],
|
2022-05-04 08:55:22 -04:00
|
|
|
theme: supersetTheme,
|
2018-12-11 14:58:02 -05:00
|
|
|
});
|
2018-11-06 17:45:50 -05:00
|
|
|
it('defaults to identity function', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
2019-03-22 14:40:23 -04:00
|
|
|
const fn = plugin.loadTransformProps() as TransformProps;
|
2018-12-11 14:58:02 -05:00
|
|
|
expect(fn(PROPS)).toBe(PROPS);
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('uses loadTransformProps field if specified', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
2018-11-06 17:45:50 -05:00
|
|
|
loadTransformProps: () => () => ({ field2: 2 }),
|
|
|
|
});
|
2019-03-22 14:40:23 -04:00
|
|
|
const fn = plugin.loadTransformProps() as TransformProps;
|
2018-12-11 14:58:02 -05:00
|
|
|
expect(fn(PROPS)).toEqual({ field2: 2 });
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('uses transformProps field if specified', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
2018-12-11 14:58:02 -05:00
|
|
|
Chart: FakeChart,
|
2018-11-06 17:45:50 -05:00
|
|
|
transformProps: () => ({ field2: 2 }),
|
|
|
|
});
|
2019-03-22 14:40:23 -04:00
|
|
|
const fn = plugin.loadTransformProps() as TransformProps;
|
2018-12-11 14:58:02 -05:00
|
|
|
expect(fn(PROPS)).toEqual({ field2: 2 });
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
});
|
2019-08-14 13:21:17 -04:00
|
|
|
describe('controlPanel', () => {
|
|
|
|
it('takes controlPanel from input', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
|
|
|
Chart: FakeChart,
|
|
|
|
controlPanel,
|
|
|
|
});
|
|
|
|
expect(plugin.controlPanel).toBe(controlPanel);
|
|
|
|
});
|
|
|
|
it('defaults to empty object', () => {
|
|
|
|
const plugin = new ChartPlugin({
|
|
|
|
metadata,
|
|
|
|
Chart: FakeChart,
|
|
|
|
});
|
|
|
|
expect(plugin.controlPanel).toEqual({});
|
|
|
|
});
|
|
|
|
});
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
|
2018-12-14 17:09:48 -05:00
|
|
|
describe('.register()', () => {
|
2019-06-11 21:08:12 -04:00
|
|
|
let plugin: ChartPlugin;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
plugin = new ChartPlugin({
|
|
|
|
metadata,
|
|
|
|
Chart: FakeChart,
|
|
|
|
buildQuery,
|
2019-08-14 13:21:17 -04:00
|
|
|
controlPanel,
|
2019-06-11 21:08:12 -04:00
|
|
|
});
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
2019-06-11 21:08:12 -04:00
|
|
|
|
2018-11-06 17:45:50 -05:00
|
|
|
it('throws an error if key is not provided', () => {
|
2020-01-28 18:15:31 -05:00
|
|
|
expect(() => plugin.register()).toThrow(Error);
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(() => plugin.configure({ key: 'ab' }).register()).not.toThrow(
|
|
|
|
Error,
|
|
|
|
);
|
2019-06-11 21:08:12 -04:00
|
|
|
});
|
|
|
|
it('add the plugin to the registries', () => {
|
|
|
|
plugin.configure({ key: 'cd' }).register();
|
|
|
|
expect(getChartMetadataRegistry().get('cd')).toBe(metadata);
|
|
|
|
expect(getChartComponentRegistry().get('cd')).toBe(FakeChart);
|
|
|
|
expect(getChartTransformPropsRegistry().has('cd')).toEqual(true);
|
|
|
|
expect(getChartBuildQueryRegistry().get('cd')).toBe(buildQuery);
|
2019-08-14 13:21:17 -04:00
|
|
|
expect(getChartControlPanelRegistry().get('cd')).toBe(controlPanel);
|
2019-06-11 21:08:12 -04:00
|
|
|
});
|
|
|
|
it('does not register buildQuery when it is not specified in the ChartPlugin', () => {
|
|
|
|
new ChartPlugin({
|
|
|
|
metadata,
|
|
|
|
Chart: FakeChart,
|
|
|
|
})
|
|
|
|
.configure({ key: 'ef' })
|
|
|
|
.register();
|
|
|
|
expect(getChartBuildQueryRegistry().has('ef')).toEqual(false);
|
|
|
|
});
|
|
|
|
it('returns itself', () => {
|
|
|
|
expect(plugin.configure({ key: 'gh' }).register()).toBe(plugin);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('.unregister()', () => {
|
|
|
|
let plugin: ChartPlugin;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
plugin = new ChartPlugin({
|
|
|
|
metadata,
|
|
|
|
Chart: FakeChart,
|
|
|
|
buildQuery,
|
2019-08-14 13:21:17 -04:00
|
|
|
controlPanel,
|
2019-06-11 21:08:12 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws an error if key is not provided', () => {
|
2020-01-28 18:15:31 -05:00
|
|
|
expect(() => plugin.unregister()).toThrow(Error);
|
2021-11-09 07:42:28 -05:00
|
|
|
expect(() => plugin.configure({ key: 'abc' }).unregister()).not.toThrow(
|
|
|
|
Error,
|
|
|
|
);
|
2019-06-11 21:08:12 -04:00
|
|
|
});
|
|
|
|
it('removes the chart from the registries', () => {
|
|
|
|
plugin.configure({ key: 'def' }).register();
|
|
|
|
expect(getChartMetadataRegistry().get('def')).toBe(metadata);
|
|
|
|
expect(getChartComponentRegistry().get('def')).toBe(FakeChart);
|
|
|
|
expect(getChartTransformPropsRegistry().has('def')).toEqual(true);
|
|
|
|
expect(getChartBuildQueryRegistry().get('def')).toBe(buildQuery);
|
2019-08-14 13:21:17 -04:00
|
|
|
expect(getChartControlPanelRegistry().get('def')).toBe(controlPanel);
|
2019-06-11 21:08:12 -04:00
|
|
|
plugin.unregister();
|
2019-08-14 13:21:17 -04:00
|
|
|
expect(getChartMetadataRegistry().has('def')).toBeFalsy();
|
|
|
|
expect(getChartComponentRegistry().has('def')).toBeFalsy();
|
|
|
|
expect(getChartTransformPropsRegistry().has('def')).toBeFalsy();
|
|
|
|
expect(getChartBuildQueryRegistry().has('def')).toBeFalsy();
|
|
|
|
expect(getChartControlPanelRegistry().has('def')).toBeFalsy();
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
it('returns itself', () => {
|
2019-06-11 21:08:12 -04:00
|
|
|
expect(plugin.configure({ key: 'xyz' }).unregister()).toBe(plugin);
|
2018-11-06 17:45:50 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|