index.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { render, unmountComponentAtNode } from 'react-dom';
  2. import { act } from 'react-dom/test-utils';
  3. import MilvusGrid from '../../grid/index';
  4. import { ToolBarConfig } from '../../grid/Types';
  5. let container: any = null;
  6. jest.mock('react-i18next', () => {
  7. return {
  8. useTranslation: () => ({
  9. t: () => ({
  10. grid: {},
  11. }),
  12. }),
  13. };
  14. });
  15. jest.mock('../../grid/Table', () => {
  16. return () => {
  17. return <div id="table">{}</div>;
  18. };
  19. });
  20. jest.mock('../../grid/ToolBar', () => {
  21. return () => {
  22. return <div id="tool-bar"></div>;
  23. };
  24. });
  25. jest.mock('react-router-dom', () => {
  26. return {
  27. useHistory: () => {
  28. return {
  29. listen: () => () => {},
  30. location: {
  31. name: '',
  32. },
  33. };
  34. },
  35. };
  36. });
  37. describe('Test Grid index', () => {
  38. beforeEach(() => {
  39. container = document.createElement('div');
  40. document.body.appendChild(container);
  41. });
  42. afterEach(() => {
  43. unmountComponentAtNode(container);
  44. container.remove();
  45. container = null;
  46. });
  47. it('Has Table Data', () => {
  48. act(() => {
  49. render(
  50. <MilvusGrid
  51. primaryKey="id"
  52. rows={[{}]}
  53. colDefinitions={[]}
  54. rowCount={10}
  55. toolbarConfigs={[]}
  56. />,
  57. container
  58. );
  59. });
  60. expect(container.querySelectorAll('#table').length).toEqual(1);
  61. });
  62. it('Test title', () => {
  63. const title = ['collections', 'vectors'];
  64. act(() => {
  65. render(
  66. <MilvusGrid
  67. primaryKey="id"
  68. rows={[]}
  69. colDefinitions={[]}
  70. rowCount={0}
  71. toolbarConfigs={[]}
  72. title={title}
  73. />,
  74. container
  75. );
  76. });
  77. const titleNodes = container.querySelectorAll('h6');
  78. expect(titleNodes.length).toEqual(title.length);
  79. expect(titleNodes[0].textContent).toEqual(title[0]);
  80. expect(titleNodes[1].textContent).toEqual(title[1]);
  81. });
  82. it('Test SearchForm', () => {
  83. const SearchForm = () => <div id="search-form"></div>;
  84. act(() => {
  85. render(
  86. <MilvusGrid
  87. primaryKey="id"
  88. rows={[]}
  89. colDefinitions={[]}
  90. rowCount={0}
  91. toolbarConfigs={[]}
  92. searchForm={<SearchForm />}
  93. />,
  94. container
  95. );
  96. });
  97. expect(container.querySelectorAll('#search-form').length).toEqual(1);
  98. });
  99. it('Test Toolbar ', () => {
  100. const ToolbarConfig: ToolBarConfig[] = [
  101. {
  102. label: 'collection',
  103. icon: 'search',
  104. onClick: () => {},
  105. onSearch: () => {},
  106. },
  107. ];
  108. act(() => {
  109. render(
  110. <MilvusGrid
  111. primaryKey="id"
  112. rows={[]}
  113. colDefinitions={[]}
  114. rowCount={0}
  115. toolbarConfigs={ToolbarConfig}
  116. />,
  117. container
  118. );
  119. });
  120. expect(container.querySelectorAll('#tool-bar').length).toEqual(1);
  121. });
  122. });