Design system.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

214 linhas
4.7 KiB

  1. import * as React from 'react';
  2. import {
  3. render,
  4. screen
  5. } from '@testing-library/react';
  6. import userEvent from '@testing-library/user-event';
  7. import * as TextControlBase from '@tesseract-design/web-base-textcontrol';
  8. import { vi } from 'vitest';
  9. import {
  10. TextInput, TextInputType,
  11. } from '.';
  12. vi.mock('@tesseract-design/web-base-textcontrol');
  13. describe('TextInput', () => {
  14. it('renders a textbox', () => {
  15. render(
  16. <TextInput />
  17. );
  18. const textbox = screen.getByRole('textbox');
  19. expect(textbox).toBeInTheDocument();
  20. expect(textbox).toHaveProperty('type', 'text');
  21. });
  22. it('renders a border', () => {
  23. render(
  24. <TextInput
  25. border
  26. />
  27. );
  28. const border = screen.getByTestId('border');
  29. expect(border).toBeInTheDocument();
  30. });
  31. it('renders a label', () => {
  32. render(
  33. <TextInput
  34. label="foo"
  35. />
  36. );
  37. const textbox = screen.getByLabelText('foo');
  38. expect(textbox).toBeInTheDocument();
  39. const label = screen.getByTestId('label');
  40. expect(label).toHaveTextContent('foo');
  41. });
  42. it('renders a hidden label', () => {
  43. render(
  44. <TextInput
  45. label="foo"
  46. hiddenLabel
  47. />
  48. );
  49. const textbox = screen.getByLabelText('foo');
  50. expect(textbox).toBeInTheDocument();
  51. const label = screen.queryByTestId('label');
  52. expect(label).toBeNull();
  53. });
  54. it('renders a hint', () => {
  55. render(
  56. <TextInput
  57. hint="foo"
  58. />
  59. );
  60. const hint = screen.getByTestId('hint');
  61. expect(hint).toBeInTheDocument();
  62. });
  63. it('renders an indicator', () => {
  64. render(
  65. <TextInput
  66. indicator={
  67. <div data-testid="indicator" />
  68. }
  69. />
  70. );
  71. const indicator = screen.getByTestId('indicator');
  72. expect(indicator).toBeInTheDocument();
  73. });
  74. describe.each(Object.values(TextControlBase.TextControlSize))('on %s size', (size) => {
  75. it('renders input styles', () => {
  76. render(
  77. <TextInput
  78. size={size}
  79. />
  80. );
  81. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  82. size,
  83. }));
  84. });
  85. it('renders hint styles', () => {
  86. render(
  87. <TextInput
  88. size={size}
  89. hint="hint"
  90. />
  91. );
  92. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  93. size,
  94. }));
  95. });
  96. it('renders indicator styles', () => {
  97. render(
  98. <TextInput
  99. size={size}
  100. indicator={
  101. <div data-testid="indicator" />
  102. }
  103. />
  104. );
  105. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  106. size,
  107. }));
  108. });
  109. });
  110. it('renders a block textbox', () => {
  111. render(
  112. <TextInput
  113. block
  114. />
  115. );
  116. expect(TextControlBase.Root).toBeCalledWith(expect.objectContaining({
  117. block: true,
  118. }));
  119. });
  120. it.each(Object.values(TextInputType))('renders a textbox with type %s', (buttonType) => {
  121. render(
  122. <TextInput
  123. type={buttonType}
  124. />
  125. );
  126. const textbox: HTMLButtonElement = screen.getByTestId('input');
  127. expect(textbox).toHaveProperty('type', buttonType);
  128. });
  129. describe.each(Object.values(TextControlBase.TextControlStyle))('on %s style', (style) => {
  130. it('renders input styles', () => {
  131. render(
  132. <TextInput
  133. style={style}
  134. />
  135. );
  136. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  137. style,
  138. }));
  139. });
  140. it('renders hint styles', () => {
  141. render(
  142. <TextInput
  143. style={style}
  144. hint="hint"
  145. />
  146. );
  147. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  148. style,
  149. }));
  150. });
  151. it('renders indicator styles', () => {
  152. render(
  153. <TextInput
  154. style={style}
  155. indicator={
  156. <div
  157. data-testid="indicator"
  158. />
  159. }
  160. />
  161. );
  162. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  163. style,
  164. }));
  165. });
  166. });
  167. it('handles change events', () => {
  168. const onChange = vi.fn().mockImplementationOnce((e) => { e.preventDefault() });
  169. render(
  170. <TextInput
  171. onChange={onChange}
  172. />
  173. );
  174. const textbox: HTMLInputElement = screen.getByRole('textbox');
  175. userEvent.type(textbox, 'foobar');
  176. expect(onChange).toBeCalled();
  177. });
  178. it('handles input events', () => {
  179. const onInput = vi.fn().mockImplementationOnce((e) => { e.preventDefault() });
  180. render(
  181. <TextInput
  182. onInput={onInput}
  183. />
  184. );
  185. const textbox: HTMLInputElement = screen.getByTestId('input');
  186. userEvent.type(textbox, 'foobar');
  187. expect(onInput).toBeCalled();
  188. });
  189. });