transformProps.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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, SqlaFormData, supersetTheme } from '@superset-ui/core';
  20. import transformProps from '../../src/Graph/transformProps';
  21. import { DEFAULT_GRAPH_SERIES_OPTION } from '../../src/Graph/constants';
  22. import { EchartsGraphChartProps } from '../../src/Graph/types';
  23. const formData: SqlaFormData = {
  24. colorScheme: 'bnbColors',
  25. datasource: '3__table',
  26. granularity_sqla: 'ds',
  27. metric: 'count',
  28. source: 'source_column',
  29. target: 'target_column',
  30. category: null,
  31. viz_type: 'graph',
  32. };
  33. const queriesData = [
  34. {
  35. colnames: ['source_column', 'target_column', 'count'],
  36. data: [
  37. {
  38. source_column: 'source_value_1',
  39. target_column: 'target_value_1',
  40. count: 6,
  41. },
  42. {
  43. source_column: 'source_value_2',
  44. target_column: 'target_value_2',
  45. count: 5,
  46. },
  47. ],
  48. },
  49. ];
  50. const chartPropsConfig = {
  51. formData,
  52. width: 800,
  53. height: 600,
  54. queriesData,
  55. theme: supersetTheme,
  56. };
  57. describe('EchartsGraph transformProps', () => {
  58. it('should transform chart props for viz without category', () => {
  59. const chartProps = new ChartProps(chartPropsConfig);
  60. expect(transformProps(chartProps as EchartsGraphChartProps)).toEqual(
  61. expect.objectContaining({
  62. width: 800,
  63. height: 600,
  64. echartOptions: expect.objectContaining({
  65. legend: expect.objectContaining({
  66. data: [],
  67. }),
  68. series: expect.arrayContaining([
  69. expect.objectContaining({
  70. data: [
  71. {
  72. col: 'source_column',
  73. category: undefined,
  74. id: '0',
  75. itemStyle: {
  76. color: '#1f77b4',
  77. },
  78. label: { show: true },
  79. name: 'source_value_1',
  80. select: {
  81. itemStyle: { borderWidth: 3, opacity: 1 },
  82. label: { fontWeight: 'bolder' },
  83. },
  84. symbolSize: 50,
  85. tooltip: expect.anything(),
  86. value: 6,
  87. },
  88. {
  89. col: 'target_column',
  90. category: undefined,
  91. id: '1',
  92. itemStyle: {
  93. color: '#1f77b4',
  94. },
  95. label: { show: true },
  96. name: 'target_value_1',
  97. select: {
  98. itemStyle: { borderWidth: 3, opacity: 1 },
  99. label: { fontWeight: 'bolder' },
  100. },
  101. symbolSize: 50,
  102. tooltip: expect.anything(),
  103. value: 6,
  104. },
  105. {
  106. col: 'source_column',
  107. category: undefined,
  108. id: '2',
  109. itemStyle: {
  110. color: '#1f77b4',
  111. },
  112. label: { show: true },
  113. name: 'source_value_2',
  114. select: {
  115. itemStyle: { borderWidth: 3, opacity: 1 },
  116. label: { fontWeight: 'bolder' },
  117. },
  118. symbolSize: 10,
  119. tooltip: expect.anything(),
  120. value: 5,
  121. },
  122. {
  123. col: 'target_column',
  124. category: undefined,
  125. id: '3',
  126. itemStyle: {
  127. color: '#1f77b4',
  128. },
  129. label: { show: true },
  130. name: 'target_value_2',
  131. select: {
  132. itemStyle: { borderWidth: 3, opacity: 1 },
  133. label: { fontWeight: 'bolder' },
  134. },
  135. symbolSize: 10,
  136. tooltip: expect.anything(),
  137. value: 5,
  138. },
  139. ],
  140. }),
  141. expect.objectContaining({
  142. links: [
  143. {
  144. emphasis: { lineStyle: { width: 12 } },
  145. lineStyle: { width: 6, color: '#1f77b4' },
  146. select: {
  147. lineStyle: { opacity: 1, width: 9.600000000000001 },
  148. },
  149. source: '0',
  150. target: '1',
  151. value: 6,
  152. },
  153. {
  154. emphasis: { lineStyle: { width: 5 } },
  155. lineStyle: { width: 1.5, color: '#1f77b4' },
  156. select: { lineStyle: { opacity: 1, width: 5 } },
  157. source: '2',
  158. target: '3',
  159. value: 5,
  160. },
  161. ],
  162. }),
  163. ]),
  164. }),
  165. }),
  166. );
  167. });
  168. it('should transform chart props for viz with category and falsy normalization', () => {
  169. const formData: SqlaFormData = {
  170. colorScheme: 'bnbColors',
  171. datasource: '3__table',
  172. granularity_sqla: 'ds',
  173. metric: 'count',
  174. source: 'source_column',
  175. target: 'target_column',
  176. sourceCategory: 'source_category_column',
  177. targetCategory: 'target_category_column',
  178. viz_type: 'graph',
  179. };
  180. const queriesData = [
  181. {
  182. colnames: [
  183. 'source_column',
  184. 'target_column',
  185. 'source_category_column',
  186. 'target_category_column',
  187. 'count',
  188. ],
  189. data: [
  190. {
  191. source_column: 'source_value',
  192. target_column: 'target_value',
  193. source_category_column: 'category_value_1',
  194. target_category_column: 'category_value_2',
  195. count: 6,
  196. },
  197. {
  198. source_column: 'source_value',
  199. target_column: 'target_value',
  200. source_category_column: 'category_value_1',
  201. target_category_column: 'category_value_2',
  202. count: 5,
  203. },
  204. ],
  205. },
  206. ];
  207. const chartPropsConfig = {
  208. formData,
  209. width: 800,
  210. height: 600,
  211. queriesData,
  212. theme: supersetTheme,
  213. };
  214. const chartProps = new ChartProps(chartPropsConfig);
  215. expect(transformProps(chartProps as EchartsGraphChartProps)).toEqual(
  216. expect.objectContaining({
  217. width: 800,
  218. height: 600,
  219. echartOptions: expect.objectContaining({
  220. legend: expect.objectContaining({
  221. data: ['category_value_1', 'category_value_2'],
  222. }),
  223. series: expect.arrayContaining([
  224. expect.objectContaining({
  225. data: [
  226. {
  227. id: '0',
  228. itemStyle: {
  229. color: '#1f77b4',
  230. },
  231. col: 'source_column',
  232. name: 'source_value',
  233. value: 11,
  234. symbolSize: 10,
  235. category: 'category_value_1',
  236. select: DEFAULT_GRAPH_SERIES_OPTION.select,
  237. tooltip: expect.anything(),
  238. label: { show: true },
  239. },
  240. {
  241. id: '1',
  242. itemStyle: {
  243. color: '#ff7f0e',
  244. },
  245. col: 'target_column',
  246. name: 'target_value',
  247. value: 11,
  248. symbolSize: 10,
  249. category: 'category_value_2',
  250. select: DEFAULT_GRAPH_SERIES_OPTION.select,
  251. tooltip: expect.anything(),
  252. label: { show: true },
  253. },
  254. ],
  255. }),
  256. ]),
  257. }),
  258. }),
  259. );
  260. });
  261. });
  262. describe('legend sorting', () => {
  263. const queriesData = [
  264. {
  265. colnames: [
  266. 'source_column',
  267. 'target_column',
  268. 'source_category_column',
  269. 'target_category_column',
  270. 'count',
  271. ],
  272. data: [
  273. {
  274. source_column: 'source_value',
  275. target_column: 'target_value',
  276. source_category_column: 'category_value_1',
  277. target_category_column: 'category_value_3',
  278. count: 6,
  279. },
  280. {
  281. source_column: 'source_value',
  282. target_column: 'target_value',
  283. source_category_column: 'category_value_3',
  284. target_category_column: 'category_value_2',
  285. count: 5,
  286. },
  287. {
  288. source_column: 'source_value',
  289. target_column: 'target_value',
  290. source_category_column: 'category_value_2',
  291. target_category_column: 'category_value_1',
  292. count: 4,
  293. },
  294. ],
  295. },
  296. ];
  297. const getChartProps = (overrides = {}) =>
  298. new ChartProps({
  299. ...chartPropsConfig,
  300. formData: {
  301. ...formData,
  302. ...overrides,
  303. sourceCategory: 'source_category_column',
  304. targetCategory: 'target_category_column',
  305. },
  306. queriesData,
  307. });
  308. it('sort legend by data', () => {
  309. const chartProps = getChartProps({
  310. legendSort: null,
  311. });
  312. const transformed = transformProps(chartProps as EchartsGraphChartProps);
  313. expect((transformed.echartOptions.legend as any).data).toEqual([
  314. 'category_value_1',
  315. 'category_value_3',
  316. 'category_value_2',
  317. ]);
  318. });
  319. it('sort legend by label ascending', () => {
  320. const chartProps = getChartProps({
  321. legendSort: 'asc',
  322. });
  323. const transformed = transformProps(chartProps as EchartsGraphChartProps);
  324. expect((transformed.echartOptions.legend as any).data).toEqual([
  325. 'category_value_1',
  326. 'category_value_2',
  327. 'category_value_3',
  328. ]);
  329. });
  330. it('sort legend by label descending', () => {
  331. const chartProps = getChartProps({
  332. legendSort: 'desc',
  333. });
  334. const transformed = transformProps(chartProps as EchartsGraphChartProps);
  335. expect((transformed.echartOptions.legend as any).data).toEqual([
  336. 'category_value_3',
  337. 'category_value_2',
  338. 'category_value_1',
  339. ]);
  340. });
  341. });