Design system.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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