Website for showcasing all features of Tesseract Web.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

211 строки
4.7 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. EmailAddressInput,
  11. } from '.';
  12. jest.mock('@tesseract-design/web-base-textcontrol');
  13. describe('EmailAddressInput', () => {
  14. it('should render a textbox', () => {
  15. render(
  16. <EmailAddressInput />
  17. );
  18. const textbox = screen.getByRole('textbox');
  19. expect(textbox).toBeInTheDocument();
  20. expect(textbox).toHaveProperty('type', 'email');
  21. });
  22. it('should render a border', () => {
  23. render(
  24. <EmailAddressInput
  25. border
  26. />
  27. );
  28. const border = screen.getByTestId('border');
  29. expect(border).toBeInTheDocument();
  30. });
  31. it('should render a label', () => {
  32. render(
  33. <EmailAddressInput
  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. <EmailAddressInput
  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. <EmailAddressInput
  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. <EmailAddressInput
  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. <EmailAddressInput
  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. <EmailAddressInput
  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. <EmailAddressInput
  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. <EmailAddressInput
  117. block
  118. />
  119. );
  120. expect(TextControlBase.Root).toBeCalledWith(expect.objectContaining({
  121. block: true,
  122. }));
  123. });
  124. describe.each([
  125. TextControlBase.TextControlStyle.DEFAULT,
  126. TextControlBase.TextControlStyle.ALTERNATE,
  127. ])('on %s style', (style) => {
  128. it('should render input styles', () => {
  129. render(
  130. <EmailAddressInput
  131. style={style}
  132. />
  133. );
  134. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  135. style,
  136. }));
  137. });
  138. it('should render hint styles', () => {
  139. render(
  140. <EmailAddressInput
  141. style={style}
  142. hint="hint"
  143. />
  144. );
  145. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  146. style,
  147. }));
  148. });
  149. it('should render indicator styles', () => {
  150. render(
  151. <EmailAddressInput
  152. style={style}
  153. indicator={
  154. <div
  155. data-testid="indicator"
  156. />
  157. }
  158. />
  159. );
  160. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  161. style,
  162. }));
  163. });
  164. });
  165. it('should handle change events', () => {
  166. const onChange = jest.fn();
  167. render(
  168. <EmailAddressInput
  169. onChange={onChange}
  170. />
  171. );
  172. const textbox: HTMLInputElement = screen.getByRole('textbox');
  173. userEvent.type(textbox, 'foobar');
  174. expect(onChange).toBeCalled();
  175. });
  176. it('should handle input events', () => {
  177. const onInput = jest.fn();
  178. render(
  179. <EmailAddressInput
  180. onInput={onInput}
  181. />
  182. );
  183. const textbox: HTMLInputElement = screen.getByTestId('input');
  184. userEvent.type(textbox, 'foobar');
  185. expect(onInput).toBeCalled();
  186. });
  187. });