|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { ControlPanelsContainerProps } from '@superset-ui/chart-controls/types'; |
| 20 | +import controlPanel from '../../../src/Timeseries/Regular/Scatter/controlPanel'; |
| 21 | + |
| 22 | +const config = controlPanel; |
| 23 | + |
| 24 | +const getControl = (controlName: string) => { |
| 25 | + for (const section of config.controlPanelSections) { |
| 26 | + if (section && section.controlSetRows) { |
| 27 | + for (const row of section.controlSetRows) { |
| 28 | + for (const control of row) { |
| 29 | + if ( |
| 30 | + typeof control === 'object' && |
| 31 | + control !== null && |
| 32 | + 'name' in control && |
| 33 | + control.name === controlName |
| 34 | + ) { |
| 35 | + return control; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return null; |
| 43 | +}; |
| 44 | + |
| 45 | +const mockControls = ( |
| 46 | + xAxisColumn: string | null, |
| 47 | + xAxisType: string | null, |
| 48 | +): ControlPanelsContainerProps => { |
| 49 | + const options = xAxisType |
| 50 | + ? [{ column_name: xAxisColumn, type: xAxisType }] |
| 51 | + : []; |
| 52 | + |
| 53 | + return { |
| 54 | + controls: { |
| 55 | + // @ts-ignore |
| 56 | + x_axis: { |
| 57 | + value: xAxisColumn, |
| 58 | + options: options, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }; |
| 62 | +}; |
| 63 | + |
| 64 | +// tests for x_axis_time_format control |
| 65 | +const timeFormatControl: any = getControl('x_axis_time_format'); |
| 66 | + |
| 67 | +test('scatter chart control panel should include x_axis_time_format control in the panel', () => { |
| 68 | + expect(timeFormatControl).toBeDefined(); |
| 69 | +}); |
| 70 | + |
| 71 | +test('scatter chart control panel should have correct default value for x_axis_time_format', () => { |
| 72 | + expect(timeFormatControl).toBeDefined(); |
| 73 | + expect(timeFormatControl.config).toBeDefined(); |
| 74 | + expect(timeFormatControl.config.default).toBe('smart_date'); |
| 75 | +}); |
| 76 | + |
| 77 | +test('scatter chart control panel should have visibility function for x_axis_time_format', () => { |
| 78 | + expect(timeFormatControl).toBeDefined(); |
| 79 | + expect(timeFormatControl.config.visibility).toBeDefined(); |
| 80 | + expect(typeof timeFormatControl.config.visibility).toBe('function'); |
| 81 | + |
| 82 | + // The visibility function exists - the exact logic is tested implicitly through UI behavior |
| 83 | + // The important part is that the control has proper visibility configuration |
| 84 | +}); |
| 85 | + |
| 86 | +const isTimeVisible = ( |
| 87 | + xAxisColumn: string | null, |
| 88 | + xAxisType: string | null, |
| 89 | +): boolean => { |
| 90 | + const props = mockControls(xAxisColumn, xAxisType); |
| 91 | + const visibilityFn = timeFormatControl?.config?.visibility; |
| 92 | + return visibilityFn ? visibilityFn(props) : false; |
| 93 | +}; |
| 94 | + |
| 95 | +test('x_axis_time_format control should be visible for any data types include TIME', () => { |
| 96 | + expect(isTimeVisible('time_column', 'TIME')).toBe(true); |
| 97 | + expect(isTimeVisible('time_column', 'TIME WITH TIME ZONE')).toBe(true); |
| 98 | + expect(isTimeVisible('time_column', 'TIMESTAMP WITH TIME ZONE')).toBe(true); |
| 99 | + expect(isTimeVisible('time_column', 'TIMESTAMP WITHOUT TIME ZONE')).toBe( |
| 100 | + true, |
| 101 | + ); |
| 102 | +}); |
| 103 | + |
| 104 | +test('x_axis_time_format control should be hidden for data types that do NOT include TIME', () => { |
| 105 | + expect(isTimeVisible('null', 'null')).toBe(false); |
| 106 | + expect(isTimeVisible(null, null)).toBe(false); |
| 107 | + expect(isTimeVisible('float_column', 'FLOAT')).toBe(false); |
| 108 | +}); |
| 109 | + |
| 110 | +// tests for x_axis_number_format control |
| 111 | +const numberFormatControl: any = getControl('x_axis_number_format'); |
| 112 | + |
| 113 | +test('scatter chart control panel should include x_axis_number_format control in the panel', () => { |
| 114 | + expect(numberFormatControl).toBeDefined(); |
| 115 | +}); |
| 116 | + |
| 117 | +test('scatter chart control panel should have correct default value for x_axis_number_format', () => { |
| 118 | + expect(numberFormatControl).toBeDefined(); |
| 119 | + expect(numberFormatControl.config).toBeDefined(); |
| 120 | + expect(numberFormatControl.config.default).toBe('SMART_NUMBER'); |
| 121 | +}); |
| 122 | + |
| 123 | +test('scatter chart control panel should have visibility function for x_axis_number_format', () => { |
| 124 | + expect(numberFormatControl).toBeDefined(); |
| 125 | + expect(numberFormatControl.config.visibility).toBeDefined(); |
| 126 | + expect(typeof numberFormatControl.config.visibility).toBe('function'); |
| 127 | + |
| 128 | + // The visibility function exists - the exact logic is tested implicitly through UI behavior |
| 129 | + // The important part is that the control has proper visibility configuration |
| 130 | +}); |
| 131 | + |
| 132 | +const isNumberVisible = ( |
| 133 | + xAxisColumn: string | null, |
| 134 | + xAxisType: string | null, |
| 135 | +): boolean => { |
| 136 | + const props = mockControls(xAxisColumn, xAxisType); |
| 137 | + const visibilityFn = numberFormatControl?.config?.visibility; |
| 138 | + return visibilityFn ? visibilityFn(props) : false; |
| 139 | +}; |
| 140 | + |
| 141 | +test('x_axis_number_format control should be visible for any floating-point data types', () => { |
| 142 | + expect(isNumberVisible('float_column', 'FLOAT')).toBe(true); |
| 143 | + expect(isNumberVisible('double_column', 'DOUBLE')).toBe(true); |
| 144 | + expect(isNumberVisible('real_column', 'REAL')).toBe(true); |
| 145 | + expect(isNumberVisible('numeric_column', 'NUMERIC')).toBe(true); |
| 146 | + expect(isNumberVisible('decimal_column', 'DECIMAL')).toBe(true); |
| 147 | +}); |
| 148 | + |
| 149 | +test('x_axis_number_format control should be hidden for any non-floating-point data types', () => { |
| 150 | + expect(isNumberVisible('string_column', 'VARCHAR')).toBe(false); |
| 151 | + expect(isNumberVisible('null', 'null')).toBe(false); |
| 152 | + expect(isNumberVisible(null, null)).toBe(false); |
| 153 | + expect(isNumberVisible('time_column', 'TIMESTAMP WITHOUT TIME ZONE')).toBe( |
| 154 | + false, |
| 155 | + ); |
| 156 | +}); |
0 commit comments