renderer.test.ts 12 KB

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