@dxc-technology/halstack-react 0.0.0-2d619b4 → 0.0.0-2edb175

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (294) hide show
  1. package/BackgroundColorContext.js +50 -0
  2. package/ThemeContext.js +246 -0
  3. package/{dist/select/Select.js → V3Select/V3Select.js} +38 -132
  4. package/V3Select/index.d.ts +27 -0
  5. package/{dist/textarea/Textarea.js → V3Textarea/V3Textarea.js} +14 -18
  6. package/V3Textarea/index.d.ts +27 -0
  7. package/accordion/Accordion.d.ts +4 -0
  8. package/{dist/accordion → accordion}/Accordion.js +35 -130
  9. package/accordion/types.d.ts +68 -0
  10. package/accordion/types.js +5 -0
  11. package/accordion-group/AccordionGroup.d.ts +7 -0
  12. package/accordion-group/AccordionGroup.js +170 -0
  13. package/accordion-group/types.d.ts +72 -0
  14. package/accordion-group/types.js +5 -0
  15. package/alert/Alert.d.ts +4 -0
  16. package/{dist/alert → alert}/Alert.js +41 -154
  17. package/alert/types.d.ts +49 -0
  18. package/alert/types.js +5 -0
  19. package/badge/Badge.js +59 -0
  20. package/{dist/box → box}/Box.js +9 -13
  21. package/box/Box.stories.jsx +132 -0
  22. package/box/index.d.ts +25 -0
  23. package/button/Button.d.ts +4 -0
  24. package/{dist/button → button}/Button.js +16 -72
  25. package/button/Button.stories.tsx +293 -0
  26. package/button/types.d.ts +57 -0
  27. package/button/types.js +5 -0
  28. package/card/Card.d.ts +4 -0
  29. package/{dist/card → card}/Card.js +32 -121
  30. package/card/types.d.ts +69 -0
  31. package/card/types.js +5 -0
  32. package/checkbox/Checkbox.d.ts +4 -0
  33. package/{dist/checkbox → checkbox}/Checkbox.js +16 -63
  34. package/checkbox/Checkbox.stories.tsx +192 -0
  35. package/checkbox/types.d.ts +60 -0
  36. package/checkbox/types.js +5 -0
  37. package/{dist/chip → chip}/Chip.js +17 -61
  38. package/chip/index.d.ts +22 -0
  39. package/{dist/common → common}/OpenSans.css +0 -0
  40. package/{dist/common → common}/RequiredComponent.js +3 -11
  41. package/{dist/common → common}/fonts/OpenSans-Bold.ttf +0 -0
  42. package/{dist/common → common}/fonts/OpenSans-BoldItalic.ttf +0 -0
  43. package/{dist/common → common}/fonts/OpenSans-ExtraBold.ttf +0 -0
  44. package/{dist/common → common}/fonts/OpenSans-ExtraBoldItalic.ttf +0 -0
  45. package/{dist/common → common}/fonts/OpenSans-Italic.ttf +0 -0
  46. package/{dist/common → common}/fonts/OpenSans-Light.ttf +0 -0
  47. package/{dist/common → common}/fonts/OpenSans-LightItalic.ttf +0 -0
  48. package/{dist/common → common}/fonts/OpenSans-Regular.ttf +0 -0
  49. package/{dist/common → common}/fonts/OpenSans-SemiBold.ttf +0 -0
  50. package/{dist/common → common}/fonts/OpenSans-SemiBoldItalic.ttf +0 -0
  51. package/{dist/common → common}/utils.js +0 -0
  52. package/{dist/common → common}/variables.js +234 -99
  53. package/{dist/date → date}/Date.js +16 -22
  54. package/date/index.d.ts +27 -0
  55. package/date-input/DateInput.d.ts +4 -0
  56. package/{dist/new-date/NewDate.js → date-input/DateInput.js} +81 -120
  57. package/date-input/types.d.ts +100 -0
  58. package/date-input/types.js +5 -0
  59. package/dialog/Dialog.d.ts +4 -0
  60. package/{dist/dialog → dialog}/Dialog.js +20 -73
  61. package/dialog/types.d.ts +43 -0
  62. package/dialog/types.js +5 -0
  63. package/dropdown/Dropdown.d.ts +4 -0
  64. package/{dist/dropdown → dropdown}/Dropdown.js +44 -171
  65. package/dropdown/types.d.ts +89 -0
  66. package/dropdown/types.js +5 -0
  67. package/{dist/file-input → file-input}/FileInput.js +57 -166
  68. package/file-input/FileItem.js +193 -0
  69. package/file-input/index.d.ts +81 -0
  70. package/footer/Footer.d.ts +4 -0
  71. package/footer/Footer.js +266 -0
  72. package/footer/Footer.stories.jsx +151 -0
  73. package/footer/Icons.js +77 -0
  74. package/footer/types.d.ts +61 -0
  75. package/footer/types.js +5 -0
  76. package/header/Header.d.ts +7 -0
  77. package/header/Header.js +324 -0
  78. package/header/Icons.js +34 -0
  79. package/header/types.d.ts +45 -0
  80. package/header/types.js +5 -0
  81. package/{dist/heading → heading}/Heading.js +30 -72
  82. package/heading/index.d.ts +17 -0
  83. package/input-text/Icons.js +22 -0
  84. package/{dist/input-text → input-text}/InputText.js +37 -133
  85. package/input-text/index.d.ts +36 -0
  86. package/{dist/layout → layout}/ApplicationLayout.js +35 -131
  87. package/layout/Icons.js +55 -0
  88. package/{dist/link → link}/Link.js +18 -72
  89. package/link/index.d.ts +23 -0
  90. package/main.d.ts +44 -0
  91. package/{dist/main.js → main.js} +100 -96
  92. package/{dist/number/Number.js → number-input/NumberInput.js} +10 -20
  93. package/number-input/NumberInput.stories.jsx +115 -0
  94. package/{dist/number/NumberContext.js → number-input/NumberInputContext.js} +2 -2
  95. package/{dist/number → number-input}/index.d.ts +27 -9
  96. package/package.json +32 -25
  97. package/paginator/Icons.js +66 -0
  98. package/paginator/Paginator.d.ts +4 -0
  99. package/paginator/Paginator.js +198 -0
  100. package/paginator/Paginator.stories.tsx +63 -0
  101. package/paginator/types.d.ts +38 -0
  102. package/paginator/types.js +5 -0
  103. package/{dist/password/Password.js → password-input/PasswordInput.js} +26 -27
  104. package/password-input/PasswordInput.stories.jsx +131 -0
  105. package/{dist/password → password-input}/index.d.ts +30 -16
  106. package/progress-bar/ProgressBar.d.ts +4 -0
  107. package/{dist/progress-bar → progress-bar}/ProgressBar.js +20 -92
  108. package/progress-bar/types.d.ts +37 -0
  109. package/progress-bar/types.js +5 -0
  110. package/radio/Radio.d.ts +4 -0
  111. package/{dist/radio → radio}/Radio.js +15 -50
  112. package/radio/types.d.ts +54 -0
  113. package/radio/types.js +5 -0
  114. package/{dist/resultsetTable → resultsetTable}/ResultsetTable.js +35 -119
  115. package/resultsetTable/index.d.ts +19 -0
  116. package/select/Select.js +865 -0
  117. package/select/index.d.ts +131 -0
  118. package/{dist/sidenav → sidenav}/Sidenav.js +15 -49
  119. package/sidenav/index.d.ts +13 -0
  120. package/slider/Slider.d.ts +4 -0
  121. package/{dist/slider → slider}/Slider.js +90 -103
  122. package/slider/types.d.ts +83 -0
  123. package/slider/types.js +5 -0
  124. package/{dist/spinner → spinner}/Spinner.js +43 -157
  125. package/spinner/Spinner.stories.jsx +102 -0
  126. package/spinner/index.d.ts +17 -0
  127. package/switch/Switch.d.ts +4 -0
  128. package/{dist/switch → switch}/Switch.js +26 -69
  129. package/switch/types.d.ts +58 -0
  130. package/switch/types.js +5 -0
  131. package/{dist/table → table}/Table.js +10 -24
  132. package/table/Table.stories.jsx +276 -0
  133. package/table/index.d.ts +13 -0
  134. package/tabs/Tabs.d.ts +4 -0
  135. package/tabs/Tabs.js +211 -0
  136. package/tabs/types.d.ts +71 -0
  137. package/tabs/types.js +5 -0
  138. package/{dist/tag → tag}/Tag.js +22 -96
  139. package/tag/index.d.ts +24 -0
  140. package/text-input/TextInput.js +825 -0
  141. package/{dist/new-input-text → text-input}/index.d.ts +33 -18
  142. package/{dist/new-textarea/NewTextarea.js → textarea/Textarea.js} +71 -119
  143. package/textarea/Textarea.stories.jsx +135 -0
  144. package/{dist/new-textarea → textarea}/index.d.ts +34 -18
  145. package/{dist/toggle → toggle}/Toggle.js +15 -49
  146. package/toggle/index.d.ts +21 -0
  147. package/toggle-group/ToggleGroup.js +243 -0
  148. package/toggle-group/index.d.ts +21 -0
  149. package/{dist/upload → upload}/Upload.js +11 -15
  150. package/upload/buttons-upload/ButtonsUpload.js +111 -0
  151. package/upload/buttons-upload/Icons.js +40 -0
  152. package/upload/dragAndDropArea/DragAndDropArea.js +225 -0
  153. package/upload/dragAndDropArea/Icons.js +39 -0
  154. package/upload/file-upload/FileToUpload.js +115 -0
  155. package/upload/file-upload/Icons.js +66 -0
  156. package/{dist/upload → upload}/files-upload/FilesToUpload.js +12 -26
  157. package/upload/index.d.ts +15 -0
  158. package/upload/transaction/Icons.js +160 -0
  159. package/upload/transaction/Transaction.js +104 -0
  160. package/upload/transactions/Transactions.js +94 -0
  161. package/{dist/useTheme.js → useTheme.js} +0 -0
  162. package/wizard/Icons.js +65 -0
  163. package/{dist/wizard → wizard}/Wizard.js +32 -172
  164. package/wizard/index.d.ts +18 -0
  165. package/README.md +0 -66
  166. package/babel.config.js +0 -8
  167. package/dist/BackgroundColorContext.js +0 -46
  168. package/dist/ThemeContext.js +0 -240
  169. package/dist/accordion-group/AccordionGroup.js +0 -186
  170. package/dist/alert/index.d.ts +0 -51
  171. package/dist/badge/Badge.js +0 -63
  172. package/dist/checkbox/Checkbox.stories.js +0 -144
  173. package/dist/checkbox/readme.md +0 -116
  174. package/dist/date/Date.stories.js +0 -205
  175. package/dist/date/readme.md +0 -73
  176. package/dist/file-input/FileItem.js +0 -263
  177. package/dist/footer/Footer.js +0 -395
  178. package/dist/footer/Footer.stories.js +0 -94
  179. package/dist/footer/dxc_logo.svg +0 -15
  180. package/dist/footer/readme.md +0 -41
  181. package/dist/header/Header.js +0 -403
  182. package/dist/header/Header.stories.js +0 -176
  183. package/dist/header/close_icon.svg +0 -1
  184. package/dist/header/dxc_logo_black.svg +0 -8
  185. package/dist/header/hamb_menu_black.svg +0 -1
  186. package/dist/header/hamb_menu_white.svg +0 -1
  187. package/dist/header/readme.md +0 -33
  188. package/dist/input-text/InputText.stories.js +0 -209
  189. package/dist/input-text/error.svg +0 -1
  190. package/dist/input-text/readme.md +0 -91
  191. package/dist/layout/facebook.svg +0 -45
  192. package/dist/layout/linkedin.svg +0 -50
  193. package/dist/layout/twitter.svg +0 -53
  194. package/dist/link/readme.md +0 -51
  195. package/dist/main.d.ts +0 -6
  196. package/dist/new-date/index.d.ts +0 -86
  197. package/dist/new-input-text/NewInputText.js +0 -977
  198. package/dist/paginator/Paginator.js +0 -289
  199. package/dist/paginator/images/next.svg +0 -3
  200. package/dist/paginator/images/nextPage.svg +0 -3
  201. package/dist/paginator/images/previous.svg +0 -3
  202. package/dist/paginator/images/previousPage.svg +0 -3
  203. package/dist/paginator/readme.md +0 -50
  204. package/dist/password/styles.css +0 -3
  205. package/dist/progress-bar/ProgressBar.stories.js +0 -280
  206. package/dist/progress-bar/readme.md +0 -63
  207. package/dist/radio/Radio.stories.js +0 -166
  208. package/dist/radio/readme.md +0 -70
  209. package/dist/resultsetTable/arrow_downward-24px_wht.svg +0 -1
  210. package/dist/resultsetTable/arrow_upward-24px_wht.svg +0 -1
  211. package/dist/resultsetTable/unfold_more-24px_wht.svg +0 -1
  212. package/dist/slider/Slider.stories.js +0 -241
  213. package/dist/slider/readme.md +0 -64
  214. package/dist/spinner/Spinner.stories.js +0 -183
  215. package/dist/spinner/readme.md +0 -65
  216. package/dist/switch/Switch.stories.js +0 -134
  217. package/dist/switch/readme.md +0 -133
  218. package/dist/tabs/Tabs.js +0 -343
  219. package/dist/tabs/Tabs.stories.js +0 -130
  220. package/dist/tabs/readme.md +0 -78
  221. package/dist/tabs-for-sections/TabsForSections.js +0 -92
  222. package/dist/tabs-for-sections/readme.md +0 -78
  223. package/dist/toggle/Toggle.stories.js +0 -297
  224. package/dist/toggle/readme.md +0 -80
  225. package/dist/toggle-group/ToggleGroup.js +0 -223
  226. package/dist/upload/Upload.stories.js +0 -72
  227. package/dist/upload/buttons-upload/ButtonsUpload.js +0 -139
  228. package/dist/upload/buttons-upload/drag-drop-icon.svg +0 -4
  229. package/dist/upload/buttons-upload/upload-button.svg +0 -1
  230. package/dist/upload/dragAndDropArea/DragAndDropArea.js +0 -329
  231. package/dist/upload/dragAndDropArea/upload_drop.svg +0 -4
  232. package/dist/upload/dragAndDropArea/upload_file.svg +0 -4
  233. package/dist/upload/file-upload/FileToUpload.js +0 -184
  234. package/dist/upload/file-upload/audio-icon.svg +0 -4
  235. package/dist/upload/file-upload/close.svg +0 -4
  236. package/dist/upload/file-upload/file-icon.svg +0 -4
  237. package/dist/upload/file-upload/video-icon.svg +0 -4
  238. package/dist/upload/readme.md +0 -37
  239. package/dist/upload/transaction/Transaction.js +0 -175
  240. package/dist/upload/transaction/audio-icon-err.svg +0 -4
  241. package/dist/upload/transaction/audio-icon.svg +0 -4
  242. package/dist/upload/transaction/error-icon.svg +0 -4
  243. package/dist/upload/transaction/file-icon-err.svg +0 -4
  244. package/dist/upload/transaction/file-icon.svg +0 -4
  245. package/dist/upload/transaction/image-icon-err.svg +0 -4
  246. package/dist/upload/transaction/image-icon.svg +0 -4
  247. package/dist/upload/transaction/success-icon.svg +0 -4
  248. package/dist/upload/transaction/video-icon-err.svg +0 -4
  249. package/dist/upload/transaction/video-icon.svg +0 -4
  250. package/dist/upload/transactions/Transactions.js +0 -138
  251. package/dist/wizard/invalid_icon.svg +0 -5
  252. package/dist/wizard/valid_icon.svg +0 -5
  253. package/dist/wizard/validation-wrong.svg +0 -6
  254. package/test/Accordion.test.js +0 -33
  255. package/test/AccordionGroup.test.js +0 -125
  256. package/test/Alert.test.js +0 -53
  257. package/test/Box.test.js +0 -10
  258. package/test/Button.test.js +0 -18
  259. package/test/Card.test.js +0 -30
  260. package/test/Checkbox.test.js +0 -45
  261. package/test/Chip.test.js +0 -25
  262. package/test/Date.test.js +0 -393
  263. package/test/Dialog.test.js +0 -23
  264. package/test/Dropdown.test.js +0 -145
  265. package/test/FileInput.test.js +0 -39
  266. package/test/Footer.test.js +0 -99
  267. package/test/Header.test.js +0 -39
  268. package/test/Heading.test.js +0 -35
  269. package/test/InputText.test.js +0 -240
  270. package/test/Link.test.js +0 -43
  271. package/test/NewDate.test.js +0 -203
  272. package/test/NewInputText.test.js +0 -866
  273. package/test/NewTextarea.test.js +0 -252
  274. package/test/Number.test.js +0 -241
  275. package/test/Paginator.test.js +0 -177
  276. package/test/Password.test.js +0 -76
  277. package/test/ProgressBar.test.js +0 -35
  278. package/test/Radio.test.js +0 -37
  279. package/test/ResultsetTable.test.js +0 -329
  280. package/test/Select.test.js +0 -212
  281. package/test/Sidenav.test.js +0 -45
  282. package/test/Slider.test.js +0 -82
  283. package/test/Spinner.test.js +0 -32
  284. package/test/Switch.test.js +0 -45
  285. package/test/Table.test.js +0 -36
  286. package/test/Tabs.test.js +0 -109
  287. package/test/TabsForSections.test.js +0 -34
  288. package/test/Tag.test.js +0 -32
  289. package/test/TextArea.test.js +0 -52
  290. package/test/ToggleGroup.test.js +0 -81
  291. package/test/Upload.test.js +0 -60
  292. package/test/Wizard.test.js +0 -130
  293. package/test/mocks/pngMock.js +0 -1
  294. package/test/mocks/svgMock.js +0 -1
@@ -1,866 +0,0 @@
1
- import React from "react";
2
- import { render, fireEvent, waitForElementToBeRemoved } from "@testing-library/react";
3
- import userEvent from "@testing-library/user-event";
4
-
5
- import DxcNewInputText from "../src/new-input-text/NewInputText";
6
-
7
- const countries = [
8
- "Afghanistan",
9
- "Albania",
10
- "Algeria",
11
- "Andorra",
12
- "Angola",
13
- "Antigua and Barbuda",
14
- "Bahamas",
15
- "Bahrain",
16
- "Bangladesh",
17
- "Barbados",
18
- "Cabo Verde",
19
- "Cambodia",
20
- "Cameroon",
21
- "Canada",
22
- "Cayman Islands, The",
23
- "Central African Republic",
24
- "Chad",
25
- "Democratic Republic of the Congo",
26
- "Dominican Republic",
27
- "Dominica",
28
- "Denmark",
29
- "Djibouti",
30
- ];
31
-
32
- describe("NewInputText component tests", () => {
33
- test("Renders with correct label", () => {
34
- const { getByText } = render(<DxcNewInputText label="Example label" />);
35
- expect(getByText("Example label")).toBeTruthy();
36
- });
37
- test("Renders with correct label and helper text", () => {
38
- const { getByText } = render(<DxcNewInputText label="Example label" helperText="Example helper text" />);
39
- expect(getByText("Example label")).toBeTruthy();
40
- expect(getByText("Example helper text")).toBeTruthy();
41
- });
42
- test("Renders with correct label and optional", () => {
43
- const { getByText } = render(<DxcNewInputText label="Example label" helperText="Example helper text" optional />);
44
- expect(getByText("Example label")).toBeTruthy();
45
- expect(getByText("(Optional)")).toBeTruthy();
46
- expect(getByText("Example helper text")).toBeTruthy();
47
- });
48
- test("Renders with correct placeholder", () => {
49
- const { getByRole } = render(<DxcNewInputText label="Example label" placeholder="Placeholder" />);
50
- const input = getByRole("textbox");
51
- expect(input.getAttribute("placeholder")).toBe("Placeholder");
52
- });
53
- test("Renders with error message", () => {
54
- const { getByText } = render(
55
- <DxcNewInputText label="Error label" placeholder="Placeholder" error="Error message." />
56
- );
57
- expect(getByText("Error message.")).toBeTruthy();
58
- });
59
- test("Input Strict - Not optional constraint", () => {
60
- const onChange = jest.fn();
61
- const onBlur = jest.fn();
62
- const { getByRole, getByText, queryByText } = render(
63
- <DxcNewInputText
64
- label="Input label"
65
- placeholder="Placeholder"
66
- onChange={onChange}
67
- onBlur={onBlur}
68
- clearable
69
- />
70
- );
71
- const input = getByRole("textbox");
72
- fireEvent.focus(input);
73
- fireEvent.blur(input);
74
- expect(getByText("This field is required. Please, enter a value.")).toBeTruthy();
75
- userEvent.type(input, "A");
76
- fireEvent.blur(input);
77
- expect(queryByText("This field is required. Please, enter a value.")).toBeFalsy();
78
- });
79
- test("Input Strict - Pattern constraint", () => {
80
- const onChange = jest.fn();
81
- const onBlur = jest.fn();
82
- const { getByRole, getByText, queryByText } = render(
83
- <DxcNewInputText
84
- label="Input label"
85
- placeholder="Placeholder"
86
- onChange={onChange}
87
- onBlur={onBlur}
88
- margin={{ left: "medium", right: "medium" }}
89
- clearable
90
- pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
91
- />
92
- );
93
- const input = getByRole("textbox");
94
- userEvent.type(input, "pattern test");
95
- fireEvent.blur(input);
96
- expect(getByText("Please match the format requested.")).toBeTruthy();
97
- userEvent.type(input, "pattern4&");
98
- fireEvent.blur(input);
99
- expect(queryByText("Please match the format requested.")).toBeFalsy();
100
- });
101
- test("Input Strict - Length constraint", () => {
102
- const onChange = jest.fn();
103
- const onBlur = jest.fn();
104
- const { getByRole, getByText, queryByText } = render(
105
- <DxcNewInputText
106
- label="Input label"
107
- placeholder="Placeholder"
108
- onChange={onChange}
109
- onBlur={onBlur}
110
- margin={{ left: "medium", right: "medium" }}
111
- clearable
112
- length={{ min: 5, max: 10 }}
113
- />
114
- );
115
- const input = getByRole("textbox");
116
- userEvent.type(input, "test");
117
- fireEvent.blur(input);
118
- expect(getByText("Min length 5, max length 10.")).toBeTruthy();
119
- userEvent.type(input, "test ");
120
- fireEvent.blur(input);
121
- expect(queryByText(/Min length /)).toBeFalsy();
122
- });
123
- test("Input Strict - Pattern and length constraints", () => {
124
- const onChange = jest.fn();
125
- const onBlur = jest.fn();
126
- const { getByRole, getByText, queryByText } = render(
127
- <DxcNewInputText
128
- label="Input label"
129
- placeholder="Placeholder"
130
- onChange={onChange}
131
- onBlur={onBlur}
132
- margin={{ left: "medium", right: "medium" }}
133
- clearable
134
- pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
135
- length={{ min: 5, max: 10 }}
136
- />
137
- );
138
- const input = getByRole("textbox");
139
- userEvent.type(input, "test");
140
- fireEvent.blur(input);
141
- expect(getByText("Min length 5, max length 10.")).toBeTruthy();
142
- userEvent.type(input, "test ");
143
- fireEvent.blur(input);
144
- expect(getByText("Please match the format requested.")).toBeTruthy();
145
- userEvent.type(input, "test 4");
146
- fireEvent.blur(input);
147
- expect(queryByText("Please match the format requested.")).toBeFalsy();
148
- });
149
- test("Input Non Strict - Not optional constraint", () => {
150
- const onChange = jest.fn((value) => {
151
- expect(value).toBe("");
152
- });
153
- const onBlur = jest.fn(({ value, error }) => {
154
- expect(value).toBe("");
155
- expect(error).toBe("This field is required. Please, enter a value.");
156
- });
157
- const { getByRole } = render(
158
- <DxcNewInputText
159
- label="Input label"
160
- placeholder="Placeholder"
161
- onChange={onChange}
162
- onBlur={onBlur}
163
- />
164
- );
165
- const input = getByRole("textbox");
166
- fireEvent.focus(input);
167
- fireEvent.blur(input);
168
- });
169
- test("Input Non Strict - Pattern constraint", () => {
170
- const onChange = jest.fn((value) => {
171
- expect(value).toBe("t");
172
- });
173
- const onBlur = jest.fn(({ value, error }) => {
174
- expect(value).toBe("t");
175
- expect(error).toBe("Please match the format requested.");
176
- });
177
- const { getByRole } = render(
178
- <DxcNewInputText
179
- label="Input label"
180
- placeholder="Placeholder"
181
- onChange={onChange}
182
- onBlur={onBlur}
183
- margin={{ left: "medium", right: "medium" }}
184
- clearable
185
- pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
186
- />
187
- );
188
- const input = getByRole("textbox");
189
- userEvent.type(input, "t");
190
- fireEvent.blur(input);
191
- });
192
- test("Input Non Strict - Length constraint", () => {
193
- const onChange = jest.fn((value) => {
194
- expect(value).toBe("t");
195
- });
196
- const onBlur = jest.fn(({ value, error }) => {
197
- expect(value).toBe("t");
198
- expect(error).toBe("Min length 5, max length 10.");
199
- });
200
- const { getByRole } = render(
201
- <DxcNewInputText
202
- label="Input label"
203
- placeholder="Placeholder"
204
- onChange={onChange}
205
- onBlur={onBlur}
206
- margin={{ left: "medium", right: "medium" }}
207
- clearable
208
- length={{ min: 5, max: 10 }}
209
- />
210
- );
211
- const input = getByRole("textbox");
212
- userEvent.type(input, "t");
213
- fireEvent.blur(input);
214
- });
215
- test("Input Non Strict - Pattern and length constraints", () => {
216
- const onChange = jest.fn((value) => {
217
- expect(value).toBe("t");
218
- });
219
- const onBlur = jest.fn(({ value, error }) => {
220
- expect(value).toBe("t");
221
- expect(error).toBe("Min length 5, max length 10.");
222
- });
223
- const { getByRole } = render(
224
- <DxcNewInputText
225
- label="Input label"
226
- placeholder="Placeholder"
227
- onChange={onChange}
228
- onBlur={onBlur}
229
- margin={{ left: "medium", right: "medium" }}
230
- clearable
231
- length={{ min: 5, max: 10 }}
232
- />
233
- );
234
- const input = getByRole("textbox");
235
- userEvent.type(input, "t");
236
- fireEvent.blur(input);
237
- });
238
- test("onChange function is called correctly", () => {
239
- const onChange = jest.fn();
240
- const { getByRole } = render(<DxcNewInputText label="Input label" onChange={onChange} />);
241
- const input = getByRole("textbox");
242
- userEvent.type(input, "onchange event test");
243
- expect(input.value).toBe("onchange event test");
244
- expect(onChange).toHaveBeenCalled();
245
- expect(onChange).toHaveBeenCalledWith("onchange event test");
246
- });
247
- test("onBlur function is called correctly", () => {
248
- const onBlur = jest.fn();
249
- const onChange = jest.fn();
250
- const { getByRole } = render(<DxcNewInputText label="Input label" onChange={onChange} onBlur={onBlur} />);
251
- const input = getByRole("textbox");
252
- userEvent.type(input, "Test string");
253
- fireEvent.blur(input);
254
- expect(onBlur).toHaveBeenCalled();
255
- expect(onBlur).toHaveBeenCalledWith({ value: "Test string", error: null });
256
- });
257
- test("Uncontrolled input", () => {
258
- const onChange = jest.fn();
259
- const { getByRole } = render(<DxcNewInputText label="Input label" onChange={onChange} />);
260
- const input = getByRole("textbox");
261
- userEvent.type(input, "Uncontrolled test");
262
- expect(onChange).toHaveBeenCalled();
263
- expect(onChange).toHaveBeenCalledWith("Uncontrolled test");
264
- expect(input.value).toBe("Uncontrolled test");
265
- });
266
- test("Controlled input", () => {
267
- const onChange = jest.fn();
268
- const onBlur = jest.fn();
269
- const { getByRole } = render(
270
- <DxcNewInputText label="Input label" value="Test value" onChange={onChange} onBlur={onBlur} />
271
- );
272
- const input = getByRole("textbox");
273
- userEvent.type(input, "Example text");
274
- expect(onChange).toHaveBeenCalled();
275
- expect(input.value).toBe("Test value");
276
- fireEvent.blur(input);
277
- expect(onBlur).toHaveBeenCalled();
278
- expect(onBlur).toHaveBeenCalledWith({ value: "Test value", error: null });
279
- });
280
- test("Clear action onClick cleans the input", () => {
281
- const { getByRole } = render(<DxcNewInputText label="Input label" clearable />);
282
- const input = getByRole("textbox");
283
- userEvent.type(input, "Test");
284
- const closeAction = getByRole("button");
285
- userEvent.click(closeAction);
286
- expect(input.value).toBe("");
287
- });
288
- test("Disabled input (action must be shown but not clickable)", () => {
289
- const onClick = jest.fn();
290
- const action = {
291
- onClick: onClick,
292
- icon: (
293
- <svg
294
- data-testid="image"
295
- xmlns="http://www.w3.org/2000/svg"
296
- height="24px"
297
- viewBox="0 0 24 24"
298
- width="24px"
299
- fill="currentColor"
300
- >
301
- <path d="M0 0h24v24H0V0z" fill="none" />
302
- <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" />
303
- </svg>
304
- ),
305
- };
306
- const { getByRole } = render(<DxcNewInputText label="Disabled input label" action={action} disabled />);
307
- const input = getByRole("textbox");
308
- expect(input.disabled).toBeTruthy();
309
- userEvent.click(getByRole("button"));
310
- expect(onClick).not.toHaveBeenCalled();
311
- });
312
- test("Disabled input (clear default action should not be displayed, even with text written on the input)", () => {
313
- const { getByRole, queryByRole } = render(
314
- <DxcNewInputText label="Disabled input label" value="Sample text" disabled clearable />
315
- );
316
- const input = getByRole("textbox");
317
- expect(input.disabled).toBeTruthy();
318
- expect(queryByRole("button")).toBeFalsy();
319
- });
320
- test("Disabled input (suffix and preffix must be displayed)", () => {
321
- const { getByRole, getByText } = render(
322
- <DxcNewInputText label="Disabled input label" value="Sample text" prefix="+34" suffix="USD" disabled />
323
- );
324
- const input = getByRole("textbox");
325
- expect(input.disabled).toBeTruthy();
326
- expect(getByText("+34")).toBeTruthy();
327
- expect(getByText("USD")).toBeTruthy();
328
- });
329
- test("Action prop: image displayed and onClick event", () => {
330
- const onClick = jest.fn();
331
- const action = {
332
- onClick: onClick,
333
- icon: (
334
- <svg
335
- data-testid="image"
336
- xmlns="http://www.w3.org/2000/svg"
337
- height="24px"
338
- viewBox="0 0 24 24"
339
- width="24px"
340
- fill="currentColor"
341
- >
342
- <path d="M0 0h24v24H0V0z" fill="none" />
343
- <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" />
344
- </svg>
345
- ),
346
- };
347
- const { getByRole, getByTestId } = render(<DxcNewInputText label="Input label" action={action} />);
348
- expect(getByTestId("image")).toBeTruthy();
349
- userEvent.click(getByRole("button"));
350
- expect(onClick).toHaveBeenCalled();
351
- });
352
- test("Renders with correct prefix and suffix (text)", () => {
353
- const { getByText } = render(<DxcNewInputText label="Input label" prefix="+34" suffix="USD" />);
354
- expect(getByText("+34")).toBeTruthy();
355
- expect(getByText("USD")).toBeTruthy();
356
- });
357
- test("Renders with correct prefix (icon)", () => {
358
- const { getByTestId } = render(
359
- <DxcNewInputText
360
- label="Input label"
361
- prefix={
362
- <svg
363
- data-testid="image"
364
- version="1.1"
365
- x="0px"
366
- y="0px"
367
- width="24px"
368
- height="24px"
369
- viewBox="0 0 24 24"
370
- enable-background="new 0 0 24 24"
371
- >
372
- <g id="Bounding_Box">
373
- <rect fill="none" width="24" height="24" />
374
- </g>
375
- <g id="Master">
376
- <path d="M19,9.3V4h-3v2.6L12,3L2,12h3v8h5v-6h4v6h5v-8h3L19,9.3z M10,10c0-1.1,0.9-2,2-2s2,0.9,2,2H10z" />
377
- </g>
378
- </svg>
379
- }
380
- />
381
- );
382
- expect(getByTestId("image")).toBeTruthy();
383
- });
384
- test("Renders with correct suffix (icon)", () => {
385
- const { getByTestId } = render(
386
- <DxcNewInputText
387
- label="Input label"
388
- suffix={
389
- <svg
390
- data-testid="image"
391
- version="1.1"
392
- x="0px"
393
- y="0px"
394
- width="24px"
395
- height="24px"
396
- viewBox="0 0 24 24"
397
- enable-background="new 0 0 24 24"
398
- >
399
- <g id="Bounding_Box">
400
- <rect fill="none" width="24" height="24" />
401
- </g>
402
- <g id="Master">
403
- <path d="M19,9.3V4h-3v2.6L12,3L2,12h3v8h5v-6h4v6h5v-8h3L19,9.3z M10,10c0-1.1,0.9-2,2-2s2,0.9,2,2H10z" />
404
- </g>
405
- </svg>
406
- }
407
- />
408
- );
409
- expect(getByTestId("image")).toBeTruthy();
410
- });
411
- test("Input has correct accesibility attributes", () => {
412
- const onClick = jest.fn();
413
- const action = {
414
- onClick: onClick,
415
- icon: (
416
- <svg
417
- data-testid="image"
418
- xmlns="http://www.w3.org/2000/svg"
419
- height="24px"
420
- viewBox="0 0 24 24"
421
- width="24px"
422
- fill="currentColor"
423
- >
424
- <path d="M0 0h24v24H0V0z" fill="none" />
425
- <path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" />
426
- </svg>
427
- ),
428
- };
429
- const { getByRole, getAllByRole } = render(
430
- <DxcNewInputText label="Example label" clearable action={action} />
431
- );
432
- const input = getByRole("textbox");
433
- expect(input.getAttribute("aria-autocomplete")).toBeNull();
434
- expect(input.getAttribute("aria-controls")).toBeNull();
435
- expect(input.getAttribute("aria-expanded")).toBeNull();
436
- expect(input.getAttribute("aria-activedescendant")).toBeNull();
437
- expect(input.getAttribute("aria-invalid")).toBe("false");
438
- expect(input.getAttribute("aria-describedBy")).toBeNull();
439
- expect(input.getAttribute("aria-required")).toBe("true");
440
- userEvent.type(input, "Text");
441
- const clear = getAllByRole("button")[0];
442
- expect(clear.getAttribute("aria-label")).toBe("Clear");
443
- userEvent.click(clear);
444
- fireEvent.blur(input);
445
- expect(input.getAttribute("aria-invalid")).toBe("true");
446
- expect(input.getAttribute("aria-describedBy")).not.toBeNull();
447
- });
448
- test("Autosuggest has correct accesibility attributes", () => {
449
- const { getByRole, getAllByRole } = render(
450
- <DxcNewInputText label="Autocomplete Countries" optional suggestions={countries} />
451
- );
452
- const input = getByRole("combobox");
453
- const inputId = input.id;
454
- expect(input.getAttribute("aria-autocomplete")).toBe("list");
455
- expect(input.getAttribute("aria-controls")).toBe(inputId);
456
- expect(input.getAttribute("aria-expanded")).toBe("false");
457
- expect(input.getAttribute("aria-required")).toBe("false");
458
- fireEvent.focus(input);
459
- const list = getByRole("listbox");
460
- expect(input.getAttribute("aria-expanded")).toBe("true");
461
- expect(list.getAttribute("aria-label")).toBe("Autocomplete Countries");
462
- const options = getAllByRole("option");
463
- expect(options[0].getAttribute("aria-selected")).toBe("false");
464
- });
465
- });
466
-
467
- describe("NewInputText component synchronous autosuggest tests", () => {
468
- test("Autosuggest is displayed when the input gains focus", async () => {
469
- const onChange = jest.fn();
470
- const { getByRole, getByText } = render(
471
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
472
- );
473
- const input = getByRole("combobox");
474
- fireEvent.focus(input);
475
- const list = getByRole("listbox");
476
- expect(list).toBeTruthy();
477
- expect(getByText("Afghanistan")).toBeTruthy();
478
- expect(getByText("Albania")).toBeTruthy();
479
- expect(getByText("Algeria")).toBeTruthy();
480
- expect(getByText("Andorra")).toBeTruthy();
481
- });
482
- test("Autosuggest is displayed when the user clicks the input", async () => {
483
- const onChange = jest.fn();
484
- const { getByRole, getByText } = render(
485
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
486
- );
487
- const input = getByRole("combobox");
488
- userEvent.click(input);
489
- const list = getByRole("listbox");
490
- expect(list).toBeTruthy();
491
- expect(getByText("Afghanistan")).toBeTruthy();
492
- expect(getByText("Albania")).toBeTruthy();
493
- expect(getByText("Algeria")).toBeTruthy();
494
- expect(getByText("Andorra")).toBeTruthy();
495
- });
496
- test("Autosuggest is displayed while the user is writing (if closed previously, if open stays open)", async () => {
497
- const onChange = jest.fn();
498
- const { getByRole, queryByRole, getByText, getAllByText } = render(
499
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
500
- );
501
- const input = getByRole("combobox");
502
- fireEvent.focus(input);
503
- const list = getByRole("listbox");
504
- expect(list).toBeTruthy();
505
- fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
506
- expect(queryByRole("listbox")).toBeFalsy();
507
- userEvent.type(input, "B");
508
- expect(list).toBeTruthy();
509
- expect(getAllByText("B").length).toBe(4);
510
- expect(getByText("ahamas")).toBeTruthy();
511
- expect(getByText("ahrain")).toBeTruthy();
512
- expect(getByText("angladesh")).toBeTruthy();
513
- expect(getByText("arbados")).toBeTruthy();
514
- });
515
- test("Autosuggest is not displayed when prop suggestions is an empty array", async () => {
516
- const onChange = jest.fn();
517
- const { queryByRole } = render(
518
- <DxcNewInputText label="Autocomplete Countries" suggestions={[]} onChange={onChange} />
519
- );
520
- const input = queryByRole("textbox");
521
- fireEvent.focus(input);
522
- expect(queryByRole("listbox")).toBeFalsy();
523
- });
524
- test("Autosuggest shows 'No results found' message when there are no matches with the user's input", async () => {
525
- const onChange = jest.fn();
526
- const { getByRole, getByText } = render(
527
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
528
- );
529
- const input = getByRole("combobox");
530
- fireEvent.focus(input);
531
- const list = getByRole("listbox");
532
- expect(list).toBeTruthy();
533
- expect(getByText("Afghanistan")).toBeTruthy();
534
- userEvent.type(input, "x");
535
- expect(list).toBeTruthy();
536
- expect(getByText("No results found")).toBeTruthy();
537
- });
538
- test("Autosuggest uncontrolled suggestion selected", async () => {
539
- const onChange = jest.fn();
540
- const { getByRole, getByText, queryByRole } = render(
541
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
542
- );
543
- const input = getByRole("combobox");
544
- fireEvent.focus(input);
545
- userEvent.type(input, "Alba");
546
- expect(onChange).toHaveBeenCalled();
547
- expect(getByText("Alba")).toBeTruthy();
548
- expect(getByText("nia")).toBeTruthy();
549
- fireEvent.mouseDown(getByRole("option"));
550
- fireEvent.mouseUp(getByRole("option"));
551
- expect(input.value).toBe("Albania");
552
- expect(queryByRole("listbox")).toBeFalsy();
553
- });
554
- test("Autosuggest controlled suggestions selected", async () => {
555
- const onChange = jest.fn();
556
- const { getByRole, getByText, queryByRole } = render(
557
- <DxcNewInputText label="Autocomplete Countries" value="Andor" suggestions={countries} onChange={onChange} />
558
- );
559
- const input = getByRole("combobox");
560
- fireEvent.focus(input);
561
- expect(input.value).toBe("Andor");
562
- expect(getByText("Andor")).toBeTruthy();
563
- expect(getByText("ra")).toBeTruthy();
564
- fireEvent.mouseDown(getByRole("option"));
565
- fireEvent.mouseUp(getByRole("option"));
566
- expect(onChange).toHaveBeenCalledWith("Andorra");
567
- expect(queryByRole("listbox")).toBeFalsy();
568
- });
569
- test("Autosuggest Strict - Pattern constraint", async () => {
570
- const onChange = jest.fn();
571
- const { getByRole, getByText, queryByText } = render(
572
- <DxcNewInputText
573
- label="Autocomplete Countries"
574
- value="Andor"
575
- suggestions={countries}
576
- onChange={onChange}
577
- pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
578
- />
579
- );
580
- const input = getByRole("combobox");
581
- fireEvent.focus(input);
582
- userEvent.type(input, "Andor");
583
- expect(getByText("Andor")).toBeTruthy();
584
- expect(getByText("ra")).toBeTruthy();
585
- fireEvent.mouseDown(getByRole("option"));
586
- fireEvent.mouseUp(getByRole("option"));
587
- expect(onChange).toHaveBeenCalledWith("Andorra");
588
- fireEvent.blur(input);
589
- expect(queryByText("Please match the format requested.")).toBeTruthy();
590
- });
591
- test("Autosuggest Strict - Length constraint", async () => {
592
- const onChange = jest.fn();
593
- const { getByRole, queryByText } = render(
594
- <DxcNewInputText
595
- label="Autocomplete Countries"
596
- value="Cha"
597
- suggestions={countries}
598
- onChange={onChange}
599
- length={{ min: 5, max: 10 }}
600
- />
601
- );
602
- const input = getByRole("combobox");
603
- fireEvent.focus(input);
604
- userEvent.type(input, "d");
605
- const option = getByRole("option");
606
- fireEvent.mouseDown(option);
607
- fireEvent.mouseUp(option);
608
- expect(onChange).toHaveBeenCalledWith("Chad");
609
- fireEvent.blur(input);
610
- expect(queryByText("Min length 5, max length 10.")).toBeTruthy();
611
- });
612
- test("Autosuggest keys: arrow down key opens autosuggest, active first option is selected with Enter and closes the autosuggest", async () => {
613
- Element.prototype.scrollTo = () => {};
614
- const onChange = jest.fn();
615
- const { getByRole, queryByRole } = render(
616
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
617
- );
618
- const input = getByRole("combobox");
619
- fireEvent.keyDown(input, { key: "ArrowDown", code: "ArrowDown", keyCode: 40, charCode: 40 });
620
- const list = getByRole("listbox");
621
- expect(list).toBeTruthy();
622
- fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
623
- expect(input.value).toBe("Afghanistan");
624
- expect(queryByRole("list")).toBeFalsy();
625
- });
626
- test("Autosuggest keys: arrow up key opens autosuggest, active last option is selected with Enter and closes the autosuggest", async () => {
627
- Element.prototype.scrollTo = () => {};
628
- const onChange = jest.fn();
629
- const { getByRole, queryByRole } = render(
630
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
631
- );
632
- const input = getByRole("combobox");
633
- fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
634
- const list = getByRole("listbox");
635
- expect(list).toBeTruthy();
636
- fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
637
- expect(input.value).toBe("Djibouti");
638
- expect(queryByRole("list")).toBeFalsy();
639
- });
640
- test("Autosuggest keys: Esc key closes the autosuggest and cleans the input", async () => {
641
- Element.prototype.scrollTo = () => {};
642
- const onChange = jest.fn();
643
- const { getByRole, queryByRole } = render(
644
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
645
- );
646
- const input = getByRole("combobox");
647
- fireEvent.focus(input);
648
- userEvent.type(input, "Bangla");
649
- const list = getByRole("listbox");
650
- expect(list).toBeTruthy();
651
- fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
652
- expect(input.value).toBe("");
653
- expect(queryByRole("listbox")).toBeFalsy();
654
- });
655
- test("Autosuggest keys: Enter, if no active suggestion closes the autosuggest", async () => {
656
- Element.prototype.scrollTo = () => {};
657
- const onChange = jest.fn();
658
- const { getByRole, queryByRole } = render(
659
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
660
- );
661
- const input = getByRole("combobox");
662
- fireEvent.focus(input);
663
- const list = getByRole("listbox");
664
- expect(list).toBeTruthy();
665
- fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 27, charCode: 27 });
666
- expect(input.value).toBe("");
667
- expect(queryByRole("list")).toBeFalsy();
668
- });
669
- test("Autosuggest complex key secuence: write, arrow up two times, arrow down and select with Enter. Then, clean with Esc.", async () => {
670
- Element.prototype.scrollTo = () => {};
671
- const onChange = jest.fn();
672
- const { getByRole, queryByRole } = render(
673
- <DxcNewInputText label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
674
- );
675
- const input = getByRole("combobox");
676
- fireEvent.focus(input);
677
- userEvent.type(input, "Ba");
678
- fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
679
- fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
680
- fireEvent.keyDown(input, { key: "ArrowDown", code: "ArrowDown", keyCode: 40, charCode: 40 });
681
- fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
682
- expect(input.value).toBe("Barbados");
683
- expect(queryByRole("listbox")).toBeFalsy();
684
- fireEvent.keyDown(input, { key: "Esc", code: "Esp", keyCode: 27, charCode: 27 });
685
- expect(input.value).toBe("");
686
- expect(queryByRole("listbox")).toBeFalsy();
687
- });
688
- });
689
-
690
- describe("NewInputText component asynchronous autosuggest tests", () => {
691
- test("Autosuggest 'Searching...' message is shown", async () => {
692
- const callbackFunc = jest.fn((newValue) => {
693
- const result = new Promise((resolve) =>
694
- setTimeout(() => {
695
- resolve(
696
- newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
697
- );
698
- }, 1000)
699
- );
700
- return result;
701
- });
702
- const onChange = jest.fn();
703
- const { getByRole, getByText } = render(
704
- <DxcNewInputText label="Autosuggest Countries" suggestions={callbackFunc} onChange={onChange} />
705
- );
706
- const input = getByRole("combobox");
707
- fireEvent.focus(input);
708
- expect(getByRole("listbox")).toBeTruthy();
709
- await waitForElementToBeRemoved(() => getByText("Searching..."));
710
- expect(getByText("Afghanistan")).toBeTruthy();
711
- expect(getByText("Albania")).toBeTruthy();
712
- expect(getByText("Algeria")).toBeTruthy();
713
- expect(getByText("Andorra")).toBeTruthy();
714
- userEvent.type(input, "Ab");
715
- await waitForElementToBeRemoved(() => getByText("Searching..."));
716
- expect(getByText("Cabo Verde")).toBeTruthy();
717
- fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
718
- fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
719
- expect(input.value).toBe("Cabo Verde");
720
- });
721
- test("Autosuggest Esc key works while 'Searching...' message is shown", async () => {
722
- const callbackFunc = jest.fn((newValue) => {
723
- const result = new Promise((resolve) =>
724
- setTimeout(() => {
725
- resolve(
726
- newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
727
- );
728
- }, 1000)
729
- );
730
- return result;
731
- });
732
- const onChange = jest.fn();
733
- const { getByRole, queryByText, queryByRole } = render(
734
- <DxcNewInputText label="Autosuggest Countries" suggestions={callbackFunc} onChange={onChange} />
735
- );
736
- const input = getByRole("combobox");
737
- fireEvent.focus(input);
738
- expect(getByRole("listbox")).toBeTruthy();
739
- userEvent.type(input, "Ab");
740
- fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
741
- expect(queryByRole("listbox")).toBeFalsy();
742
- expect(queryByText("Searching...")).toBeFalsy();
743
- expect(input.value).toBe("");
744
- });
745
- test("Autosuggest Esc + arrow down working while 'Searching...' message is shown", async () => {
746
- const callbackFunc = jest.fn((newValue) => {
747
- const result = new Promise((resolve) =>
748
- setTimeout(() => {
749
- resolve(
750
- newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
751
- );
752
- }, 1000)
753
- );
754
- return result;
755
- });
756
- const onChange = jest.fn();
757
- const { getByRole, getByText, queryByText, queryByRole } = render(
758
- <DxcNewInputText label="Autosuggest Countries" suggestions={callbackFunc} onChange={onChange} />
759
- );
760
- const input = getByRole("combobox");
761
- fireEvent.focus(input);
762
- const list = getByRole("listbox");
763
- expect(list).toBeTruthy();
764
- userEvent.type(input, "Ab");
765
- fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
766
- expect(queryByRole("listbox")).toBeFalsy();
767
- expect(queryByText("Searching...")).toBeFalsy();
768
- expect(input.value).toBe("");
769
- fireEvent.keyDown(input, { key: "ArrowDown", code: "ArrowDown", keyCode: 40, charCode: 40 });
770
- expect(list).toBeTruthy();
771
- await waitForElementToBeRemoved(() => getByText("Searching..."));
772
- expect(getByText("Afghanistan")).toBeTruthy();
773
- expect(getByText("Albania")).toBeTruthy();
774
- expect(getByText("Algeria")).toBeTruthy();
775
- expect(getByText("Andorra")).toBeTruthy();
776
- });
777
- test("Asynchronous uncontrolled autosuggest test", async () => {
778
- const callbackFunc = jest.fn((newValue) => {
779
- const result = new Promise((resolve) =>
780
- setTimeout(() => {
781
- resolve(
782
- newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
783
- );
784
- }, 1000)
785
- );
786
- return result;
787
- });
788
- const onChange = jest.fn();
789
- const { getByRole, getByText } = render(
790
- <DxcNewInputText label="Autosuggest Countries" onChange={onChange} suggestions={callbackFunc} />
791
- );
792
- const input = getByRole("combobox");
793
- fireEvent.focus(input);
794
- userEvent.type(input, "Den");
795
- await waitForElementToBeRemoved(() => getByText("Searching..."));
796
- expect(getByText("Denmark")).toBeTruthy();
797
- fireEvent.mouseDown(getByRole("option"));
798
- fireEvent.mouseUp(getByRole("option"));
799
- expect(onChange).toHaveBeenCalledWith("Denmark");
800
- expect(input.value).toBe("Denmark");
801
- });
802
- test("Asynchronous controlled autosuggest test", async () => {
803
- const callbackFunc = jest.fn((newValue) => {
804
- const result = new Promise((resolve) =>
805
- setTimeout(() => {
806
- resolve(
807
- newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
808
- );
809
- }, 1000)
810
- );
811
- return result;
812
- });
813
- const onChange = jest.fn();
814
- const { getByRole, getByText, queryByRole } = render(
815
- <DxcNewInputText label="Autosuggest Countries" value="Denm" onChange={onChange} suggestions={callbackFunc} />
816
- );
817
- const input = getByRole("combobox");
818
- fireEvent.focus(input);
819
- await waitForElementToBeRemoved(() => getByText("Searching..."));
820
- expect(input.value).toBe("Denm");
821
- expect(getByText("Denmark")).toBeTruthy();
822
- fireEvent.mouseDown(getByRole("option"));
823
- fireEvent.mouseUp(getByRole("option"));
824
- expect(onChange).toHaveBeenCalledWith("Denmark");
825
- expect(queryByRole("listbox")).toBeFalsy();
826
- });
827
- test("Asynchronous autosuggest shows 'No results found' after finishing no matches search", async () => {
828
- const callbackFunc = jest.fn((newValue) => {
829
- const result = new Promise((resolve) =>
830
- setTimeout(() => {
831
- resolve(
832
- newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
833
- );
834
- }, 1000)
835
- );
836
- return result;
837
- });
838
- const onChange = jest.fn();
839
- const { getByRole, getByText } = render(
840
- <DxcNewInputText label="Autosuggest Countries" onChange={onChange} suggestions={callbackFunc} />
841
- );
842
- const input = getByRole("combobox");
843
- fireEvent.focus(input);
844
- userEvent.type(input, "Example text");
845
- await waitForElementToBeRemoved(() => getByText("Searching..."));
846
- expect(getByText("No results found")).toBeTruthy();
847
- });
848
- test("Asynchronous autosuggest request failed, shows 'Error fetching data' message", async () => {
849
- const errorCallbackFunc = jest.fn(() => {
850
- const result = new Promise((resolve, reject) =>
851
- setTimeout(() => {
852
- reject("err");
853
- }, 1000)
854
- );
855
- return result;
856
- });
857
- const onChange = jest.fn();
858
- const { getByRole, getByText } = render(
859
- <DxcNewInputText label="Autosuggest Countries" onChange={onChange} suggestions={errorCallbackFunc} />
860
- );
861
- const input = getByRole("combobox");
862
- fireEvent.focus(input);
863
- await waitForElementToBeRemoved(() => getByText("Searching..."));
864
- expect(getByText("Error fetching data")).toBeTruthy();
865
- });
866
- });