transformProps.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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, VizType } from '@superset-ui/core';
  20. import {
  21. LegendOrientation,
  22. LegendType,
  23. EchartsTimeseriesSeriesType,
  24. } from '../../src';
  25. import transformProps from '../../src/MixedTimeseries/transformProps';
  26. import {
  27. EchartsMixedTimeseriesFormData,
  28. EchartsMixedTimeseriesProps,
  29. } from '../../src/MixedTimeseries/types';
  30. const formData: EchartsMixedTimeseriesFormData = {
  31. annotationLayers: [],
  32. area: false,
  33. areaB: false,
  34. legendMargin: null,
  35. logAxis: false,
  36. logAxisSecondary: false,
  37. markerEnabled: false,
  38. markerEnabledB: false,
  39. markerSize: 0,
  40. markerSizeB: 0,
  41. minorSplitLine: false,
  42. minorTicks: false,
  43. opacity: 0,
  44. opacityB: 0,
  45. orderDesc: false,
  46. orderDescB: false,
  47. richTooltip: false,
  48. rowLimit: 0,
  49. rowLimitB: 0,
  50. legendOrientation: LegendOrientation.Top,
  51. legendType: LegendType.Scroll,
  52. showLegend: false,
  53. showValue: false,
  54. showValueB: false,
  55. stack: true,
  56. stackB: true,
  57. truncateYAxis: false,
  58. truncateYAxisSecondary: false,
  59. xAxisLabelRotation: 0,
  60. xAxisTitle: '',
  61. xAxisTitleMargin: 0,
  62. yAxisBounds: [undefined, undefined],
  63. yAxisBoundsSecondary: [undefined, undefined],
  64. yAxisTitle: '',
  65. yAxisTitleMargin: 0,
  66. yAxisTitlePosition: '',
  67. yAxisTitleSecondary: '',
  68. zoomable: false,
  69. colorScheme: 'bnbColors',
  70. datasource: '3__table',
  71. x_axis: 'ds',
  72. metrics: ['sum__num'],
  73. metricsB: ['sum__num'],
  74. groupby: ['gender'],
  75. groupbyB: ['gender'],
  76. seriesType: EchartsTimeseriesSeriesType.Line,
  77. seriesTypeB: EchartsTimeseriesSeriesType.Bar,
  78. viz_type: VizType.MixedTimeseries,
  79. forecastEnabled: false,
  80. forecastPeriods: [],
  81. forecastInterval: 0,
  82. forecastSeasonalityDaily: 0,
  83. legendSort: null,
  84. };
  85. const queriesData = [
  86. {
  87. data: [
  88. { boy: 1, girl: 2, ds: 599616000000 },
  89. { boy: 3, girl: 4, ds: 599916000000 },
  90. ],
  91. label_map: {
  92. ds: ['ds'],
  93. boy: ['boy'],
  94. girl: ['girl'],
  95. },
  96. },
  97. {
  98. data: [
  99. { boy: 1, girl: 2, ds: 599616000000 },
  100. { boy: 3, girl: 4, ds: 599916000000 },
  101. ],
  102. label_map: {
  103. ds: ['ds'],
  104. boy: ['boy'],
  105. girl: ['girl'],
  106. },
  107. },
  108. ];
  109. const chartPropsConfig = {
  110. formData,
  111. width: 800,
  112. height: 600,
  113. queriesData,
  114. theme: supersetTheme,
  115. };
  116. test('should transform chart props for viz with showQueryIdentifiers=false', () => {
  117. const chartPropsConfigWithoutIdentifiers = {
  118. ...chartPropsConfig,
  119. formData: {
  120. ...formData,
  121. showQueryIdentifiers: false,
  122. },
  123. };
  124. const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
  125. const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
  126. // Check that series IDs don't include query identifiers
  127. const seriesIds = (transformed.echartOptions.series as any[]).map(
  128. (s: any) => s.id,
  129. );
  130. expect(seriesIds).toContain('sum__num, girl');
  131. expect(seriesIds).toContain('sum__num, boy');
  132. expect(seriesIds).not.toContain('sum__num (Query A), girl');
  133. expect(seriesIds).not.toContain('sum__num (Query A), boy');
  134. expect(seriesIds).not.toContain('sum__num (Query B), girl');
  135. expect(seriesIds).not.toContain('sum__num (Query B), boy');
  136. // Check that series name include query identifiers
  137. const seriesName = (transformed.echartOptions.series as any[]).map(
  138. (s: any) => s.name,
  139. );
  140. expect(seriesName).toContain('sum__num, girl');
  141. expect(seriesName).toContain('sum__num, boy');
  142. expect(seriesName).not.toContain('sum__num (Query A), girl');
  143. expect(seriesName).not.toContain('sum__num (Query A), boy');
  144. expect(seriesName).not.toContain('sum__num (Query B), girl');
  145. expect(seriesName).not.toContain('sum__num (Query B), boy');
  146. expect((transformed.echartOptions.legend as any).data).toEqual([
  147. 'sum__num, girl',
  148. 'sum__num, boy',
  149. 'sum__num, girl',
  150. 'sum__num, boy',
  151. ]);
  152. });
  153. test('should transform chart props for viz with showQueryIdentifiers=true', () => {
  154. const chartPropsConfigWithIdentifiers = {
  155. ...chartPropsConfig,
  156. formData: {
  157. ...formData,
  158. showQueryIdentifiers: true,
  159. },
  160. };
  161. const chartProps = new ChartProps(chartPropsConfigWithIdentifiers);
  162. const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
  163. // Check that series IDs include query identifiers
  164. const seriesIds = (transformed.echartOptions.series as any[]).map(
  165. (s: any) => s.id,
  166. );
  167. expect(seriesIds).toContain('sum__num (Query A), girl');
  168. expect(seriesIds).toContain('sum__num (Query A), boy');
  169. expect(seriesIds).toContain('sum__num (Query B), girl');
  170. expect(seriesIds).toContain('sum__num (Query B), boy');
  171. expect(seriesIds).not.toContain('sum__num, girl');
  172. expect(seriesIds).not.toContain('sum__num, boy');
  173. // Check that series name include query identifiers
  174. const seriesName = (transformed.echartOptions.series as any[]).map(
  175. (s: any) => s.name,
  176. );
  177. expect(seriesName).toContain('sum__num (Query A), girl');
  178. expect(seriesName).toContain('sum__num (Query A), boy');
  179. expect(seriesName).toContain('sum__num (Query B), girl');
  180. expect(seriesName).toContain('sum__num (Query B), boy');
  181. expect(seriesName).not.toContain('sum__num, girl');
  182. expect(seriesName).not.toContain('sum__num, boy');
  183. expect((transformed.echartOptions.legend as any).data).toEqual([
  184. 'sum__num (Query A), girl',
  185. 'sum__num (Query A), boy',
  186. 'sum__num (Query B), girl',
  187. 'sum__num (Query B), boy',
  188. ]);
  189. });
  190. describe('legend sorting', () => {
  191. const getChartProps = (overrides = {}) =>
  192. new ChartProps({
  193. ...chartPropsConfig,
  194. formData: {
  195. ...formData,
  196. ...overrides,
  197. showQueryIdentifiers: true,
  198. },
  199. });
  200. it('sort legend by data', () => {
  201. const chartProps = getChartProps({
  202. legendSort: null,
  203. });
  204. const transformed = transformProps(
  205. chartProps as EchartsMixedTimeseriesProps,
  206. );
  207. expect((transformed.echartOptions.legend as any).data).toEqual([
  208. 'sum__num (Query A), girl',
  209. 'sum__num (Query A), boy',
  210. 'sum__num (Query B), girl',
  211. 'sum__num (Query B), boy',
  212. ]);
  213. });
  214. it('sort legend by label ascending', () => {
  215. const chartProps = getChartProps({
  216. legendSort: 'asc',
  217. });
  218. const transformed = transformProps(
  219. chartProps as EchartsMixedTimeseriesProps,
  220. );
  221. expect((transformed.echartOptions.legend as any).data).toEqual([
  222. 'sum__num (Query A), boy',
  223. 'sum__num (Query A), girl',
  224. 'sum__num (Query B), boy',
  225. 'sum__num (Query B), girl',
  226. ]);
  227. });
  228. it('sort legend by label descending', () => {
  229. const chartProps = getChartProps({
  230. legendSort: 'desc',
  231. });
  232. const transformed = transformProps(
  233. chartProps as EchartsMixedTimeseriesProps,
  234. );
  235. expect((transformed.echartOptions.legend as any).data).toEqual([
  236. 'sum__num (Query B), girl',
  237. 'sum__num (Query B), boy',
  238. 'sum__num (Query A), girl',
  239. 'sum__num (Query A), boy',
  240. ]);
  241. });
  242. });
  243. test('legend margin: top orientation sets grid.top correctly', () => {
  244. const chartPropsConfigWithoutIdentifiers = {
  245. ...chartPropsConfig,
  246. formData: {
  247. ...formData,
  248. legendMargin: 250,
  249. showLegend: true,
  250. },
  251. };
  252. const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
  253. const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
  254. expect((transformed.echartOptions.grid as any).top).toEqual(270);
  255. });
  256. test('legend margin: bottom orientation sets grid.bottom correctly', () => {
  257. const chartPropsConfigWithoutIdentifiers = {
  258. ...chartPropsConfig,
  259. formData: {
  260. ...formData,
  261. legendMargin: 250,
  262. showLegend: true,
  263. legendOrientation: LegendOrientation.Bottom,
  264. },
  265. };
  266. const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
  267. const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
  268. expect((transformed.echartOptions.grid as any).bottom).toEqual(270);
  269. });
  270. test('legend margin: left orientation sets grid.left correctly', () => {
  271. const chartPropsConfigWithoutIdentifiers = {
  272. ...chartPropsConfig,
  273. formData: {
  274. ...formData,
  275. legendMargin: 250,
  276. showLegend: true,
  277. legendOrientation: LegendOrientation.Left,
  278. },
  279. };
  280. const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
  281. const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
  282. expect((transformed.echartOptions.grid as any).left).toEqual(270);
  283. });
  284. test('legend margin: right orientation sets grid.right correctly', () => {
  285. const chartPropsConfigWithoutIdentifiers = {
  286. ...chartPropsConfig,
  287. formData: {
  288. ...formData,
  289. legendMargin: 270,
  290. showLegend: true,
  291. legendOrientation: LegendOrientation.Right,
  292. },
  293. };
  294. const chartProps = new ChartProps(chartPropsConfigWithoutIdentifiers);
  295. const transformed = transformProps(chartProps as EchartsMixedTimeseriesProps);
  296. expect((transformed.echartOptions.grid as any).right).toEqual(270);
  297. });