node-details-table-test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import TestUtils from 'react-dom/test-utils';
  3. import { Provider } from 'react-redux';
  4. import configureStore from '../../../stores/configureStore';
  5. // need ES5 require to keep automocking off
  6. const NodeDetailsTable = require('../node-details-table.js').default;
  7. describe('NodeDetailsTable', () => {
  8. let nodes;
  9. let columns;
  10. let component;
  11. beforeEach(() => {
  12. columns = [
  13. { dataType: 'ip', id: 'kubernetes_ip', label: 'IP' },
  14. { id: 'kubernetes_namespace', label: 'Namespace' },
  15. { dataType: 'duration', id: 'uptime', label: 'Uptime' },
  16. ];
  17. nodes = [
  18. {
  19. id: 'node-1',
  20. metadata: [
  21. { id: 'kubernetes_ip', label: 'IP', value: '10.244.253.24' },
  22. { id: 'kubernetes_namespace', label: 'Namespace', value: '1111' },
  23. {
  24. dataType: 'duration', id: 'uptime', label: 'Uptime', value: '1'
  25. },
  26. ]
  27. }, {
  28. id: 'node-2',
  29. metadata: [
  30. { id: 'kubernetes_ip', label: 'IP', value: '10.244.253.4' },
  31. { id: 'kubernetes_namespace', label: 'Namespace', value: '12' },
  32. {
  33. dataType: 'duration', id: 'uptime', label: 'Uptime', value: '4'
  34. },
  35. ]
  36. }, {
  37. id: 'node-3',
  38. metadata: [
  39. { id: 'kubernetes_ip', label: 'IP', value: '10.44.253.255' },
  40. { id: 'kubernetes_namespace', label: 'Namespace', value: '5' },
  41. {
  42. dataType: 'duration', id: 'uptime', label: 'Uptime', value: '30'
  43. },
  44. ]
  45. }, {
  46. id: 'node-4',
  47. metadata: [
  48. { id: 'kubernetes_ip', label: 'IP', value: '10.244.253.100' },
  49. { id: 'kubernetes_namespace', label: 'Namespace', value: '00000' },
  50. {
  51. dataType: 'duration', id: 'uptime', label: 'Uptime', value: '22222'
  52. },
  53. ]
  54. },
  55. ];
  56. });
  57. function matchColumnValues(columnLabel, expectedValues) {
  58. // Get the index of the column whose values we want to match.
  59. const columnIndex = columns.findIndex(column => column.id === columnLabel);
  60. // Get all the values rendered in the table.
  61. const values = TestUtils
  62. .scryRenderedDOMComponentsWithClass(component, 'node-details-table-node-value')
  63. .map(d => d.title);
  64. // Since we are interested only in the values that appear in the column `columnIndex`,
  65. // we drop the rest. As `values` are ordered by appearance in the DOM structure
  66. // (that is, first by row and then by column), the indexes we are interested in are of the
  67. // form columnIndex + n * columns.length, where n >= 0. Therefore we take only the values
  68. // at the index which divided by columns.length gives a reminder columnIndex.
  69. const filteredValues = values.filter(
  70. (element, index) => index % columns.length === columnIndex
  71. );
  72. // Array comparison
  73. expect(filteredValues).toEqual(expectedValues);
  74. }
  75. function clickColumn(title) {
  76. const node = TestUtils.scryRenderedDOMComponentsWithTag(component, 'td')
  77. .find(d => d.title === title);
  78. TestUtils.Simulate.click(node.children[0]);
  79. }
  80. describe('kubernetes_ip', () => {
  81. it('sorts by column', () => {
  82. component = TestUtils.renderIntoDocument((
  83. <Provider store={configureStore()}>
  84. <NodeDetailsTable
  85. columns={columns}
  86. sortedBy="kubernetes_ip"
  87. nodeIdKey="id"
  88. nodes={nodes}
  89. />
  90. </Provider>
  91. ));
  92. matchColumnValues('kubernetes_ip', [
  93. '10.44.253.255',
  94. '10.244.253.4',
  95. '10.244.253.24',
  96. '10.244.253.100'
  97. ]);
  98. clickColumn('IP');
  99. matchColumnValues('kubernetes_ip', [
  100. '10.244.253.100',
  101. '10.244.253.24',
  102. '10.244.253.4',
  103. '10.44.253.255'
  104. ]);
  105. clickColumn('IP');
  106. matchColumnValues('kubernetes_ip', [
  107. '10.44.253.255',
  108. '10.244.253.4',
  109. '10.244.253.24',
  110. '10.244.253.100'
  111. ]);
  112. });
  113. });
  114. describe('kubernetes_namespace', () => {
  115. it('sorts by column', () => {
  116. component = TestUtils.renderIntoDocument((
  117. <Provider store={configureStore()}>
  118. <NodeDetailsTable
  119. columns={columns}
  120. sortedBy="kubernetes_namespace"
  121. nodeIdKey="id"
  122. nodes={nodes}
  123. />
  124. </Provider>
  125. ));
  126. matchColumnValues('kubernetes_namespace', ['00000', '1111', '12', '5']);
  127. clickColumn('Namespace');
  128. matchColumnValues('kubernetes_namespace', ['5', '12', '1111', '00000']);
  129. clickColumn('Namespace');
  130. matchColumnValues('kubernetes_namespace', ['00000', '1111', '12', '5']);
  131. });
  132. });
  133. describe('uptime duration', () => {
  134. it('sorts by column', () => {
  135. component = TestUtils.renderIntoDocument((
  136. <Provider store={configureStore()}>
  137. <NodeDetailsTable
  138. columns={columns}
  139. sortedBy="uptime"
  140. nodeIdKey="id"
  141. nodes={nodes}
  142. />
  143. </Provider>
  144. ));
  145. matchColumnValues('uptime', ['1 second', '4 seconds', '30 seconds', '6 hours']);
  146. clickColumn('Uptime');
  147. matchColumnValues('uptime', ['6 hours', '30 seconds', '4 seconds', '1 second']);
  148. });
  149. });
  150. });