TableChart.test.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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 '@testing-library/jest-dom';
  20. import { render, screen } from '@superset-ui/core/spec';
  21. import TableChart from '../src/TableChart';
  22. import transformProps from '../src/transformProps';
  23. import DateWithFormatter from '../src/utils/DateWithFormatter';
  24. import testData from './testData';
  25. import { ProviderWrapper } from './testHelpers';
  26. describe('plugin-chart-table', () => {
  27. describe('transformProps', () => {
  28. it('should parse pageLength to pageSize', () => {
  29. expect(transformProps(testData.basic).pageSize).toBe(20);
  30. expect(
  31. transformProps({
  32. ...testData.basic,
  33. rawFormData: { ...testData.basic.rawFormData, page_length: '20' },
  34. }).pageSize,
  35. ).toBe(20);
  36. expect(
  37. transformProps({
  38. ...testData.basic,
  39. rawFormData: { ...testData.basic.rawFormData, page_length: '' },
  40. }).pageSize,
  41. ).toBe(0);
  42. });
  43. it('should memoize data records', () => {
  44. expect(transformProps(testData.basic).data).toBe(
  45. transformProps(testData.basic).data,
  46. );
  47. });
  48. it('should memoize columns meta', () => {
  49. expect(transformProps(testData.basic).columns).toBe(
  50. transformProps({
  51. ...testData.basic,
  52. rawFormData: { ...testData.basic.rawFormData, pageLength: null },
  53. }).columns,
  54. );
  55. });
  56. it('should format timestamp', () => {
  57. // eslint-disable-next-line no-underscore-dangle
  58. const parsedDate = transformProps(testData.basic).data[0]
  59. .__timestamp as DateWithFormatter;
  60. expect(String(parsedDate)).toBe('2020-01-01 12:34:56');
  61. expect(parsedDate.getTime()).toBe(1577882096000);
  62. });
  63. it('should process comparison columns when time_compare and comparison_type are set', () => {
  64. const transformedProps = transformProps(testData.comparison);
  65. const comparisonColumns = transformedProps.columns.filter(
  66. col =>
  67. col.originalLabel === 'metric_1' ||
  68. col.originalLabel === 'metric_2' ||
  69. col.label === '#' ||
  70. col.label === '△' ||
  71. col.label === '%',
  72. );
  73. expect(comparisonColumns.length).toBeGreaterThan(0);
  74. expect(
  75. comparisonColumns.some(col => col.originalLabel === 'metric_1'),
  76. ).toBe(true);
  77. expect(
  78. comparisonColumns.some(col => col.originalLabel === 'metric_2'),
  79. ).toBe(true);
  80. expect(comparisonColumns.some(col => col.label === '#')).toBe(true);
  81. expect(comparisonColumns.some(col => col.label === '△')).toBe(true);
  82. expect(comparisonColumns.some(col => col.label === '%')).toBe(true);
  83. });
  84. it('should not process comparison columns when time_compare is empty', () => {
  85. const propsWithoutTimeCompare = {
  86. ...testData.comparison,
  87. rawFormData: {
  88. ...testData.comparison.rawFormData,
  89. time_compare: [],
  90. },
  91. };
  92. const transformedProps = transformProps(propsWithoutTimeCompare);
  93. // Check if comparison columns are not processed
  94. const comparisonColumns = transformedProps.columns.filter(
  95. col =>
  96. col.label === 'Main' ||
  97. col.label === '#' ||
  98. col.label === '△' ||
  99. col.label === '%',
  100. );
  101. expect(comparisonColumns.length).toBe(0);
  102. });
  103. it('should correctly apply column configuration for comparison columns', () => {
  104. const transformedProps = transformProps(testData.comparisonWithConfig);
  105. const comparisonColumns = transformedProps.columns.filter(
  106. col =>
  107. col.key.startsWith('Main') ||
  108. col.key.startsWith('#') ||
  109. col.key.startsWith('△') ||
  110. col.key.startsWith('%'),
  111. );
  112. expect(comparisonColumns).toHaveLength(4);
  113. const mainMetricConfig = comparisonColumns.find(
  114. col => col.key === 'Main metric_1',
  115. );
  116. expect(mainMetricConfig).toBeDefined();
  117. expect(mainMetricConfig?.config).toEqual({ d3NumberFormat: '.2f' });
  118. const hashMetricConfig = comparisonColumns.find(
  119. col => col.key === '# metric_1',
  120. );
  121. expect(hashMetricConfig).toBeDefined();
  122. expect(hashMetricConfig?.config).toEqual({ d3NumberFormat: '.1f' });
  123. const deltaMetricConfig = comparisonColumns.find(
  124. col => col.key === '△ metric_1',
  125. );
  126. expect(deltaMetricConfig).toBeDefined();
  127. expect(deltaMetricConfig?.config).toEqual({ d3NumberFormat: '.0f' });
  128. const percentMetricConfig = comparisonColumns.find(
  129. col => col.key === '% metric_1',
  130. );
  131. expect(percentMetricConfig).toBeDefined();
  132. expect(percentMetricConfig?.config).toEqual({ d3NumberFormat: '.3f' });
  133. });
  134. it('should correctly format comparison columns using getComparisonColFormatter', () => {
  135. const transformedProps = transformProps(testData.comparisonWithConfig);
  136. const comparisonColumns = transformedProps.columns.filter(
  137. col =>
  138. col.key.startsWith('Main') ||
  139. col.key.startsWith('#') ||
  140. col.key.startsWith('△') ||
  141. col.key.startsWith('%'),
  142. );
  143. const formattedMainMetric = comparisonColumns
  144. .find(col => col.key === 'Main metric_1')
  145. ?.formatter?.(12345.678);
  146. expect(formattedMainMetric).toBe('12345.68');
  147. const formattedHashMetric = comparisonColumns
  148. .find(col => col.key === '# metric_1')
  149. ?.formatter?.(12345.678);
  150. expect(formattedHashMetric).toBe('12345.7');
  151. const formattedDeltaMetric = comparisonColumns
  152. .find(col => col.key === '△ metric_1')
  153. ?.formatter?.(12345.678);
  154. expect(formattedDeltaMetric).toBe('12346');
  155. const formattedPercentMetric = comparisonColumns
  156. .find(col => col.key === '% metric_1')
  157. ?.formatter?.(0.123456);
  158. expect(formattedPercentMetric).toBe('0.123');
  159. });
  160. it('should set originalLabel for comparison columns when time_compare and comparison_type are set', () => {
  161. const transformedProps = transformProps(testData.comparison);
  162. // Check if comparison columns are processed
  163. // Now we're looking for columns with metric names as labels
  164. const comparisonColumns = transformedProps.columns.filter(
  165. col =>
  166. col.originalLabel === 'metric_1' ||
  167. col.originalLabel === 'metric_2' ||
  168. col.label === '#' ||
  169. col.label === '△' ||
  170. col.label === '%',
  171. );
  172. expect(comparisonColumns.length).toBeGreaterThan(0);
  173. expect(
  174. comparisonColumns.some(col => col.originalLabel === 'metric_1'),
  175. ).toBe(true);
  176. expect(
  177. comparisonColumns.some(col => col.originalLabel === 'metric_2'),
  178. ).toBe(true);
  179. expect(comparisonColumns.some(col => col.label === '#')).toBe(true);
  180. expect(comparisonColumns.some(col => col.label === '△')).toBe(true);
  181. expect(comparisonColumns.some(col => col.label === '%')).toBe(true);
  182. // Verify originalLabel for metric_1 comparison columns
  183. const metric1Column = transformedProps.columns.find(
  184. col =>
  185. col.originalLabel === 'metric_1' &&
  186. !col.key.startsWith('#') &&
  187. !col.key.startsWith('△') &&
  188. !col.key.startsWith('%'),
  189. );
  190. expect(metric1Column).toBeDefined();
  191. expect(metric1Column?.originalLabel).toBe('metric_1');
  192. expect(metric1Column?.label).toBe('Main');
  193. const hashMetric1 = transformedProps.columns.find(
  194. col => col.key === '# metric_1',
  195. );
  196. expect(hashMetric1).toBeDefined();
  197. expect(hashMetric1?.originalLabel).toBe('metric_1');
  198. const deltaMetric1 = transformedProps.columns.find(
  199. col => col.key === '△ metric_1',
  200. );
  201. expect(deltaMetric1).toBeDefined();
  202. expect(deltaMetric1?.originalLabel).toBe('metric_1');
  203. const percentMetric1 = transformedProps.columns.find(
  204. col => col.key === '% metric_1',
  205. );
  206. expect(percentMetric1).toBeDefined();
  207. expect(percentMetric1?.originalLabel).toBe('metric_1');
  208. // Verify originalLabel for metric_2 comparison columns
  209. const metric2Column = transformedProps.columns.find(
  210. col =>
  211. col.originalLabel === 'metric_2' &&
  212. !col.key.startsWith('#') &&
  213. !col.key.startsWith('△') &&
  214. !col.key.startsWith('%'),
  215. );
  216. expect(metric2Column).toBeDefined();
  217. expect(metric2Column?.originalLabel).toBe('metric_2');
  218. expect(metric2Column?.label).toBe('Main');
  219. const hashMetric2 = transformedProps.columns.find(
  220. col => col.key === '# metric_2',
  221. );
  222. expect(hashMetric2).toBeDefined();
  223. expect(hashMetric2?.originalLabel).toBe('metric_2');
  224. const deltaMetric2 = transformedProps.columns.find(
  225. col => col.key === '△ metric_2',
  226. );
  227. expect(deltaMetric2).toBeDefined();
  228. expect(deltaMetric2?.originalLabel).toBe('metric_2');
  229. const percentMetric2 = transformedProps.columns.find(
  230. col => col.key === '% metric_2',
  231. );
  232. expect(percentMetric2).toBeDefined();
  233. expect(percentMetric2?.originalLabel).toBe('metric_2');
  234. });
  235. describe('TableChart', () => {
  236. it('render basic data', () => {
  237. render(
  238. <TableChart {...transformProps(testData.basic)} sticky={false} />,
  239. );
  240. const firstDataRow = screen.getAllByRole('rowgroup')[1];
  241. const cells = firstDataRow.querySelectorAll('td');
  242. expect(cells).toHaveLength(12);
  243. expect(cells[0]).toHaveTextContent('2020-01-01 12:34:56');
  244. expect(cells[1]).toHaveTextContent('Michael');
  245. // number is not in `metrics` list, so it should output raw value
  246. // (in real world Superset, this would mean the column is used in GROUP BY)
  247. expect(cells[2]).toHaveTextContent('2467063');
  248. // should not render column with `.` in name as `undefined`
  249. expect(cells[3]).toHaveTextContent('foo');
  250. expect(cells[6]).toHaveTextContent('2467');
  251. expect(cells[8]).toHaveTextContent('N/A');
  252. });
  253. it('render advanced data', () => {
  254. render(
  255. <>
  256. <TableChart {...transformProps(testData.advanced)} sticky={false} />
  257. ,
  258. </>,
  259. );
  260. const secondColumnHeader = screen.getByText('Sum of Num');
  261. expect(secondColumnHeader).toBeInTheDocument();
  262. expect(secondColumnHeader?.getAttribute('data-column-name')).toEqual(
  263. '1',
  264. );
  265. const firstDataRow = screen.getAllByRole('rowgroup')[1];
  266. const cells = firstDataRow.querySelectorAll('td');
  267. expect(cells[0]).toHaveTextContent('Michael');
  268. expect(cells[2]).toHaveTextContent('12.346%');
  269. expect(cells[4]).toHaveTextContent('2.47k');
  270. });
  271. it('render advanced data with currencies', () => {
  272. render(
  273. ProviderWrapper({
  274. children: (
  275. <TableChart
  276. {...transformProps(testData.advancedWithCurrency)}
  277. sticky={false}
  278. />
  279. ),
  280. }),
  281. );
  282. const cells = document.querySelectorAll('td');
  283. expect(document.querySelectorAll('th')[1]).toHaveTextContent(
  284. 'Sum of Num',
  285. );
  286. expect(cells[0]).toHaveTextContent('Michael');
  287. expect(cells[2]).toHaveTextContent('12.346%');
  288. expect(cells[4]).toHaveTextContent('$ 2.47k');
  289. });
  290. it('render data with a bigint value in a raw record mode', () => {
  291. render(
  292. ProviderWrapper({
  293. children: (
  294. <TableChart
  295. {...transformProps(testData.bigint)}
  296. sticky={false}
  297. isRawRecords
  298. />
  299. ),
  300. }),
  301. );
  302. const cells = document.querySelectorAll('td');
  303. expect(document.querySelectorAll('th')[0]).toHaveTextContent('name');
  304. expect(document.querySelectorAll('th')[1]).toHaveTextContent('id');
  305. expect(cells[0]).toHaveTextContent('Michael');
  306. expect(cells[1]).toHaveTextContent('4312');
  307. expect(cells[2]).toHaveTextContent('John');
  308. expect(cells[3]).toHaveTextContent('1234567890123456789');
  309. });
  310. it('render raw data', () => {
  311. const props = transformProps({
  312. ...testData.raw,
  313. rawFormData: { ...testData.raw.rawFormData },
  314. });
  315. render(
  316. ProviderWrapper({
  317. children: <TableChart {...props} sticky={false} />,
  318. }),
  319. );
  320. const cells = document.querySelectorAll('td');
  321. expect(document.querySelectorAll('th')[0]).toHaveTextContent('num');
  322. expect(cells[0]).toHaveTextContent('1234');
  323. expect(cells[1]).toHaveTextContent('10000');
  324. expect(cells[1]).toHaveTextContent('0');
  325. });
  326. it('render raw data with currencies', () => {
  327. const props = transformProps({
  328. ...testData.raw,
  329. rawFormData: {
  330. ...testData.raw.rawFormData,
  331. column_config: {
  332. num: {
  333. currencyFormat: { symbol: 'USD', symbolPosition: 'prefix' },
  334. },
  335. },
  336. },
  337. });
  338. render(
  339. ProviderWrapper({
  340. children: <TableChart {...props} sticky={false} />,
  341. }),
  342. );
  343. const cells = document.querySelectorAll('td');
  344. expect(document.querySelectorAll('th')[0]).toHaveTextContent('num');
  345. expect(cells[0]).toHaveTextContent('$ 1.23k');
  346. expect(cells[1]).toHaveTextContent('$ 10k');
  347. expect(cells[2]).toHaveTextContent('$ 0');
  348. });
  349. it('render small formatted data with currencies', () => {
  350. const props = transformProps({
  351. ...testData.raw,
  352. rawFormData: {
  353. ...testData.raw.rawFormData,
  354. column_config: {
  355. num: {
  356. d3SmallNumberFormat: '.2r',
  357. currencyFormat: { symbol: 'USD', symbolPosition: 'prefix' },
  358. },
  359. },
  360. },
  361. queriesData: [
  362. {
  363. ...testData.raw.queriesData[0],
  364. data: [
  365. {
  366. num: 1234,
  367. },
  368. {
  369. num: 0.5,
  370. },
  371. {
  372. num: 0.61234,
  373. },
  374. ],
  375. },
  376. ],
  377. });
  378. render(
  379. ProviderWrapper({
  380. children: <TableChart {...props} sticky={false} />,
  381. }),
  382. );
  383. const cells = document.querySelectorAll('td');
  384. expect(document.querySelectorAll('th')[0]).toHaveTextContent('num');
  385. expect(cells[0]).toHaveTextContent('$ 1.23k');
  386. expect(cells[1]).toHaveTextContent('$ 0.50');
  387. expect(cells[2]).toHaveTextContent('$ 0.61');
  388. });
  389. it('render empty data', () => {
  390. render(
  391. <TableChart {...transformProps(testData.empty)} sticky={false} />,
  392. );
  393. expect(screen.getByText('No records found')).toBeInTheDocument();
  394. });
  395. it('render color with column color formatter', () => {
  396. render(
  397. ProviderWrapper({
  398. children: (
  399. <TableChart
  400. {...transformProps({
  401. ...testData.advanced,
  402. rawFormData: {
  403. ...testData.advanced.rawFormData,
  404. conditional_formatting: [
  405. {
  406. colorScheme: '#ACE1C4',
  407. column: 'sum__num',
  408. operator: '>',
  409. targetValue: 2467,
  410. },
  411. ],
  412. },
  413. })}
  414. />
  415. ),
  416. }),
  417. );
  418. expect(getComputedStyle(screen.getByTitle('2467063')).background).toBe(
  419. 'rgba(172, 225, 196, 1)',
  420. );
  421. expect(getComputedStyle(screen.getByTitle('2467')).background).toBe('');
  422. });
  423. it('render cell without color', () => {
  424. const dataWithEmptyCell = testData.advanced.queriesData[0];
  425. dataWithEmptyCell.data.push({
  426. __timestamp: null,
  427. name: 'Noah',
  428. sum__num: null,
  429. '%pct_nice': 0.643,
  430. 'abc.com': 'bazzinga',
  431. });
  432. render(
  433. ProviderWrapper({
  434. children: (
  435. <TableChart
  436. {...transformProps({
  437. ...testData.advanced,
  438. queriesData: [dataWithEmptyCell],
  439. rawFormData: {
  440. ...testData.advanced.rawFormData,
  441. conditional_formatting: [
  442. {
  443. colorScheme: '#ACE1C4',
  444. column: 'sum__num',
  445. operator: '<',
  446. targetValue: 12342,
  447. },
  448. ],
  449. },
  450. })}
  451. />
  452. ),
  453. }),
  454. );
  455. expect(getComputedStyle(screen.getByTitle('2467')).background).toBe(
  456. 'rgba(172, 225, 196, 0.812)',
  457. );
  458. expect(getComputedStyle(screen.getByTitle('2467063')).background).toBe(
  459. '',
  460. );
  461. expect(getComputedStyle(screen.getByText('N/A')).background).toBe('');
  462. });
  463. it('should display original label in grouped headers', () => {
  464. const props = transformProps(testData.comparison);
  465. render(<TableChart {...props} sticky={false} />);
  466. const groupHeaders = screen.getAllByRole('columnheader');
  467. expect(groupHeaders.length).toBeGreaterThan(0);
  468. const hasMetricHeaders = groupHeaders.some(
  469. header =>
  470. header.textContent &&
  471. (header.textContent.includes('metric') ||
  472. header.textContent.includes('Metric')),
  473. );
  474. expect(hasMetricHeaders).toBe(true);
  475. });
  476. it('render cell bars properly, and only when it is toggled on in both regular and percent metrics', () => {
  477. const props = transformProps({
  478. ...testData.raw,
  479. rawFormData: { ...testData.raw.rawFormData },
  480. });
  481. props.columns[0].isMetric = true;
  482. render(
  483. ProviderWrapper({
  484. children: <TableChart {...props} sticky={false} />,
  485. }),
  486. );
  487. let cells = document.querySelectorAll('div.cell-bar');
  488. cells.forEach(cell => {
  489. expect(cell).toHaveClass('positive');
  490. });
  491. props.columns[0].isMetric = false;
  492. props.columns[0].isPercentMetric = true;
  493. render(
  494. ProviderWrapper({
  495. children: <TableChart {...props} sticky={false} />,
  496. }),
  497. );
  498. cells = document.querySelectorAll('div.cell-bar');
  499. cells.forEach(cell => {
  500. expect(cell).toHaveClass('positive');
  501. });
  502. props.showCellBars = false;
  503. render(
  504. ProviderWrapper({
  505. children: <TableChart {...props} sticky={false} />,
  506. }),
  507. );
  508. cells = document.querySelectorAll('td');
  509. props.columns[0].isPercentMetric = false;
  510. props.columns[0].isMetric = true;
  511. render(
  512. ProviderWrapper({
  513. children: <TableChart {...props} sticky={false} />,
  514. }),
  515. );
  516. cells = document.querySelectorAll('td');
  517. });
  518. it('render color with string column color formatter(operator begins with)', () => {
  519. render(
  520. ProviderWrapper({
  521. children: (
  522. <TableChart
  523. {...transformProps({
  524. ...testData.advanced,
  525. rawFormData: {
  526. ...testData.advanced.rawFormData,
  527. conditional_formatting: [
  528. {
  529. colorScheme: '#ACE1C4',
  530. column: 'name',
  531. operator: 'begins with',
  532. targetValue: 'J',
  533. },
  534. ],
  535. },
  536. })}
  537. />
  538. ),
  539. }),
  540. );
  541. expect(getComputedStyle(screen.getByText('Joe')).background).toBe(
  542. 'rgba(172, 225, 196, 1)',
  543. );
  544. expect(getComputedStyle(screen.getByText('Michael')).background).toBe(
  545. '',
  546. );
  547. });
  548. it('render color with string column color formatter (operator ends with)', () => {
  549. render(
  550. ProviderWrapper({
  551. children: (
  552. <TableChart
  553. {...transformProps({
  554. ...testData.advanced,
  555. rawFormData: {
  556. ...testData.advanced.rawFormData,
  557. conditional_formatting: [
  558. {
  559. colorScheme: '#ACE1C4',
  560. column: 'name',
  561. operator: 'ends with',
  562. targetValue: 'ia',
  563. },
  564. ],
  565. },
  566. })}
  567. />
  568. ),
  569. }),
  570. );
  571. expect(getComputedStyle(screen.getByText('Maria')).background).toBe(
  572. 'rgba(172, 225, 196, 1)',
  573. );
  574. expect(getComputedStyle(screen.getByText('Joe')).background).toBe('');
  575. });
  576. it('render color with string column color formatter (operator containing)', () => {
  577. render(
  578. ProviderWrapper({
  579. children: (
  580. <TableChart
  581. {...transformProps({
  582. ...testData.advanced,
  583. rawFormData: {
  584. ...testData.advanced.rawFormData,
  585. conditional_formatting: [
  586. {
  587. colorScheme: '#ACE1C4',
  588. column: 'name',
  589. operator: 'containing',
  590. targetValue: 'c',
  591. },
  592. ],
  593. },
  594. })}
  595. />
  596. ),
  597. }),
  598. );
  599. expect(getComputedStyle(screen.getByText('Michael')).background).toBe(
  600. 'rgba(172, 225, 196, 1)',
  601. );
  602. expect(getComputedStyle(screen.getByText('Joe')).background).toBe('');
  603. });
  604. it('render color with string column color formatter (operator not containing)', () => {
  605. render(
  606. ProviderWrapper({
  607. children: (
  608. <TableChart
  609. {...transformProps({
  610. ...testData.advanced,
  611. rawFormData: {
  612. ...testData.advanced.rawFormData,
  613. conditional_formatting: [
  614. {
  615. colorScheme: '#ACE1C4',
  616. column: 'name',
  617. operator: 'not containing',
  618. targetValue: 'i',
  619. },
  620. ],
  621. },
  622. })}
  623. />
  624. ),
  625. }),
  626. );
  627. expect(getComputedStyle(screen.getByText('Joe')).background).toBe(
  628. 'rgba(172, 225, 196, 1)',
  629. );
  630. expect(getComputedStyle(screen.getByText('Michael')).background).toBe(
  631. '',
  632. );
  633. });
  634. it('render color with string column color formatter (operator =)', () => {
  635. render(
  636. ProviderWrapper({
  637. children: (
  638. <TableChart
  639. {...transformProps({
  640. ...testData.advanced,
  641. rawFormData: {
  642. ...testData.advanced.rawFormData,
  643. conditional_formatting: [
  644. {
  645. colorScheme: '#ACE1C4',
  646. column: 'name',
  647. operator: '=',
  648. targetValue: 'Joe',
  649. },
  650. ],
  651. },
  652. })}
  653. />
  654. ),
  655. }),
  656. );
  657. expect(getComputedStyle(screen.getByText('Joe')).background).toBe(
  658. 'rgba(172, 225, 196, 1)',
  659. );
  660. expect(getComputedStyle(screen.getByText('Michael')).background).toBe(
  661. '',
  662. );
  663. });
  664. it('render color with string column color formatter (operator None)', () => {
  665. render(
  666. ProviderWrapper({
  667. children: (
  668. <TableChart
  669. {...transformProps({
  670. ...testData.advanced,
  671. rawFormData: {
  672. ...testData.advanced.rawFormData,
  673. conditional_formatting: [
  674. {
  675. colorScheme: '#ACE1C4',
  676. column: 'name',
  677. operator: 'None',
  678. },
  679. ],
  680. },
  681. })}
  682. />
  683. ),
  684. }),
  685. );
  686. expect(getComputedStyle(screen.getByText('Joe')).background).toBe(
  687. 'rgba(172, 225, 196, 1)',
  688. );
  689. expect(getComputedStyle(screen.getByText('Michael')).background).toBe(
  690. 'rgba(172, 225, 196, 1)',
  691. );
  692. expect(getComputedStyle(screen.getByText('Maria')).background).toBe(
  693. 'rgba(172, 225, 196, 1)',
  694. );
  695. });
  696. it('render color with column color formatter to entire row', () => {
  697. render(
  698. ProviderWrapper({
  699. children: (
  700. <TableChart
  701. {...transformProps({
  702. ...testData.advanced,
  703. rawFormData: {
  704. ...testData.advanced.rawFormData,
  705. conditional_formatting: [
  706. {
  707. colorScheme: '#ACE1C4',
  708. column: 'sum__num',
  709. operator: '>',
  710. targetValue: 2467,
  711. toAllRow: true,
  712. },
  713. ],
  714. },
  715. })}
  716. />
  717. ),
  718. }),
  719. );
  720. expect(getComputedStyle(screen.getByText('Michael')).background).toBe(
  721. 'rgba(172, 225, 196, 1)',
  722. );
  723. expect(getComputedStyle(screen.getByTitle('2467063')).background).toBe(
  724. 'rgba(172, 225, 196, 1)',
  725. );
  726. expect(getComputedStyle(screen.getByTitle('0.123456')).background).toBe(
  727. 'rgba(172, 225, 196, 1)',
  728. );
  729. });
  730. it('display text color using column color formatter', () => {
  731. render(
  732. ProviderWrapper({
  733. children: (
  734. <TableChart
  735. {...transformProps({
  736. ...testData.advanced,
  737. rawFormData: {
  738. ...testData.advanced.rawFormData,
  739. conditional_formatting: [
  740. {
  741. colorScheme: '#ACE1C4',
  742. column: 'sum__num',
  743. operator: '>',
  744. targetValue: 2467,
  745. toTextColor: true,
  746. },
  747. ],
  748. },
  749. })}
  750. />
  751. ),
  752. }),
  753. );
  754. expect(getComputedStyle(screen.getByTitle('2467063')).color).toBe(
  755. 'rgba(172, 225, 196, 1)',
  756. );
  757. expect(getComputedStyle(screen.getByTitle('2467')).color).toBe(
  758. 'rgba(0, 0, 0, 0.88)',
  759. );
  760. });
  761. it('display text color using column color formatter for entire row', () => {
  762. render(
  763. ProviderWrapper({
  764. children: (
  765. <TableChart
  766. {...transformProps({
  767. ...testData.advanced,
  768. rawFormData: {
  769. ...testData.advanced.rawFormData,
  770. conditional_formatting: [
  771. {
  772. colorScheme: '#ACE1C4',
  773. column: 'sum__num',
  774. operator: '>',
  775. targetValue: 2467,
  776. toAllRow: true,
  777. toTextColor: true,
  778. },
  779. ],
  780. },
  781. })}
  782. />
  783. ),
  784. }),
  785. );
  786. expect(getComputedStyle(screen.getByText('Michael')).color).toBe(
  787. 'rgba(172, 225, 196, 1)',
  788. );
  789. expect(getComputedStyle(screen.getByTitle('2467063')).color).toBe(
  790. 'rgba(172, 225, 196, 1)',
  791. );
  792. expect(getComputedStyle(screen.getByTitle('0.123456')).color).toBe(
  793. 'rgba(172, 225, 196, 1)',
  794. );
  795. });
  796. });
  797. });
  798. });