Website for showcasing all features of Tesseract Web.
Você não pode selecionar mais de 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.
 
 

212 linhas
4.6 KiB

  1. import * as React from 'react';
  2. import {
  3. render,
  4. screen
  5. } from '@testing-library/react';
  6. import '@testing-library/jest-dom';
  7. import userEvent from '@testing-library/user-event';
  8. import * as TextControlBase from '@tesseract-design/web-base-textcontrol';
  9. import {
  10. TextInput, TextInputType,
  11. } from '.';
  12. jest.mock('@tesseract-design/web-base-textcontrol');
  13. describe('TextInput', () => {
  14. it('should render 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('should render a border', () => {
  23. render(
  24. <TextInput
  25. border
  26. />
  27. );
  28. const border = screen.getByTestId('border');
  29. expect(border).toBeInTheDocument();
  30. });
  31. it('should render 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('should render 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('should render a hint', () => {
  55. render(
  56. <TextInput
  57. hint="foo"
  58. />
  59. );
  60. const hint = screen.getByTestId('hint');
  61. expect(hint).toBeInTheDocument();
  62. });
  63. it('should render 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([
  75. TextControlBase.TextControlSize.SMALL,
  76. TextControlBase.TextControlSize.MEDIUM,
  77. TextControlBase.TextControlSize.LARGE,
  78. ])('on %s size', (size) => {
  79. it('should render input styles', () => {
  80. render(
  81. <TextInput
  82. size={size}
  83. />
  84. );
  85. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  86. size,
  87. }));
  88. });
  89. it('should render hint styles', () => {
  90. render(
  91. <TextInput
  92. size={size}
  93. hint="hint"
  94. />
  95. );
  96. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  97. size,
  98. }));
  99. });
  100. it('should render indicator styles', () => {
  101. render(
  102. <TextInput
  103. size={size}
  104. indicator={
  105. <div data-testid="indicator" />
  106. }
  107. />
  108. );
  109. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  110. size,
  111. }));
  112. });
  113. });
  114. it('should render a block textbox', () => {
  115. render(
  116. <TextInput
  117. block
  118. />
  119. );
  120. expect(TextControlBase.Root).toBeCalledWith(expect.objectContaining({
  121. block: true,
  122. }));
  123. });
  124. it.each([
  125. TextInputType.TEXT,
  126. TextInputType.SEARCH,
  127. ])('should render a textbox with type %s', (buttonType) => {
  128. render(
  129. <TextInput
  130. type={buttonType}
  131. />
  132. );
  133. const textbox: HTMLButtonElement = screen.getByTestId('input');
  134. expect(textbox).toHaveProperty('type', buttonType);
  135. });
  136. describe.each([
  137. TextControlBase.TextControlStyle.DEFAULT,
  138. TextControlBase.TextControlStyle.ALTERNATE,
  139. ])('on %s style', (style) => {
  140. it('should render input styles', () => {
  141. render(
  142. <TextInput
  143. style={style}
  144. />
  145. );
  146. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  147. style,
  148. }));
  149. });
  150. it('should render hint styles', () => {
  151. render(
  152. <TextInput
  153. style={style}
  154. hint="hint"
  155. />
  156. );
  157. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  158. style,
  159. }));
  160. });
  161. it('should render indicator styles', () => {
  162. render(
  163. <TextInput
  164. style={style}
  165. indicator={
  166. <div
  167. data-testid="indicator"
  168. />
  169. }
  170. />
  171. );
  172. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  173. style,
  174. }));
  175. });
  176. });
  177. it('should handle change events', () => {
  178. const onChange = jest.fn();
  179. render(
  180. <TextInput
  181. onChange={onChange}
  182. />
  183. );
  184. const textbox: HTMLInputElement = screen.getByRole('textbox');
  185. userEvent.type(textbox, 'foobar');
  186. expect(onChange).toBeCalled();
  187. });
  188. });