Website for showcasing all features of Tesseract Web.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

283 regels
6.2 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 * as TextControlBase from '@tesseract-design/web-base-textcontrol';
  8. import {
  9. DropdownSelect
  10. } from '.';
  11. jest.mock('@tesseract-design/web-base-textcontrol');
  12. describe('DropdownSelect', () => {
  13. it('should render a combobox', () => {
  14. render(<DropdownSelect />);
  15. const combobox = screen.getByRole('combobox');
  16. expect(combobox).toBeInTheDocument();
  17. });
  18. it('should render a border', () => {
  19. render(
  20. <DropdownSelect
  21. border
  22. />
  23. );
  24. const border = screen.getByTestId('border');
  25. expect(border).toBeInTheDocument();
  26. });
  27. it('should render a label', () => {
  28. render(
  29. <DropdownSelect
  30. label="foo"
  31. />
  32. );
  33. const combobox = screen.getByLabelText('foo');
  34. expect(combobox).toBeInTheDocument();
  35. const label = screen.getByTestId('label');
  36. expect(label).toHaveTextContent('foo');
  37. });
  38. it('should render a hidden label', () => {
  39. render(
  40. <DropdownSelect
  41. label="foo"
  42. hiddenLabel
  43. />
  44. );
  45. const combobox = screen.getByLabelText('foo');
  46. expect(combobox).toBeInTheDocument();
  47. const label = screen.queryByTestId('label');
  48. expect(label).toBeNull();
  49. });
  50. it('should render a hint', () => {
  51. render(
  52. <DropdownSelect
  53. hint="foo"
  54. />
  55. );
  56. const hint = screen.getByTestId('hint');
  57. expect(hint).toBeInTheDocument();
  58. });
  59. it('should not render invalid options', () => {
  60. render(
  61. <DropdownSelect
  62. options={[
  63. {
  64. label: 'foo',
  65. },
  66. {
  67. label: 'bar',
  68. }
  69. ]}
  70. />
  71. );
  72. const combobox = screen.getByRole('combobox');
  73. expect(combobox.children).toHaveLength(0);
  74. });
  75. it('should render valid options', () => {
  76. render(
  77. <DropdownSelect
  78. options={[
  79. {
  80. label: 'foo',
  81. value: 'foo',
  82. },
  83. {
  84. label: 'bar',
  85. value: 'bar',
  86. }
  87. ]}
  88. />
  89. );
  90. const combobox = screen.getByRole('combobox');
  91. expect(combobox.children).toHaveLength(2);
  92. });
  93. it('should render shallow option groups', () => {
  94. render(
  95. <DropdownSelect
  96. options={[
  97. {
  98. label: 'foo',
  99. children: [
  100. {
  101. label: 'baz',
  102. value: 'baz',
  103. },
  104. ],
  105. },
  106. {
  107. label: 'bar',
  108. children: [
  109. {
  110. label: 'quux',
  111. value: 'quux',
  112. },
  113. {
  114. label: 'quuux',
  115. value: 'quuux',
  116. },
  117. ],
  118. }
  119. ]}
  120. />
  121. );
  122. const combobox = screen.getByRole('combobox');
  123. expect(combobox.children).toHaveLength(2);
  124. expect(combobox.children[0].children).toHaveLength(1);
  125. expect(combobox.children[1].children).toHaveLength(2);
  126. });
  127. it('should render deep option groups', () => {
  128. render(
  129. <DropdownSelect
  130. options={[
  131. {
  132. label: 'foo',
  133. children: [
  134. {
  135. label: 'baz',
  136. children: [
  137. {
  138. label: 'quuuux',
  139. value: 'quuuux',
  140. },
  141. {
  142. label: 'quuuuux',
  143. value: 'quuuuux',
  144. },
  145. {
  146. label: 'quuuuuux',
  147. value: 'quuuuuux',
  148. },
  149. ],
  150. },
  151. ],
  152. },
  153. {
  154. label: 'bar',
  155. children: [
  156. {
  157. label: 'quux',
  158. value: 'quux',
  159. },
  160. {
  161. label: 'quuux',
  162. value: 'quuux',
  163. },
  164. ],
  165. }
  166. ]}
  167. />
  168. );
  169. const combobox = screen.getByRole('combobox');
  170. expect(combobox.children).toHaveLength(2);
  171. expect(combobox.children[0].children).toHaveLength(4);
  172. expect(combobox.children[1].children).toHaveLength(2);
  173. });
  174. describe.each([
  175. TextControlBase.TextControlSize.SMALL,
  176. TextControlBase.TextControlSize.MEDIUM,
  177. TextControlBase.TextControlSize.LARGE,
  178. ])('on %s size', (size) => {
  179. it('should render input styles', () => {
  180. render(
  181. <DropdownSelect
  182. size={size}
  183. />
  184. );
  185. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  186. size,
  187. }));
  188. });
  189. it('should render hint styles', () => {
  190. render(
  191. <DropdownSelect
  192. size={size}
  193. hint="hint"
  194. />
  195. );
  196. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  197. size,
  198. }));
  199. });
  200. it('should render indicator styles', () => {
  201. render(
  202. <DropdownSelect
  203. size={size}
  204. />
  205. );
  206. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  207. size,
  208. }));
  209. });
  210. });
  211. it('should render a block textbox', () => {
  212. render(
  213. <DropdownSelect
  214. block
  215. />
  216. );
  217. expect(TextControlBase.Root).toBeCalledWith(expect.objectContaining({
  218. block: true,
  219. }));
  220. });
  221. describe.each([
  222. TextControlBase.TextControlStyle.DEFAULT,
  223. TextControlBase.TextControlStyle.ALTERNATE,
  224. ])('on %s style', (style) => {
  225. it('should render input styles', () => {
  226. render(
  227. <DropdownSelect
  228. style={style}
  229. />
  230. );
  231. expect(TextControlBase.Input).toBeCalledWith(expect.objectContaining({
  232. style,
  233. }));
  234. });
  235. it('should render hint styles', () => {
  236. render(
  237. <DropdownSelect
  238. style={style}
  239. hint="hint"
  240. />
  241. );
  242. expect(TextControlBase.HintWrapper).toBeCalledWith(expect.objectContaining({
  243. style,
  244. }));
  245. });
  246. it('should render indicator styles', () => {
  247. render(
  248. <DropdownSelect
  249. style={style}
  250. />
  251. );
  252. expect(TextControlBase.IndicatorWrapper).toBeCalledWith(expect.objectContaining({
  253. style,
  254. }));
  255. });
  256. });
  257. });