renderer.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import _ from 'lodash';
  2. import TableModel from 'app/core/table_model';
  3. import { TablePanel } from '../TablePanel';
  4. import { getColorDefinitionByName } from '@grafana/ui';
  5. import { Options } from '../types';
  6. import { PanelProps, LoadingState } from '@grafana/ui/src/types';
  7. import moment from 'moment';
  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 renderer = new TablePanel(props); //panel, table, 'utc', sanitize, templateSrv);
  201. renderer.render(); // This will initalize
  202. it('time column should be formated', () => {
  203. const html = renderer.renderCell(0, 0, 1388556366666);
  204. expect(html).toBe('<td>2014-01-01T06:06:06Z</td>');
  205. });
  206. it('time column with epoch as string should be formatted', () => {
  207. const html = renderer.renderCell(0, 0, '1388556366666');
  208. expect(html).toBe('<td>2014-01-01T06:06:06Z</td>');
  209. });
  210. it('time column with RFC2822 date as string should be formatted', () => {
  211. const html = renderer.renderCell(0, 0, 'Sat, 01 Dec 2018 01:00:00 GMT');
  212. expect(html).toBe('<td>2018-12-01T01:00:00Z</td>');
  213. });
  214. it('time column with ISO date as string should be formatted', () => {
  215. const html = renderer.renderCell(0, 0, '2018-12-01T01:00:00Z');
  216. expect(html).toBe('<td>2018-12-01T01:00:00Z</td>');
  217. });
  218. it('undefined time column should be rendered as -', () => {
  219. const html = renderer.renderCell(0, 0, undefined);
  220. expect(html).toBe('<td>-</td>');
  221. });
  222. it('null time column should be rendered as -', () => {
  223. const html = renderer.renderCell(0, 0, null);
  224. expect(html).toBe('<td>-</td>');
  225. });
  226. it('number column with unit specified should ignore style unit', () => {
  227. const html = renderer.renderCell(5, 0, 1230);
  228. expect(html).toBe('<td>1.23 kbps</td>');
  229. });
  230. it('number column should be formated', () => {
  231. const html = renderer.renderCell(1, 0, 1230);
  232. expect(html).toBe('<td>1.230 s</td>');
  233. });
  234. it('number style should ignore string values', () => {
  235. const html = renderer.renderCell(1, 0, 'asd');
  236. expect(html).toBe('<td>asd</td>');
  237. });
  238. it('colored cell should have style (handles HEX color values)', () => {
  239. const html = renderer.renderCell(2, 0, 40);
  240. expect(html).toBe('<td style="color:#00ff00">40.0</td>');
  241. });
  242. it('colored cell should have style (handles named color values', () => {
  243. const html = renderer.renderCell(2, 0, 55);
  244. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">55.0</td>`);
  245. });
  246. it('colored cell should have style handles(rgb color values)', () => {
  247. const html = renderer.renderCell(2, 0, 85);
  248. expect(html).toBe('<td style="color:rgb(1,0,0)">85.0</td>');
  249. });
  250. it('unformated undefined should be rendered as string', () => {
  251. const html = renderer.renderCell(3, 0, 'value');
  252. expect(html).toBe('<td>value</td>');
  253. });
  254. it('string style with escape html should return escaped html', () => {
  255. const html = renderer.renderCell(4, 0, '&breaking <br /> the <br /> row');
  256. expect(html).toBe('<td>&amp;breaking &lt;br /&gt; the &lt;br /&gt; row</td>');
  257. });
  258. it('undefined formater should return escaped html', () => {
  259. const html = renderer.renderCell(3, 0, '&breaking <br /> the <br /> row');
  260. expect(html).toBe('<td>&amp;breaking &lt;br /&gt; the &lt;br /&gt; row</td>');
  261. });
  262. it('undefined value should render as -', () => {
  263. const html = renderer.renderCell(3, 0, undefined);
  264. expect(html).toBe('<td></td>');
  265. });
  266. it('sanitized value should render as', () => {
  267. const html = renderer.renderCell(6, 0, 'text <a href="http://google.com">link</a>');
  268. expect(html).toBe('<td>sanitized</td>');
  269. });
  270. it('Time column title should be Timestamp', () => {
  271. expect(table.columns[0].title).toBe('Timestamp');
  272. });
  273. it('Value column title should be Val', () => {
  274. expect(table.columns[1].title).toBe('Val');
  275. });
  276. it('Colored column title should be Colored', () => {
  277. expect(table.columns[2].title).toBe('Colored');
  278. });
  279. it('link should render as', () => {
  280. const html = renderer.renderCell(7, 0, 'host1');
  281. const expectedHtml = `
  282. <td class="table-panel-cell-link">
  283. <a href="/dashboard?param=host1&param_1=1230&param_2=40"
  284. target="_blank" data-link-tooltip data-original-title="host1 1230 my.host.com" data-placement="right">
  285. host1
  286. </a>
  287. </td>
  288. `;
  289. expect(normalize(html)).toBe(normalize(expectedHtml));
  290. });
  291. it('Array column should not use number as formatter', () => {
  292. const html = renderer.renderCell(8, 0, ['value1', 'value2']);
  293. expect(html).toBe('<td>value1, value2</td>');
  294. });
  295. it('numeric value should be mapped to text', () => {
  296. const html = renderer.renderCell(9, 0, 1);
  297. expect(html).toBe('<td>on</td>');
  298. });
  299. it('string numeric value should be mapped to text', () => {
  300. const html = renderer.renderCell(9, 0, '0');
  301. expect(html).toBe('<td>off</td>');
  302. });
  303. it('string value should be mapped to text', () => {
  304. const html = renderer.renderCell(9, 0, 'HELLO WORLD');
  305. expect(html).toBe('<td>HELLO GRAFANA</td>');
  306. });
  307. it('array column value should be mapped to text', () => {
  308. const html = renderer.renderCell(9, 0, ['value1', 'value2']);
  309. expect(html).toBe('<td>value3, value4</td>');
  310. });
  311. it('value should be mapped to text (range)', () => {
  312. const html = renderer.renderCell(10, 0, 2);
  313. expect(html).toBe('<td>on</td>');
  314. });
  315. it('value should be mapped to text (range)', () => {
  316. const html = renderer.renderCell(10, 0, 5);
  317. expect(html).toBe('<td>off</td>');
  318. });
  319. it('array column value should not be mapped to text', () => {
  320. const html = renderer.renderCell(10, 0, ['value1', 'value2']);
  321. expect(html).toBe('<td>value1, value2</td>');
  322. });
  323. it('value should be mapped to text and colored cell should have style', () => {
  324. const html = renderer.renderCell(11, 0, 1);
  325. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">on</td>`);
  326. });
  327. it('value should be mapped to text and colored cell should have style', () => {
  328. const html = renderer.renderCell(11, 0, '1');
  329. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">on</td>`);
  330. });
  331. it('value should be mapped to text and colored cell should have style', () => {
  332. const html = renderer.renderCell(11, 0, 0);
  333. expect(html).toBe('<td style="color:#00ff00">off</td>');
  334. });
  335. it('value should be mapped to text and colored cell should have style', () => {
  336. const html = renderer.renderCell(11, 0, '0');
  337. expect(html).toBe('<td style="color:#00ff00">off</td>');
  338. });
  339. it('value should be mapped to text and colored cell should have style', () => {
  340. const html = renderer.renderCell(11, 0, '2.1');
  341. expect(html).toBe('<td style="color:rgb(1,0,0)">2.1</td>');
  342. });
  343. it('value should be mapped to text (range) and colored cell should have style', () => {
  344. const html = renderer.renderCell(12, 0, 0);
  345. expect(html).toBe('<td style="color:#00ff00">0</td>');
  346. });
  347. it('value should be mapped to text (range) and colored cell should have style', () => {
  348. const html = renderer.renderCell(12, 0, 1);
  349. expect(html).toBe('<td style="color:#00ff00">on</td>');
  350. });
  351. it('value should be mapped to text (range) and colored cell should have style', () => {
  352. const html = renderer.renderCell(12, 0, 4);
  353. expect(html).toBe(`<td style="color:${SemiDarkOrange.variants.dark}">off</td>`);
  354. });
  355. it('value should be mapped to text (range) and colored cell should have style', () => {
  356. const html = renderer.renderCell(12, 0, '7.1');
  357. expect(html).toBe('<td style="color:rgb(1,0,0)">7.1</td>');
  358. });
  359. });
  360. });
  361. function normalize(str) {
  362. return str.replace(/\s+/gm, ' ').trim();
  363. }