renderer.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import _ from 'lodash';
  2. import TableModel from 'app/core/table_model';
  3. import { getColorDefinitionByName } from '@grafana/ui';
  4. import { Options } from '../types';
  5. import { PanelProps, LoadingState } from '@grafana/ui/src/types';
  6. import moment from 'moment';
  7. import { TableRenderer } from '../renderer';
  8. // TODO: this is commented out with *x* describe!
  9. // Essentially all the elements need to replace the <td> with <div>
  10. xdescribe('when rendering table', () => {
  11. const SemiDarkOrange = getColorDefinitionByName('semi-dark-orange');
  12. describe('given 13 columns', () => {
  13. const table = new TableModel();
  14. table.columns = [
  15. { text: 'Time' },
  16. { text: 'Value' },
  17. { text: 'Colored' },
  18. { text: 'Undefined' },
  19. { text: 'String' },
  20. { text: 'United', unit: 'bps' },
  21. { text: 'Sanitized' },
  22. { text: 'Link' },
  23. { text: 'Array' },
  24. { text: 'Mapping' },
  25. { text: 'RangeMapping' },
  26. { text: 'MappingColored' },
  27. { text: 'RangeMappingColored' },
  28. ];
  29. table.rows = [
  30. [1388556366666, 1230, 40, undefined, '', '', 'my.host.com', 'host1', ['value1', 'value2'], 1, 2, 1, 2],
  31. ];
  32. const panel: Options = {
  33. showHeader: true,
  34. pageSize: 10,
  35. styles: [
  36. {
  37. pattern: 'Time',
  38. type: 'date',
  39. alias: 'Timestamp',
  40. },
  41. {
  42. pattern: '/(Val)ue/',
  43. type: 'number',
  44. unit: 'ms',
  45. decimals: 3,
  46. alias: '$1',
  47. },
  48. {
  49. pattern: 'Colored',
  50. type: 'number',
  51. unit: 'none',
  52. decimals: 1,
  53. colorMode: 'value',
  54. thresholds: [50, 80],
  55. colors: ['#00ff00', SemiDarkOrange.name, 'rgb(1,0,0)'],
  56. },
  57. {
  58. pattern: 'String',
  59. type: 'string',
  60. },
  61. {
  62. pattern: 'String',
  63. type: 'string',
  64. },
  65. {
  66. pattern: 'United',
  67. type: 'number',
  68. unit: 'ms',
  69. decimals: 2,
  70. },
  71. {
  72. pattern: 'Sanitized',
  73. type: 'string',
  74. sanitize: true,
  75. },
  76. {
  77. pattern: 'Link',
  78. type: 'string',
  79. link: true,
  80. linkUrl: '/dashboard?param=$__cell&param_1=$__cell_1&param_2=$__cell_2',
  81. linkTooltip: '$__cell $__cell_1 $__cell_6',
  82. linkTargetBlank: true,
  83. },
  84. {
  85. pattern: 'Array',
  86. type: 'number',
  87. unit: 'ms',
  88. decimals: 3,
  89. },
  90. {
  91. pattern: 'Mapping',
  92. type: 'string',
  93. mappingType: 1,
  94. valueMaps: [
  95. {
  96. value: '1',
  97. text: 'on',
  98. },
  99. {
  100. value: '0',
  101. text: 'off',
  102. },
  103. {
  104. value: 'HELLO WORLD',
  105. text: 'HELLO GRAFANA',
  106. },
  107. {
  108. value: 'value1, value2',
  109. text: 'value3, value4',
  110. },
  111. ],
  112. },
  113. {
  114. pattern: 'RangeMapping',
  115. type: 'string',
  116. mappingType: 2,
  117. rangeMaps: [
  118. {
  119. from: '1',
  120. to: '3',
  121. text: 'on',
  122. },
  123. {
  124. from: '3',
  125. to: '6',
  126. text: 'off',
  127. },
  128. ],
  129. },
  130. {
  131. pattern: 'MappingColored',
  132. type: 'string',
  133. mappingType: 1,
  134. valueMaps: [
  135. {
  136. value: '1',
  137. text: 'on',
  138. },
  139. {
  140. value: '0',
  141. text: 'off',
  142. },
  143. ],
  144. colorMode: 'value',
  145. thresholds: [1, 2],
  146. colors: ['#00ff00', SemiDarkOrange.name, 'rgb(1,0,0)'],
  147. },
  148. {
  149. pattern: 'RangeMappingColored',
  150. type: 'string',
  151. mappingType: 2,
  152. rangeMaps: [
  153. {
  154. from: '1',
  155. to: '3',
  156. text: 'on',
  157. },
  158. {
  159. from: '3',
  160. to: '6',
  161. text: 'off',
  162. },
  163. ],
  164. colorMode: 'value',
  165. thresholds: [2, 5],
  166. colors: ['#00ff00', SemiDarkOrange.name, 'rgb(1,0,0)'],
  167. },
  168. ],
  169. };
  170. // const sanitize = value => {
  171. // return 'sanitized';
  172. // };
  173. const props: PanelProps<Options> = {
  174. panelData: {
  175. tableData: table,
  176. },
  177. width: 100,
  178. height: 100,
  179. timeRange: {
  180. from: moment(),
  181. to: moment(),
  182. raw: {
  183. from: moment(),
  184. to: moment(),
  185. },
  186. },
  187. loading: LoadingState.Done,
  188. replaceVariables: (value, scopedVars) => {
  189. if (scopedVars) {
  190. // For testing variables replacement in link
  191. _.each(scopedVars, (val, key) => {
  192. value = value.replace('$' + key, val.value);
  193. });
  194. }
  195. return value;
  196. },
  197. renderCounter: 1,
  198. options: panel,
  199. };
  200. const rowGetter = ({ index }) => table.rows[index];
  201. const renderer = new TableRenderer(panel.styles, table.columns, rowGetter, props.replaceVariables);
  202. renderer.setTheme(null);
  203. it('time column should be formated', () => {
  204. const html = renderer.renderCell(0, 0, 1388556366666);
  205. expect(html).toBe('<td>2014-01-01T06:06:06Z</td>');
  206. });
  207. it('time column with epoch as string should be formatted', () => {
  208. const html = renderer.renderCell(0, 0, '1388556366666');
  209. expect(html).toBe('<td>2014-01-01T06:06:06Z</td>');
  210. });
  211. it('time column with RFC2822 date as string should be formatted', () => {
  212. const html = renderer.renderCell(0, 0, 'Sat, 01 Dec 2018 01:00:00 GMT');
  213. expect(html).toBe('<td>2018-12-01T01:00:00Z</td>');
  214. });
  215. it('time column with ISO date as string should be formatted', () => {
  216. const html = renderer.renderCell(0, 0, '2018-12-01T01:00:00Z');
  217. expect(html).toBe('<td>2018-12-01T01:00:00Z</td>');
  218. });
  219. it('undefined time column should be rendered as -', () => {
  220. const html = renderer.renderCell(0, 0, undefined);
  221. expect(html).toBe('<td>-</td>');
  222. });
  223. it('null time column should be rendered as -', () => {
  224. const html = renderer.renderCell(0, 0, null);
  225. expect(html).toBe('<td>-</td>');
  226. });
  227. it('number column with unit specified should ignore style unit', () => {
  228. const html = renderer.renderCell(5, 0, 1230);
  229. expect(html).toBe('<td>1.23 kbps</td>');
  230. });
  231. it('number column should be formated', () => {
  232. const html = renderer.renderCell(1, 0, 1230);
  233. expect(html).toBe('<td>1.230 s</td>');
  234. });
  235. it('number style should ignore string values', () => {
  236. const html = renderer.renderCell(1, 0, 'asd');
  237. expect(html).toBe('<td>asd</td>');
  238. });
  239. it('colored cell should have style (handles HEX color values)', () => {
  240. const html = renderer.renderCell(2, 0, 40);
  241. expect(html).toBe('<td style="color:#00ff00">40.0</td>');
  242. });
  243. it('colored cell should have style (handles named color values', () => {
  244. const html = renderer.renderCell(2, 0, 55);
  245. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">55.0</td>`);
  246. });
  247. it('colored cell should have style handles(rgb color values)', () => {
  248. const html = renderer.renderCell(2, 0, 85);
  249. expect(html).toBe('<td style="color:rgb(1,0,0)">85.0</td>');
  250. });
  251. it('unformated undefined should be rendered as string', () => {
  252. const html = renderer.renderCell(3, 0, 'value');
  253. expect(html).toBe('<td>value</td>');
  254. });
  255. it('string style with escape html should return escaped html', () => {
  256. const html = renderer.renderCell(4, 0, '&breaking <br /> the <br /> row');
  257. expect(html).toBe('<td>&amp;breaking &lt;br /&gt; the &lt;br /&gt; row</td>');
  258. });
  259. it('undefined formater should return escaped html', () => {
  260. const html = renderer.renderCell(3, 0, '&breaking <br /> the <br /> row');
  261. expect(html).toBe('<td>&amp;breaking &lt;br /&gt; the &lt;br /&gt; row</td>');
  262. });
  263. it('undefined value should render as -', () => {
  264. const html = renderer.renderCell(3, 0, undefined);
  265. expect(html).toBe('<td></td>');
  266. });
  267. it('sanitized value should render as', () => {
  268. const html = renderer.renderCell(6, 0, 'text <a href="http://google.com">link</a>');
  269. expect(html).toBe('<td>sanitized</td>');
  270. });
  271. it('Time column title should be Timestamp', () => {
  272. expect(table.columns[0].title).toBe('Timestamp');
  273. });
  274. it('Value column title should be Val', () => {
  275. expect(table.columns[1].title).toBe('Val');
  276. });
  277. it('Colored column title should be Colored', () => {
  278. expect(table.columns[2].title).toBe('Colored');
  279. });
  280. it('link should render as', () => {
  281. const html = renderer.renderCell(7, 0, 'host1');
  282. const expectedHtml = `
  283. <td class="table-panel-cell-link">
  284. <a href="/dashboard?param=host1&param_1=1230&param_2=40"
  285. target="_blank" data-link-tooltip data-original-title="host1 1230 my.host.com" data-placement="right">
  286. host1
  287. </a>
  288. </td>
  289. `;
  290. expect(normalize(html)).toBe(normalize(expectedHtml));
  291. });
  292. it('Array column should not use number as formatter', () => {
  293. const html = renderer.renderCell(8, 0, ['value1', 'value2']);
  294. expect(html).toBe('<td>value1, value2</td>');
  295. });
  296. it('numeric value should be mapped to text', () => {
  297. const html = renderer.renderCell(9, 0, 1);
  298. expect(html).toBe('<td>on</td>');
  299. });
  300. it('string numeric value should be mapped to text', () => {
  301. const html = renderer.renderCell(9, 0, '0');
  302. expect(html).toBe('<td>off</td>');
  303. });
  304. it('string value should be mapped to text', () => {
  305. const html = renderer.renderCell(9, 0, 'HELLO WORLD');
  306. expect(html).toBe('<td>HELLO GRAFANA</td>');
  307. });
  308. it('array column value should be mapped to text', () => {
  309. const html = renderer.renderCell(9, 0, ['value1', 'value2']);
  310. expect(html).toBe('<td>value3, value4</td>');
  311. });
  312. it('value should be mapped to text (range)', () => {
  313. const html = renderer.renderCell(10, 0, 2);
  314. expect(html).toBe('<td>on</td>');
  315. });
  316. it('value should be mapped to text (range)', () => {
  317. const html = renderer.renderCell(10, 0, 5);
  318. expect(html).toBe('<td>off</td>');
  319. });
  320. it('array column value should not be mapped to text', () => {
  321. const html = renderer.renderCell(10, 0, ['value1', 'value2']);
  322. expect(html).toBe('<td>value1, value2</td>');
  323. });
  324. it('value should be mapped to text and colored cell should have style', () => {
  325. const html = renderer.renderCell(11, 0, 1);
  326. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">on</td>`);
  327. });
  328. it('value should be mapped to text and colored cell should have style', () => {
  329. const html = renderer.renderCell(11, 0, '1');
  330. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">on</td>`);
  331. });
  332. it('value should be mapped to text and colored cell should have style', () => {
  333. const html = renderer.renderCell(11, 0, 0);
  334. expect(html).toBe('<td style="color:#00ff00">off</td>');
  335. });
  336. it('value should be mapped to text and colored cell should have style', () => {
  337. const html = renderer.renderCell(11, 0, '0');
  338. expect(html).toBe('<td style="color:#00ff00">off</td>');
  339. });
  340. it('value should be mapped to text and colored cell should have style', () => {
  341. const html = renderer.renderCell(11, 0, '2.1');
  342. expect(html).toBe('<td style="color:rgb(1,0,0)">2.1</td>');
  343. });
  344. it('value should be mapped to text (range) and colored cell should have style', () => {
  345. const html = renderer.renderCell(12, 0, 0);
  346. expect(html).toBe('<td style="color:#00ff00">0</td>');
  347. });
  348. it('value should be mapped to text (range) and colored cell should have style', () => {
  349. const html = renderer.renderCell(12, 0, 1);
  350. expect(html).toBe('<td style="color:#00ff00">on</td>');
  351. });
  352. it('value should be mapped to text (range) and colored cell should have style', () => {
  353. const html = renderer.renderCell(12, 0, 4);
  354. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">off</td>`);
  355. });
  356. it('value should be mapped to text (range) and colored cell should have style', () => {
  357. const html = renderer.renderCell(12, 0, '7.1');
  358. expect(html).toBe('<td style="color:rgb(1,0,0)">7.1</td>');
  359. });
  360. });
  361. });
  362. function normalize(str) {
  363. return str.replace(/\s+/gm, ' ').trim();
  364. }