transformProps.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 {
  20. ChartProps,
  21. getChartTransformPropsRegistry,
  22. supersetTheme,
  23. } from '@superset-ui/core';
  24. import { LayerConf, MapViewConfigs, ZoomConfigs } from '../../src/types';
  25. import transformProps from '../../src/plugin/transformProps';
  26. import {
  27. groupedTimeseriesChartData,
  28. groupedTimeseriesLabelMap,
  29. } from '../testData';
  30. describe('CartodiagramPlugin transformProps', () => {
  31. const chartSize: ZoomConfigs = {
  32. type: 'FIXED',
  33. configs: {
  34. height: 10,
  35. width: 10,
  36. zoom: 1,
  37. },
  38. values: {
  39. 1: {
  40. height: 10,
  41. width: 10,
  42. },
  43. },
  44. };
  45. const layerConfigs: LayerConf[] = [
  46. {
  47. type: 'XYZ',
  48. title: 'foo',
  49. url: 'example.com',
  50. },
  51. ];
  52. const mapView: MapViewConfigs = {
  53. mode: 'FIT_DATA',
  54. zoom: 1,
  55. latitude: 0,
  56. longitude: 0,
  57. fixedZoom: 1,
  58. fixedLatitude: 0,
  59. fixedLongitude: 0,
  60. };
  61. // only minimal subset of actual params
  62. const selectedChartParams = {
  63. groupby: ['bar'],
  64. x_axis: 'mydate',
  65. };
  66. const selectedChart = {
  67. id: 1,
  68. viz_type: 'pie',
  69. slice_name: 'foo',
  70. params: JSON.stringify(selectedChartParams),
  71. };
  72. const formData = {
  73. viz_type: 'cartodiagram',
  74. geomColumn: 'geom',
  75. selectedChart: JSON.stringify(selectedChart),
  76. chartSize,
  77. layerConfigs,
  78. mapView,
  79. chartBackgroundColor: '#000000',
  80. chartBackgroundBorderRadius: 5,
  81. };
  82. const chartProps = new ChartProps({
  83. formData,
  84. width: 800,
  85. height: 600,
  86. queriesData: [
  87. {
  88. data: groupedTimeseriesChartData,
  89. label_map: groupedTimeseriesLabelMap,
  90. },
  91. ],
  92. theme: supersetTheme,
  93. });
  94. let chartTransformPropsPieMock: jest.MockedFunction<any>;
  95. let chartTransformPropsTimeseriesMock: jest.MockedFunction<any>;
  96. beforeEach(() => {
  97. chartTransformPropsPieMock = jest.fn();
  98. chartTransformPropsTimeseriesMock = jest.fn();
  99. const registry = getChartTransformPropsRegistry();
  100. registry.registerValue('pie', chartTransformPropsPieMock);
  101. registry.registerValue(
  102. 'echarts_timeseries',
  103. chartTransformPropsTimeseriesMock,
  104. );
  105. });
  106. afterEach(() => {
  107. // remove registered transformProps
  108. const registry = getChartTransformPropsRegistry();
  109. registry.clear();
  110. });
  111. it('should call the transform props function of the referenced chart', () => {
  112. transformProps(chartProps);
  113. expect(chartTransformPropsPieMock).toHaveBeenCalled();
  114. expect(chartTransformPropsTimeseriesMock).not.toHaveBeenCalled();
  115. });
  116. it('should transform chart props for viz', () => {
  117. const transformedProps = transformProps(chartProps);
  118. expect(transformedProps).toEqual(
  119. expect.objectContaining({
  120. width: chartProps.width,
  121. height: chartProps.height,
  122. geomColumn: formData.geomColumn,
  123. selectedChart: expect.objectContaining({
  124. viz_type: selectedChart.viz_type,
  125. params: selectedChartParams,
  126. }),
  127. // The actual test for the created chartConfigs
  128. // will be done in transformPropsUtil.test.ts
  129. chartConfigs: expect.objectContaining({
  130. type: 'FeatureCollection',
  131. }),
  132. chartVizType: selectedChart.viz_type,
  133. chartSize,
  134. layerConfigs,
  135. mapView,
  136. chartBackgroundColor: formData.chartBackgroundColor,
  137. chartBackgroundBorderRadius: formData.chartBackgroundBorderRadius,
  138. }),
  139. );
  140. });
  141. });