|
| 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 { supersetTheme } from '@apache-superset/core/theme'; |
| 20 | +import { ChartProps } from '@superset-ui/core'; |
| 21 | +import transformProps from '../src/transformProps'; |
| 22 | + |
| 23 | +const baseChartProps = { |
| 24 | + width: 800, |
| 25 | + height: 600, |
| 26 | + datasource: {}, |
| 27 | + theme: supersetTheme, |
| 28 | + hooks: {}, |
| 29 | +}; |
| 30 | + |
| 31 | +const formData = { |
| 32 | + groupby: 'source_col', |
| 33 | + columns: 'target_col', |
| 34 | + metric: 'sum__value', |
| 35 | + yAxisFormat: '.2f', |
| 36 | + colorScheme: 'd3Category10', |
| 37 | + sliceId: 1, |
| 38 | +}; |
| 39 | + |
| 40 | +test('builds the chord node list and matrix from v1 records', () => { |
| 41 | + const chartProps = new ChartProps({ |
| 42 | + ...baseChartProps, |
| 43 | + formData, |
| 44 | + queriesData: [ |
| 45 | + { |
| 46 | + data: [ |
| 47 | + { source_col: 'a', target_col: 'b', sum__value: 10 }, |
| 48 | + { source_col: 'b', target_col: 'a', sum__value: 5 }, |
| 49 | + { source_col: 'a', target_col: 'c', sum__value: 2 }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + ], |
| 53 | + }); |
| 54 | + const { data } = transformProps(chartProps) as { |
| 55 | + data: { nodes: string[]; matrix: number[][] }; |
| 56 | + }; |
| 57 | + expect(data.nodes).toEqual(['a', 'b', 'c']); |
| 58 | + // matrix[targetIndex][sourceIndex], matching the legacy backend layout |
| 59 | + expect(data.matrix).toEqual([ |
| 60 | + [0, 5, 0], // into a: from a, b, c |
| 61 | + [10, 0, 0], // into b |
| 62 | + [2, 0, 0], // into c |
| 63 | + ]); |
| 64 | +}); |
| 65 | + |
| 66 | +test('keeps the last value for duplicate source/target pairs', () => { |
| 67 | + const chartProps = new ChartProps({ |
| 68 | + ...baseChartProps, |
| 69 | + formData, |
| 70 | + queriesData: [ |
| 71 | + { |
| 72 | + data: [ |
| 73 | + { source_col: 'a', target_col: 'b', sum__value: 1 }, |
| 74 | + { source_col: 'a', target_col: 'b', sum__value: 9 }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + const { data } = transformProps(chartProps) as { |
| 80 | + data: { nodes: string[]; matrix: number[][] }; |
| 81 | + }; |
| 82 | + expect(data.matrix[1][0]).toEqual(9); |
| 83 | +}); |
| 84 | + |
| 85 | +test('passes through legacy pre-shaped payloads', () => { |
| 86 | + const legacyPayload = { nodes: ['a'], matrix: [[0]] }; |
| 87 | + const chartProps = new ChartProps({ |
| 88 | + ...baseChartProps, |
| 89 | + formData, |
| 90 | + queriesData: [{ data: legacyPayload }], |
| 91 | + }); |
| 92 | + const { data } = transformProps(chartProps) as { data: unknown }; |
| 93 | + expect(data).toEqual(legacyPayload); |
| 94 | +}); |
0 commit comments