transformProps.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 transformProps from '../../src/Tree/transformProps';
  21. import { EchartsTreeChartProps } from '../../src/Tree/types';
  22. describe('EchartsTree transformProps', () => {
  23. const formData = {
  24. colorScheme: 'bnbColors',
  25. datasource: '3__table',
  26. granularity_sqla: 'ds',
  27. metric: 'count',
  28. id: 'id_column',
  29. parent: 'relation_column',
  30. name: 'name_column',
  31. rootNodeId: '1',
  32. };
  33. const chartPropsConfig = {
  34. formData,
  35. width: 800,
  36. height: 600,
  37. theme: supersetTheme,
  38. };
  39. it('should transform when parent present before child', () => {
  40. const queriesData = [
  41. {
  42. colnames: ['id_column', 'relation_column', 'name_column', 'count'],
  43. data: [
  44. {
  45. id_column: '1',
  46. relation_column: null,
  47. name_column: 'root',
  48. count: 10,
  49. },
  50. {
  51. id_column: '2',
  52. relation_column: '1',
  53. name_column: 'first_child',
  54. count: 10,
  55. },
  56. {
  57. id_column: '3',
  58. relation_column: '2',
  59. name_column: 'second_child',
  60. count: 10,
  61. },
  62. {
  63. id_column: '4',
  64. relation_column: '3',
  65. name_column: 'third_child',
  66. count: 10,
  67. },
  68. ],
  69. },
  70. ];
  71. const chartProps = new ChartProps({ ...chartPropsConfig, queriesData });
  72. expect(transformProps(chartProps as EchartsTreeChartProps)).toEqual(
  73. expect.objectContaining({
  74. width: 800,
  75. height: 600,
  76. echartOptions: expect.objectContaining({
  77. series: expect.arrayContaining([
  78. expect.objectContaining({
  79. data: [
  80. {
  81. name: 'root',
  82. children: [
  83. {
  84. name: 'first_child',
  85. value: 10,
  86. children: [
  87. {
  88. name: 'second_child',
  89. value: 10,
  90. children: [
  91. { name: 'third_child', value: 10, children: [] },
  92. ],
  93. },
  94. ],
  95. },
  96. ],
  97. },
  98. ],
  99. }),
  100. ]),
  101. }),
  102. }),
  103. );
  104. });
  105. it('should transform when child is present before parent', () => {
  106. const queriesData = [
  107. {
  108. colnames: ['id_column', 'relation_column', 'name_column', 'count'],
  109. data: [
  110. {
  111. id_column: '1',
  112. relation_column: null,
  113. name_column: 'root',
  114. count: 10,
  115. },
  116. {
  117. id_column: '2',
  118. relation_column: '4',
  119. name_column: 'second_child',
  120. count: 20,
  121. },
  122. {
  123. id_column: '3',
  124. relation_column: '4',
  125. name_column: 'second_child',
  126. count: 30,
  127. },
  128. {
  129. id_column: '4',
  130. relation_column: '1',
  131. name_column: 'first_child',
  132. count: 40,
  133. },
  134. ],
  135. },
  136. ];
  137. const chartProps = new ChartProps({ ...chartPropsConfig, queriesData });
  138. expect(transformProps(chartProps as EchartsTreeChartProps)).toEqual(
  139. expect.objectContaining({
  140. width: 800,
  141. height: 600,
  142. echartOptions: expect.objectContaining({
  143. series: expect.arrayContaining([
  144. expect.objectContaining({
  145. data: [
  146. {
  147. name: 'root',
  148. children: [
  149. {
  150. name: 'first_child',
  151. value: 40,
  152. children: [
  153. {
  154. name: 'second_child',
  155. value: 20,
  156. children: [],
  157. },
  158. {
  159. name: 'second_child',
  160. value: 30,
  161. children: [],
  162. },
  163. ],
  164. },
  165. ],
  166. },
  167. ],
  168. }),
  169. ]),
  170. }),
  171. }),
  172. );
  173. });
  174. it('ignore node if not attached to root', () => {
  175. const formData = {
  176. colorScheme: 'bnbColors',
  177. datasource: '3__table',
  178. granularity_sqla: 'ds',
  179. metric: 'count',
  180. id: 'id_column',
  181. parent: 'relation_column',
  182. name: 'name_column',
  183. rootNodeId: '2',
  184. };
  185. const chartPropsConfig = {
  186. formData,
  187. width: 800,
  188. height: 600,
  189. theme: supersetTheme,
  190. };
  191. const queriesData = [
  192. {
  193. colnames: ['id_column', 'relation_column', 'name_column', 'count'],
  194. data: [
  195. {
  196. id_column: '1',
  197. relation_column: null,
  198. name_column: 'root',
  199. count: 10,
  200. },
  201. {
  202. id_column: '2',
  203. relation_column: '1',
  204. name_column: 'first_child',
  205. count: 10,
  206. },
  207. {
  208. id_column: '3',
  209. relation_column: '2',
  210. name_column: 'second_child',
  211. count: 10,
  212. },
  213. {
  214. id_column: '4',
  215. relation_column: '3',
  216. name_column: 'third_child',
  217. count: 20,
  218. },
  219. ],
  220. },
  221. ];
  222. const chartProps = new ChartProps({ ...chartPropsConfig, queriesData });
  223. expect(transformProps(chartProps as EchartsTreeChartProps)).toEqual(
  224. expect.objectContaining({
  225. width: 800,
  226. height: 600,
  227. echartOptions: expect.objectContaining({
  228. series: expect.arrayContaining([
  229. expect.objectContaining({
  230. data: [
  231. {
  232. name: 'first_child',
  233. children: [
  234. {
  235. name: 'second_child',
  236. value: 10,
  237. children: [
  238. {
  239. name: 'third_child',
  240. value: 20,
  241. children: [],
  242. },
  243. ],
  244. },
  245. ],
  246. },
  247. ],
  248. }),
  249. ]),
  250. }),
  251. }),
  252. );
  253. });
  254. it('should transform props if name column is not specified', () => {
  255. const formData = {
  256. colorScheme: 'bnbColors',
  257. datasource: '3__table',
  258. granularity_sqla: 'ds',
  259. metric: 'count',
  260. id: 'id_column',
  261. parent: 'relation_column',
  262. rootNodeId: '1',
  263. };
  264. const chartPropsConfig = {
  265. formData,
  266. width: 800,
  267. height: 600,
  268. theme: supersetTheme,
  269. };
  270. const queriesData = [
  271. {
  272. colnames: ['id_column', 'relation_column', 'count'],
  273. data: [
  274. {
  275. id_column: '1',
  276. relation_column: null,
  277. count: 10,
  278. },
  279. {
  280. id_column: '2',
  281. relation_column: '1',
  282. count: 10,
  283. },
  284. {
  285. id_column: '3',
  286. relation_column: '2',
  287. count: 10,
  288. },
  289. {
  290. id_column: '4',
  291. relation_column: '3',
  292. count: 20,
  293. },
  294. ],
  295. },
  296. ];
  297. const chartProps = new ChartProps({ ...chartPropsConfig, queriesData });
  298. expect(transformProps(chartProps as EchartsTreeChartProps)).toEqual(
  299. expect.objectContaining({
  300. width: 800,
  301. height: 600,
  302. echartOptions: expect.objectContaining({
  303. series: expect.arrayContaining([
  304. expect.objectContaining({
  305. data: [
  306. {
  307. name: '1',
  308. children: [
  309. {
  310. name: '2',
  311. value: 10,
  312. children: [
  313. {
  314. name: '3',
  315. value: 10,
  316. children: [
  317. {
  318. name: '4',
  319. value: 20,
  320. children: [],
  321. },
  322. ],
  323. },
  324. ],
  325. },
  326. ],
  327. },
  328. ],
  329. }),
  330. ]),
  331. }),
  332. }),
  333. );
  334. });
  335. it('should find root node with null parent when root node name is not provided', () => {
  336. const formData = {
  337. colorScheme: 'bnbColors',
  338. datasource: '3__table',
  339. granularity_sqla: 'ds',
  340. metric: 'count',
  341. id: 'id_column',
  342. parent: 'relation_column',
  343. name: 'name_column',
  344. };
  345. const chartPropsConfig = {
  346. formData,
  347. width: 800,
  348. height: 600,
  349. theme: supersetTheme,
  350. };
  351. const queriesData = [
  352. {
  353. colnames: ['id_column', 'relation_column', 'name_column', 'count'],
  354. data: [
  355. {
  356. id_column: '2',
  357. relation_column: '4',
  358. name_column: 'second_child',
  359. count: 20,
  360. },
  361. {
  362. id_column: '3',
  363. relation_column: '4',
  364. name_column: 'second_child',
  365. count: 30,
  366. },
  367. {
  368. id_column: '4',
  369. relation_column: '1',
  370. name_column: 'first_child',
  371. count: 40,
  372. },
  373. {
  374. id_column: '1',
  375. relation_column: null,
  376. name_column: 'root',
  377. count: 10,
  378. },
  379. ],
  380. },
  381. ];
  382. const chartProps = new ChartProps({ ...chartPropsConfig, queriesData });
  383. expect(transformProps(chartProps as EchartsTreeChartProps)).toEqual(
  384. expect.objectContaining({
  385. width: 800,
  386. height: 600,
  387. echartOptions: expect.objectContaining({
  388. series: expect.arrayContaining([
  389. expect.objectContaining({
  390. data: [
  391. {
  392. name: 'root',
  393. children: [
  394. {
  395. name: 'first_child',
  396. value: 40,
  397. children: [
  398. {
  399. name: 'second_child',
  400. value: 20,
  401. children: [],
  402. },
  403. {
  404. name: 'second_child',
  405. value: 30,
  406. children: [],
  407. },
  408. ],
  409. },
  410. ],
  411. },
  412. ],
  413. }),
  414. ]),
  415. }),
  416. }),
  417. );
  418. });
  419. });