CustomGroupedSelect.spec.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { render, unmountComponentAtNode } from 'react-dom';
  2. import { act } from 'react-dom/test-utils';
  3. import { fireEvent } from '@testing-library/react';
  4. import CustomGroupedSelect from '../../customSelector/CustomGroupedSelect';
  5. import { GroupOption } from '../../customSelector/Types';
  6. let container: any = null;
  7. jest.mock('@material-ui/core/FormControl', () => {
  8. return props => {
  9. const { children } = props;
  10. return <div className="form-control">{children}</div>;
  11. };
  12. });
  13. jest.mock('@material-ui/core/Select', () => {
  14. return props => {
  15. const { children, onChange } = props;
  16. return (
  17. <select className="group-select" onChange={onChange}>
  18. {children}
  19. </select>
  20. );
  21. };
  22. });
  23. jest.mock('@material-ui/core/ListSubheader', () => {
  24. return props => {
  25. const { children } = props;
  26. return <option className="group-header">{children}</option>;
  27. };
  28. });
  29. jest.mock('@material-ui/core/MenuItem', () => {
  30. return props => {
  31. const { children, value } = props;
  32. return (
  33. <option className="group-item" value={value}>
  34. {children}
  35. </option>
  36. );
  37. };
  38. });
  39. describe('Test CustomGroupedSelect', () => {
  40. beforeEach(() => {
  41. container = document.createElement('div');
  42. document.body.appendChild(container);
  43. });
  44. afterEach(() => {
  45. unmountComponentAtNode(container);
  46. container.remove();
  47. container = null;
  48. });
  49. test('test group select props', () => {
  50. const mockOptions: GroupOption[] = [
  51. {
  52. label: 'Group 1',
  53. children: [
  54. {
  55. label: 'group text 1',
  56. value: 'group text 1',
  57. },
  58. {
  59. label: 'group text 2',
  60. value: 'group text 2',
  61. },
  62. {
  63. label: 'group text 3',
  64. value: 'group text 3',
  65. },
  66. ],
  67. },
  68. {
  69. label: 'Group 2',
  70. children: [
  71. {
  72. label: 'group text 11',
  73. value: 'group text 11',
  74. },
  75. {
  76. label: 'group text 22',
  77. value: 'group text 22',
  78. },
  79. {
  80. label: 'group text 33',
  81. value: 'group text 33',
  82. },
  83. ],
  84. },
  85. ];
  86. const handleChange = jest.fn();
  87. act(() => {
  88. render(
  89. <CustomGroupedSelect
  90. options={mockOptions}
  91. value={''}
  92. onChange={handleChange}
  93. />,
  94. container
  95. );
  96. });
  97. expect(container.querySelectorAll('.form-control').length).toBe(1);
  98. expect(container.querySelectorAll('.group-header').length).toBe(2);
  99. expect(container.querySelectorAll('.group-item').length).toBe(6);
  100. const select = container.querySelector('.group-select');
  101. fireEvent.change(select, {
  102. target: {
  103. value: 'group text 2',
  104. },
  105. });
  106. expect(handleChange).toHaveBeenCalledTimes(1);
  107. expect(select.value).toBe('group text 2');
  108. });
  109. });