transformProps.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 { ChartProps, supersetTheme } from '@superset-ui/core';
  20. import {
  21. EchartsWaterfallChartProps,
  22. WaterfallChartTransformedProps,
  23. } from '../../src/Waterfall/types';
  24. import transformProps from '../../src/Waterfall/transformProps';
  25. const extractSeries = (props: WaterfallChartTransformedProps) => {
  26. const { echartOptions } = props;
  27. const { series } = echartOptions as unknown as {
  28. series: [{ data: [{ value: number }] }];
  29. };
  30. return series.map(item => item.data).map(item => item.map(i => i.value));
  31. };
  32. const extractSeriesName = (props: WaterfallChartTransformedProps) => {
  33. const { echartOptions } = props;
  34. const { series } = echartOptions as unknown as {
  35. series: [{ name: string }];
  36. };
  37. return series.map(item => item.name);
  38. };
  39. const data = [
  40. { year: '2019', name: 'Sylvester', sum: 10 },
  41. { year: '2019', name: 'Arnold', sum: 3 },
  42. { year: '2020', name: 'Sylvester', sum: -10 },
  43. { year: '2020', name: 'Arnold', sum: 5 },
  44. ];
  45. const formData = {
  46. colorScheme: 'bnbColors',
  47. datasource: '3__table',
  48. x_axis: 'year',
  49. metric: 'sum',
  50. increaseColor: { r: 0, b: 0, g: 0 },
  51. decreaseColor: { r: 0, b: 0, g: 0 },
  52. totalColor: { r: 0, b: 0, g: 0 },
  53. showTotal: true,
  54. };
  55. test('should tranform chart props for viz when breakdown not exist', () => {
  56. const chartProps = new ChartProps({
  57. formData: { ...formData, series: 'bar' },
  58. width: 800,
  59. height: 600,
  60. queriesData: [
  61. {
  62. data,
  63. },
  64. ],
  65. theme: supersetTheme,
  66. });
  67. const transformedProps = transformProps(
  68. chartProps as unknown as EchartsWaterfallChartProps,
  69. );
  70. expect(extractSeries(transformedProps)).toEqual([
  71. [0, 8, '-'],
  72. [13, '-', '-'],
  73. ['-', 5, '-'],
  74. ['-', '-', 8],
  75. ]);
  76. });
  77. test('should tranform chart props for viz when breakdown exist', () => {
  78. const chartProps = new ChartProps({
  79. formData: { ...formData, groupby: 'name' },
  80. width: 800,
  81. height: 600,
  82. queriesData: [
  83. {
  84. data,
  85. },
  86. ],
  87. theme: supersetTheme,
  88. });
  89. const transformedProps = transformProps(
  90. chartProps as unknown as EchartsWaterfallChartProps,
  91. );
  92. expect(extractSeries(transformedProps)).toEqual([
  93. [0, 10, '-', 3, 3, '-'],
  94. [10, 3, '-', '-', 5, '-'],
  95. ['-', '-', '-', 10, '-', '-'],
  96. ['-', '-', 13, '-', '-', 8],
  97. ]);
  98. });
  99. test('renaming series names, checking legend and X axis labels', () => {
  100. const chartProps = new ChartProps({
  101. formData: {
  102. ...formData,
  103. increaseLabel: 'sale increase',
  104. decreaseLabel: 'sale decrease',
  105. totalLabel: 'sale total',
  106. },
  107. width: 800,
  108. height: 600,
  109. queriesData: [
  110. {
  111. data,
  112. },
  113. ],
  114. theme: supersetTheme,
  115. });
  116. const transformedProps = transformProps(
  117. chartProps as unknown as EchartsWaterfallChartProps,
  118. );
  119. expect((transformedProps.echartOptions.legend as any).data).toEqual([
  120. 'sale increase',
  121. 'sale decrease',
  122. 'sale total',
  123. ]);
  124. expect((transformedProps.echartOptions.xAxis as any).data).toEqual([
  125. '2019',
  126. '2020',
  127. 'sale total',
  128. ]);
  129. expect(extractSeriesName(transformedProps)).toEqual([
  130. 'Assist',
  131. 'sale increase',
  132. 'sale decrease',
  133. 'sale total',
  134. ]);
  135. });
  136. test('hide totals', () => {
  137. const chartProps = new ChartProps({
  138. formData: { ...formData, series: 'bar', showTotal: false },
  139. width: 800,
  140. height: 600,
  141. queriesData: [
  142. {
  143. data,
  144. },
  145. ],
  146. theme: supersetTheme,
  147. });
  148. const transformedProps = transformProps(
  149. chartProps as unknown as EchartsWaterfallChartProps,
  150. );
  151. expect(extractSeries(transformedProps)).toEqual([
  152. [0, 8],
  153. [13, '-'],
  154. ['-', 5],
  155. ['-', '-'],
  156. ]);
  157. });