Design system.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

239 řádky
5.4 KiB

  1. import * as React from 'react';
  2. import {
  3. cleanup,
  4. render,
  5. screen,
  6. } from '@testing-library/react';
  7. import userEvent from '@testing-library/user-event';
  8. import { TextControl } from '@tesseract-design/web-base';
  9. import {
  10. vi,
  11. expect,
  12. describe,
  13. it,
  14. afterEach,
  15. } from 'vitest';
  16. import matchers from '@testing-library/jest-dom/matchers';
  17. import {
  18. MaskedTextInput,
  19. MaskedTextInputDerivedElement,
  20. } from '.';
  21. expect.extend(matchers);
  22. describe('MaskedTextInput', () => {
  23. afterEach(() => {
  24. cleanup();
  25. });
  26. it('renders a password input', () => {
  27. render(
  28. <MaskedTextInput />,
  29. );
  30. const textbox = screen.getByTestId('input');
  31. expect(textbox).toBeInTheDocument();
  32. expect(textbox).toHaveProperty('type', 'password');
  33. });
  34. it('renders a border', () => {
  35. render(
  36. <MaskedTextInput
  37. border
  38. />,
  39. );
  40. const border = screen.getByTestId('border');
  41. expect(border).toBeInTheDocument();
  42. });
  43. it('renders a label', () => {
  44. render(
  45. <MaskedTextInput
  46. label="foo"
  47. />,
  48. );
  49. const textbox = screen.getByLabelText('foo');
  50. expect(textbox).toBeInTheDocument();
  51. const label = screen.getByTestId('label');
  52. expect(label).toHaveTextContent('foo');
  53. });
  54. it('renders a hidden label', () => {
  55. render(
  56. <MaskedTextInput
  57. label="foo"
  58. hiddenLabel
  59. />,
  60. );
  61. const textbox = screen.getByLabelText('foo');
  62. expect(textbox).toBeInTheDocument();
  63. const label = screen.queryByTestId('label');
  64. expect(label).toBeInTheDocument();
  65. expect(label).toHaveClass('sr-only');
  66. });
  67. it('renders a hint', () => {
  68. render(
  69. <MaskedTextInput
  70. hint="foo"
  71. />,
  72. );
  73. const hint = screen.getByTestId('hint');
  74. expect(hint).toBeInTheDocument();
  75. });
  76. it('renders an indicator', () => {
  77. render(
  78. <MaskedTextInput
  79. enhanced
  80. />,
  81. );
  82. const indicator = screen.getByTestId('indicator');
  83. expect(indicator).toBeInTheDocument();
  84. });
  85. describe.each`
  86. size | inputClassName | hintClassName | indicatorClassName
  87. ${'small'} | ${'h-10'} | ${'pr-10'} | ${'w-10'}
  88. ${'medium'} | ${'h-12'} | ${'pr-12'} | ${'w-12'}
  89. ${'large'} | ${'h-16'} | ${'pr-16'} | ${'w-16'}
  90. `('on $size size', ({
  91. size,
  92. inputClassName,
  93. hintClassName,
  94. indicatorClassName,
  95. }: {
  96. size: TextControl.Size,
  97. inputClassName: string,
  98. hintClassName: string,
  99. indicatorClassName: string,
  100. }) => {
  101. it('renders input styles', () => {
  102. render(
  103. <MaskedTextInput
  104. size={size}
  105. />,
  106. );
  107. const input = screen.getByTestId('input');
  108. expect(input).toHaveClass(inputClassName);
  109. });
  110. it('renders label styles with indicator', () => {
  111. render(
  112. <MaskedTextInput
  113. size={size}
  114. label="foo"
  115. enhanced
  116. />,
  117. );
  118. const label = screen.getByTestId('label');
  119. expect(label).toHaveClass(hintClassName);
  120. });
  121. it('renders hint styles when indicator is present', () => {
  122. render(
  123. <MaskedTextInput
  124. size={size}
  125. hint="hint"
  126. enhanced
  127. />,
  128. );
  129. const hint = screen.getByTestId('hint');
  130. expect(hint).toHaveClass(hintClassName);
  131. });
  132. it('renders indicator styles', () => {
  133. render(
  134. <MaskedTextInput
  135. size={size}
  136. enhanced
  137. />,
  138. );
  139. const indicator = screen.getByTestId('indicator');
  140. expect(indicator).toHaveClass(indicatorClassName);
  141. });
  142. });
  143. it('renders a block textbox', () => {
  144. render(
  145. <MaskedTextInput
  146. block
  147. />,
  148. );
  149. const base = screen.getByTestId('base');
  150. expect(base).toHaveClass('block');
  151. });
  152. describe.each`
  153. variant | inputClassName | hintClassName
  154. ${'default'} | ${'pl-4'} | ${'bottom-0 pl-4 pb-1'}
  155. ${'alternate'} | ${'pl-1.5 pt-4'} | ${'top-0.5'}
  156. `('on $variant style', ({
  157. variant,
  158. inputClassName,
  159. hintClassName,
  160. }: {
  161. variant: TextControl.Variant,
  162. inputClassName: string,
  163. hintClassName: string,
  164. }) => {
  165. it('renders input styles', () => {
  166. render(
  167. <MaskedTextInput
  168. variant={variant}
  169. />,
  170. );
  171. const input = screen.getByTestId('input');
  172. expect(input).toHaveClass(inputClassName);
  173. });
  174. it('renders hint styles', () => {
  175. render(
  176. <MaskedTextInput
  177. variant={variant}
  178. hint="hint"
  179. />,
  180. );
  181. const hint = screen.getByTestId('hint');
  182. expect(hint).toHaveClass(hintClassName);
  183. });
  184. });
  185. it('handles change events', async () => {
  186. const onChange = vi.fn().mockImplementationOnce(
  187. (e: React.ChangeEvent<MaskedTextInputDerivedElement>) => {
  188. e.preventDefault();
  189. },
  190. );
  191. render(
  192. <MaskedTextInput
  193. onChange={onChange}
  194. />,
  195. );
  196. const textbox = screen.getByTestId('input');
  197. await userEvent.type(textbox, 'foobar');
  198. expect(onChange).toBeCalled();
  199. });
  200. it('handles input events', async () => {
  201. const onInput = vi.fn().mockImplementationOnce(
  202. (e: React.SyntheticEvent<MaskedTextInputDerivedElement>) => {
  203. e.preventDefault();
  204. },
  205. );
  206. render(
  207. <MaskedTextInput
  208. onInput={onInput}
  209. />,
  210. );
  211. const textbox = screen.getByTestId('input');
  212. await userEvent.type(textbox, 'foobar');
  213. expect(onInput).toBeCalled();
  214. });
  215. });