| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- /*
- * 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 fetchMock from 'fetch-mock';
- import {
- SupersetClientClass,
- SupersetClient,
- buildQueryContext,
- QueryFormData,
- configure as configureTranslation,
- ChartClient,
- getChartBuildQueryRegistry,
- getChartMetadataRegistry,
- ChartMetadata,
- VizType,
- } from '@superset-ui/core';
- import { LOGIN_GLOB } from '../fixtures/constants';
- import { sankeyFormData } from '../fixtures/formData';
- import { SliceIdAndOrFormData } from '../../../src/chart/clients/ChartClient';
- configureTranslation();
- describe('ChartClient', () => {
- let chartClient: ChartClient;
- beforeAll(() => {
- fetchMock.get(LOGIN_GLOB, { result: '1234' });
- SupersetClient.reset();
- SupersetClient.configure().init();
- });
- beforeEach(() => {
- chartClient = new ChartClient();
- });
- afterEach(() => fetchMock.restore());
- describe('new ChartClient(config)', () => {
- it('creates a client without argument', () => {
- expect(chartClient).toBeInstanceOf(ChartClient);
- });
- it('creates a client with specified config.client', () => {
- const customClient = new SupersetClientClass();
- chartClient = new ChartClient({ client: customClient });
- expect(chartClient).toBeInstanceOf(ChartClient);
- expect(chartClient.client).toBe(customClient);
- });
- });
- describe('.loadFormData({ sliceId, formData }, options)', () => {
- const sliceId = 123;
- it('fetches formData if given only sliceId', () => {
- fetchMock.get(
- `glob:*/api/v1/form_data/?slice_id=${sliceId}`,
- sankeyFormData,
- );
- return expect(chartClient.loadFormData({ sliceId })).resolves.toEqual(
- sankeyFormData,
- );
- });
- it('fetches formData from sliceId and merges with specify formData if both fields are specified', () => {
- fetchMock.get(
- `glob:*/api/v1/form_data/?slice_id=${sliceId}`,
- sankeyFormData,
- );
- return expect(
- chartClient.loadFormData({
- sliceId,
- formData: {
- granularity: 'second',
- viz_type: VizType.Bar,
- },
- }),
- ).resolves.toEqual({
- ...sankeyFormData,
- granularity: 'second',
- viz_type: VizType.Bar,
- });
- });
- it('returns promise of formData if only formData was given', () =>
- expect(
- chartClient.loadFormData({
- formData: {
- datasource: '1__table',
- granularity: 'minute',
- viz_type: VizType.Line,
- },
- }),
- ).resolves.toEqual({
- datasource: '1__table',
- granularity: 'minute',
- viz_type: VizType.Line,
- }));
- it('rejects if none of sliceId or formData is specified', () =>
- expect(
- chartClient.loadFormData({} as SliceIdAndOrFormData),
- ).rejects.toEqual(
- new Error('At least one of sliceId or formData must be specified'),
- ));
- });
- describe('.loadQueryData(formData, options)', () => {
- it('returns a promise of query data for known chart type', () => {
- getChartMetadataRegistry().registerValue(
- VizType.WordCloud,
- new ChartMetadata({ name: 'Word Cloud', thumbnail: '' }),
- );
- getChartBuildQueryRegistry().registerValue(
- VizType.WordCloud,
- (formData: QueryFormData) => buildQueryContext(formData),
- );
- fetchMock.post('glob:*/api/v1/chart/data', [
- {
- field1: 'abc',
- field2: 'def',
- },
- ]);
- return expect(
- chartClient.loadQueryData({
- granularity: 'minute',
- viz_type: VizType.WordCloud,
- datasource: '1__table',
- }),
- ).resolves.toEqual([
- {
- field1: 'abc',
- field2: 'def',
- },
- ]);
- });
- it('returns a promise that rejects for unknown chart type', () =>
- expect(
- chartClient.loadQueryData({
- granularity: 'minute',
- viz_type: 'rainbow_3d_pie',
- datasource: '1__table',
- }),
- ).rejects.toEqual(new Error('Unknown chart type: rainbow_3d_pie')));
- it('fetches data from the legacy API if ChartMetadata has useLegacyApi=true,', () => {
- // note legacy charts do not register a buildQuery function in the registry
- getChartMetadataRegistry().registerValue(
- 'word_cloud_legacy',
- new ChartMetadata({
- name: 'Legacy Word Cloud',
- thumbnail: '.png',
- useLegacyApi: true,
- }),
- );
- fetchMock.post('glob:*/api/v1/chart/data', () =>
- Promise.reject(new Error('Unexpected all to v1 API')),
- );
- fetchMock.post('glob:*/superset/explore_json/', {
- field1: 'abc',
- field2: 'def',
- });
- return expect(
- chartClient.loadQueryData({
- granularity: 'minute',
- viz_type: 'word_cloud_legacy',
- datasource: '1__table',
- }),
- ).resolves.toEqual([
- {
- field1: 'abc',
- field2: 'def',
- },
- ]);
- });
- });
- describe('.loadDatasource(datasourceKey, options)', () => {
- it('fetches datasource', () => {
- fetchMock.get(
- 'glob:*/superset/fetch_datasource_metadata?datasourceKey=1__table',
- {
- field1: 'abc',
- field2: 'def',
- },
- );
- return expect(chartClient.loadDatasource('1__table')).resolves.toEqual({
- field1: 'abc',
- field2: 'def',
- });
- });
- });
- describe('.loadAnnotation(annotationLayer)', () => {
- it('returns an empty object if the annotation layer does not require query', () =>
- expect(
- chartClient.loadAnnotation({
- name: 'my-annotation',
- }),
- ).resolves.toEqual({}));
- it('otherwise returns a rejected promise because it is not implemented yet', () =>
- expect(
- chartClient.loadAnnotation({
- name: 'my-annotation',
- sourceType: 'abc',
- }),
- ).rejects.toEqual(new Error('This feature is not implemented yet.')));
- });
- describe('.loadAnnotations(annotationLayers)', () => {
- it('loads multiple annotation layers and combine results', () =>
- expect(
- chartClient.loadAnnotations([
- {
- name: 'anno1',
- },
- {
- name: 'anno2',
- },
- {
- name: 'anno3',
- },
- ]),
- ).resolves.toEqual({
- anno1: {},
- anno2: {},
- anno3: {},
- }));
- it('returns an empty object if input is not an array', () =>
- expect(chartClient.loadAnnotations()).resolves.toEqual({}));
- it('returns an empty object if input is an empty array', () =>
- expect(chartClient.loadAnnotations()).resolves.toEqual({}));
- });
- describe('.loadChartData({ sliceId, formData })', () => {
- const sliceId = 10120;
- it('loadAllDataNecessaryForAChart', () => {
- fetchMock.get(`glob:*/api/v1/form_data/?slice_id=${sliceId}`, {
- granularity: 'minute',
- viz_type: VizType.Line,
- datasource: '1__table',
- color: 'living-coral',
- });
- fetchMock.get(
- 'glob:*/superset/fetch_datasource_metadata?datasourceKey=1__table',
- {
- name: 'transactions',
- schema: 'staging',
- },
- );
- fetchMock.post('glob:*/api/v1/chart/data', {
- lorem: 'ipsum',
- dolor: 'sit',
- amet: true,
- });
- getChartMetadataRegistry().registerValue(
- VizType.Line,
- new ChartMetadata({ name: 'Line', thumbnail: '.gif' }),
- );
- getChartBuildQueryRegistry().registerValue(
- VizType.Line,
- (formData: QueryFormData) => buildQueryContext(formData),
- );
- return expect(
- chartClient.loadChartData({
- sliceId,
- }),
- ).resolves.toEqual({
- annotationData: {},
- datasource: {
- name: 'transactions',
- schema: 'staging',
- },
- formData: {
- granularity: 'minute',
- viz_type: VizType.Line,
- datasource: '1__table',
- color: 'living-coral',
- },
- queriesData: [
- {
- lorem: 'ipsum',
- dolor: 'sit',
- amet: true,
- },
- ],
- });
- });
- });
- });
|