transformProps.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. getNumberFormatter,
  22. supersetTheme,
  23. } from '@superset-ui/core';
  24. import transformProps, { parseParams } from '../../src/Funnel/transformProps';
  25. import {
  26. EchartsFunnelChartProps,
  27. PercentCalcType,
  28. } from '../../src/Funnel/types';
  29. const formData = {
  30. colorScheme: 'bnbColors',
  31. datasource: '3__table',
  32. granularity_sqla: 'ds',
  33. metric: 'sum__num',
  34. groupby: ['foo', 'bar'],
  35. };
  36. const queriesData = [
  37. {
  38. data: [
  39. { foo: 'Sylvester', bar: 1, sum__num: 10 },
  40. { foo: 'Arnold', bar: 2, sum__num: 2.5 },
  41. ],
  42. },
  43. ];
  44. const chartProps = new ChartProps({
  45. formData,
  46. width: 800,
  47. height: 600,
  48. queriesData,
  49. theme: supersetTheme,
  50. });
  51. describe('Funnel transformProps', () => {
  52. it('should transform chart props for viz', () => {
  53. expect(transformProps(chartProps as EchartsFunnelChartProps)).toEqual(
  54. expect.objectContaining({
  55. width: 800,
  56. height: 600,
  57. echartOptions: expect.objectContaining({
  58. series: [
  59. expect.objectContaining({
  60. data: expect.arrayContaining([
  61. expect.objectContaining({
  62. name: 'Arnold, 2',
  63. value: 2.5,
  64. }),
  65. expect.objectContaining({
  66. name: 'Sylvester, 1',
  67. value: 10,
  68. }),
  69. ]),
  70. }),
  71. ],
  72. }),
  73. }),
  74. );
  75. });
  76. });
  77. describe('formatFunnelLabel', () => {
  78. it('should generate a valid funnel chart label', () => {
  79. const numberFormatter = getNumberFormatter();
  80. const params = {
  81. name: 'My Label',
  82. value: 1234,
  83. percent: 12.34,
  84. data: { firstStepPercent: 0.5, prevStepPercent: 0.85 },
  85. };
  86. expect(
  87. parseParams({
  88. params,
  89. numberFormatter,
  90. percentCalculationType: PercentCalcType.Total,
  91. }),
  92. ).toEqual(['My Label', '1.23k', '12.34%']);
  93. expect(
  94. parseParams({
  95. params,
  96. numberFormatter,
  97. percentCalculationType: PercentCalcType.FirstStep,
  98. }),
  99. ).toEqual(['My Label', '1.23k', '50.00%']);
  100. expect(
  101. parseParams({
  102. params,
  103. numberFormatter,
  104. percentCalculationType: PercentCalcType.PreviousStep,
  105. }),
  106. ).toEqual(['My Label', '1.23k', '85.00%']);
  107. expect(
  108. parseParams({
  109. params: { ...params, name: '<NULL>' },
  110. numberFormatter,
  111. percentCalculationType: PercentCalcType.Total,
  112. }),
  113. ).toEqual(['<NULL>', '1.23k', '12.34%']);
  114. expect(
  115. parseParams({
  116. params: { ...params, name: '<NULL>' },
  117. numberFormatter,
  118. percentCalculationType: PercentCalcType.Total,
  119. sanitizeName: true,
  120. }),
  121. ).toEqual(['&lt;NULL&gt;', '1.23k', '12.34%']);
  122. });
  123. });
  124. describe('legend sorting', () => {
  125. const legendQueriesData = [
  126. {
  127. data: [
  128. { foo: 'Sylvester', sum__num: 10 },
  129. { foo: 'Arnold', sum__num: 2.5 },
  130. { foo: 'Mark', sum__num: 13 },
  131. ],
  132. },
  133. ];
  134. const createChartProps = (overrides = {}) =>
  135. new ChartProps({
  136. ...chartProps,
  137. formData: {
  138. ...formData,
  139. groupby: ['foo'],
  140. ...overrides,
  141. },
  142. queriesData: legendQueriesData,
  143. });
  144. it('preserves original data order when no sort specified', () => {
  145. const props = createChartProps({ legendSort: null });
  146. const result = transformProps(props as EchartsFunnelChartProps);
  147. const legendData = (result.echartOptions.legend as any).data;
  148. expect(legendData).toEqual(['Sylvester', 'Arnold', 'Mark']);
  149. });
  150. it('sorts alphabetically ascending when legendSort is "asc"', () => {
  151. const props = createChartProps({ legendSort: 'asc' });
  152. const result = transformProps(props as EchartsFunnelChartProps);
  153. const legendData = (result.echartOptions.legend as any).data;
  154. expect(legendData).toEqual(['Arnold', 'Mark', 'Sylvester']);
  155. });
  156. it('sorts alphabetically descending when legendSort is "desc"', () => {
  157. const props = createChartProps({ legendSort: 'desc' });
  158. const result = transformProps(props as EchartsFunnelChartProps);
  159. const legendData = (result.echartOptions.legend as any).data;
  160. expect(legendData).toEqual(['Sylvester', 'Mark', 'Arnold']);
  161. });
  162. });