| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /*
- * 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 {
- getComparisonInfo,
- ComparisonTimeRangeType,
- VizType,
- } from '@superset-ui/core';
- const form_data = {
- datasource: '22__table',
- viz_type: VizType.BigNumberPeriodOverPeriod,
- slice_id: 97,
- url_params: {
- form_data_key:
- 'TaBakyDiAx2VsQ47gLmlsJKeN4foqnoxUKdbQrM05qnKMRjO9PDe42iZN1oxmxZ8',
- save_action: 'overwrite',
- slice_id: '97',
- },
- metrics: ['count'],
- adhoc_filters: [
- {
- clause: 'WHERE',
- comparator: '2004-02-16 : 2024-02-16',
- datasourceWarning: false,
- expressionType: 'SIMPLE',
- filterOptionName: 'filter_8274fo9pogn_ihi8x28o7a',
- isExtra: false,
- isNew: false,
- operator: 'TEMPORAL_RANGE',
- sqlExpression: null,
- subject: 'order_date',
- } as any,
- ],
- time_comparison: 'y',
- adhoc_custom: [
- {
- clause: 'WHERE',
- comparator: 'No filter',
- expressionType: 'SIMPLE',
- operator: 'TEMPORAL_RANGE',
- subject: 'order_date',
- },
- ],
- row_limit: 10000,
- y_axis_format: 'SMART_NUMBER',
- header_font_size: 60,
- subheader_font_size: 26,
- comparison_color_enabled: true,
- column_config: {
- name: {
- visible: true,
- },
- },
- extra_form_data: {},
- force: false,
- result_format: 'json',
- result_type: 'full',
- };
- const mockExtraFormData = {
- time_range: 'new and cool range from extra form data',
- };
- describe('getComparisonInfo', () => {
- it('Keeps the original adhoc_filters since no extra data was passed', () => {
- const resultFormData = getComparisonInfo(
- form_data,
- ComparisonTimeRangeType.Year,
- {},
- );
- expect(resultFormData).toEqual(form_data);
- });
- it('Updates the time_range of the adhoc_filters when extra form data is passed', () => {
- const resultFormData = getComparisonInfo(
- form_data,
- ComparisonTimeRangeType.Month,
- mockExtraFormData,
- );
- const expectedFilters = [
- {
- clause: 'WHERE',
- comparator: 'new and cool range from extra form data',
- datasourceWarning: false,
- expressionType: 'SIMPLE',
- filterOptionName: 'filter_8274fo9pogn_ihi8x28o7a',
- isExtra: false,
- isNew: false,
- operator: 'TEMPORAL_RANGE',
- sqlExpression: null,
- subject: 'order_date',
- } as any,
- ];
- expect(resultFormData.adhoc_filters?.length).toEqual(1);
- expect(resultFormData.adhoc_filters).toEqual(expectedFilters);
- });
- it('handles no time range filters', () => {
- const resultFormData = getComparisonInfo(
- {
- ...form_data,
- adhoc_filters: [
- {
- expressionType: 'SIMPLE',
- subject: 'address_line1',
- operator: 'IN',
- comparator: ['7734 Strong St.'],
- clause: 'WHERE',
- isExtra: false,
- },
- ],
- },
- ComparisonTimeRangeType.Week,
- {},
- );
- const expectedFilters = [
- {
- expressionType: 'SIMPLE',
- subject: 'address_line1',
- operator: 'IN',
- comparator: ['7734 Strong St.'],
- clause: 'WHERE',
- isExtra: false,
- },
- ];
- expect(resultFormData.adhoc_filters?.length).toEqual(1);
- expect(resultFormData.adhoc_filters?.[0]).toEqual(expectedFilters[0]);
- });
- it('If adhoc_filter is undefined the code wont break', () => {
- const resultFormData = getComparisonInfo(
- {
- ...form_data,
- adhoc_filters: undefined,
- },
- ComparisonTimeRangeType.InheritedRange,
- {},
- );
- expect(resultFormData.adhoc_filters?.length).toEqual(0);
- expect(resultFormData.adhoc_filters).toEqual([]);
- });
- it('Handles the custom time filters and return the correct time shift text', () => {
- const resultFormData = getComparisonInfo(
- form_data,
- ComparisonTimeRangeType.Custom,
- {},
- );
- const expectedFilters = [
- {
- clause: 'WHERE',
- comparator: 'No filter',
- expressionType: 'SIMPLE',
- operator: 'TEMPORAL_RANGE',
- subject: 'order_date',
- },
- ];
- expect(resultFormData.adhoc_filters?.length).toEqual(1);
- expect(resultFormData.adhoc_filters).toEqual(expectedFilters);
- });
- it('Updates comparison display values when toggled', () => {
- const resultFormData = getComparisonInfo(
- {
- ...form_data,
- column_config: {
- name: {
- visible: false,
- },
- },
- },
- ComparisonTimeRangeType.Year,
- {},
- );
- expect(resultFormData.column_config.name.visible).toEqual(false);
- });
- });
|