customInput.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { fireEvent } from '@testing-library/react';
  2. import { render, unmountComponentAtNode } from 'react-dom';
  3. import { act } from 'react-dom/test-utils';
  4. import CustomInput from '../../customInput/CustomInput';
  5. import {
  6. IAdornmentConfig,
  7. IIconConfig,
  8. ITextfieldConfig,
  9. } from '../../customInput/Types';
  10. let container: any = null;
  11. jest.mock('@material-ui/core/styles/makeStyles', () => {
  12. return () => () => ({});
  13. });
  14. jest.mock('@material-ui/core/FormControl', () => {
  15. return props => {
  16. const { children } = props;
  17. return <div className="form-control">{children}</div>;
  18. };
  19. });
  20. jest.mock('@material-ui/core/InputLabel', () => {
  21. return props => {
  22. return <div className="label">{props.children}</div>;
  23. };
  24. });
  25. jest.mock('@material-ui/core/Input', () => {
  26. return props => {
  27. const { type, onBlur, endAdornment } = props;
  28. return (
  29. <>
  30. <div className="type">{type}</div>
  31. <input className="input" type={type} onBlur={onBlur} />
  32. <div>{endAdornment}</div>
  33. </>
  34. );
  35. };
  36. });
  37. jest.mock('@material-ui/core/TextField', () => {
  38. return props => {
  39. const { helperText, onBlur, onChange, label, className } = props;
  40. return (
  41. <div className="text-field">
  42. <div className="text-class">{className}</div>
  43. <div className="text-label">{label}</div>
  44. <div className="text-error">{helperText}</div>
  45. <input
  46. className="text-input"
  47. onBlur={onBlur}
  48. onChange={onChange}
  49. type="text"
  50. />
  51. </div>
  52. );
  53. };
  54. });
  55. jest.mock('@material-ui/core/Grid', () => {
  56. return props => {
  57. const { children } = props;
  58. return <div className="grid">{children}</div>;
  59. };
  60. });
  61. describe('Test CustomInput', () => {
  62. beforeEach(() => {
  63. container = document.createElement('div');
  64. document.body.appendChild(container);
  65. });
  66. afterEach(() => {
  67. unmountComponentAtNode(container);
  68. container.remove();
  69. container = null;
  70. });
  71. test('test text type input', () => {
  72. const handleBlur = jest.fn();
  73. const mockTextConfig: ITextfieldConfig = {
  74. variant: 'standard',
  75. label: 'test text',
  76. key: 'text',
  77. className: 'classname',
  78. onBlur: handleBlur,
  79. };
  80. act(() => {
  81. render(
  82. <CustomInput
  83. type="text"
  84. textConfig={mockTextConfig}
  85. checkValid={() => true}
  86. />,
  87. container
  88. );
  89. });
  90. expect(container.querySelectorAll('.text-field').length).toBe(1);
  91. expect(container.querySelector('.text-class').textContent).toBe(
  92. 'classname'
  93. );
  94. expect(container.querySelector('.text-label').textContent).toBe(
  95. 'test text'
  96. );
  97. const input = container.querySelector('.text-input');
  98. input.focus();
  99. input.blur();
  100. expect(handleBlur).toHaveBeenCalledTimes(1);
  101. });
  102. test('test icon type input', () => {
  103. const handleChange = jest.fn();
  104. const mockIconConfig: IIconConfig = {
  105. icon: <div className="icon"></div>,
  106. inputType: 'icon',
  107. inputConfig: {
  108. label: 'icon text',
  109. key: 'icon',
  110. onChange: handleChange,
  111. variant: 'standard',
  112. },
  113. };
  114. render(
  115. <CustomInput
  116. type="icon"
  117. iconConfig={mockIconConfig}
  118. checkValid={() => true}
  119. />,
  120. container
  121. );
  122. expect(container.querySelectorAll('.grid').length).toBe(3);
  123. expect(container.querySelectorAll('.icon').length).toBe(1);
  124. expect(container.querySelector('.text-label').textContent).toBe(
  125. 'icon text'
  126. );
  127. const input = container.querySelector('.text-input');
  128. fireEvent.change(input, { target: { value: 'trigger change' } });
  129. expect(handleChange).toHaveBeenCalledTimes(1);
  130. });
  131. test('test adornmentConfig type input', () => {
  132. const mockBlurFunc = jest.fn();
  133. const mockAdornmentConfig: IAdornmentConfig = {
  134. label: 'adornment',
  135. icon: <div className="adornment-icon"></div>,
  136. isPasswordType: false,
  137. key: 'adornment',
  138. onInputBlur: mockBlurFunc,
  139. };
  140. render(
  141. <CustomInput
  142. type="adornment"
  143. adornmentConfig={mockAdornmentConfig}
  144. checkValid={() => true}
  145. />,
  146. container
  147. );
  148. expect(container.querySelector('.label').textContent).toBe('adornment');
  149. expect(container.querySelector('.type').textContent).toBe('text');
  150. expect(container.querySelectorAll('.adornment-icon').length).toBe(1);
  151. const input = container.querySelector('.input');
  152. input.focus();
  153. input.blur();
  154. expect(mockBlurFunc).toHaveBeenCalledTimes(1);
  155. });
  156. test('test default type input', () => {
  157. const mockTextConfig: ITextfieldConfig = {
  158. label: 'default',
  159. key: 'default',
  160. variant: 'standard',
  161. };
  162. act(() => {
  163. render(<CustomInput textConfig={mockTextConfig} />, container);
  164. });
  165. expect(container.querySelector('.text-label').textContent).toBe('default');
  166. });
  167. });