Website for showcasing all features of Tesseract Web.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

199 lignes
4.4 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. MaskedTextInput
  11. } from '.';
  12. jest.mock('@tesseract-design/web-base-textcontrol');
  13. describe('MaskedTextInput', () => {
  14. it('should render an input', () => {
  15. render(
  16. <MaskedTextInput />
  17. );
  18. const textbox: HTMLInputElement = screen.getByTestId('input');
  19. expect(textbox).toBeInTheDocument();
  20. expect(textbox).toHaveProperty('type', 'password');
  21. });
  22. it('should render a border', () => {
  23. render(
  24. <MaskedTextInput
  25. border
  26. />
  27. );
  28. const border = screen.getByTestId('border');
  29. expect(border).toBeInTheDocument();
  30. });
  31. it('should render a label', () => {
  32. render(
  33. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  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. <MaskedTextInput
  169. onChange={onChange}
  170. />
  171. );
  172. const textbox: HTMLInputElement = screen.getByTestId('input');
  173. userEvent.type(textbox, 'foobar');
  174. expect(onChange).toBeCalled();
  175. });
  176. });