@dxc-technology/halstack-react 0.0.0-a105522 → 0.0.0-a3f5d07

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 (232) hide show
  1. package/{dist/BackgroundColorContext.js → BackgroundColorContext.js} +0 -0
  2. package/{dist/ThemeContext.js → ThemeContext.js} +74 -64
  3. package/{dist/select/Select.js → V3Select/V3Select.js} +5 -5
  4. package/V3Select/index.d.ts +27 -0
  5. package/{dist/textarea/Textarea.js → V3Textarea/V3Textarea.js} +4 -4
  6. package/V3Textarea/index.d.ts +27 -0
  7. package/{dist/accordion → accordion}/Accordion.js +0 -0
  8. package/accordion/index.d.ts +28 -0
  9. package/{dist/accordion-group → accordion-group}/AccordionGroup.js +0 -0
  10. package/accordion-group/index.d.ts +16 -0
  11. package/{dist/alert → alert}/Alert.js +5 -5
  12. package/alert/index.d.ts +51 -0
  13. package/{dist/badge → badge}/Badge.js +0 -0
  14. package/{dist/box → box}/Box.js +0 -0
  15. package/box/index.d.ts +25 -0
  16. package/{dist/button → button}/Button.js +3 -3
  17. package/button/Button.stories.js +27 -0
  18. package/{dist/alert → button}/index.d.ts +9 -11
  19. package/{dist/card → card}/Card.js +0 -0
  20. package/card/index.d.ts +22 -0
  21. package/{dist/checkbox → checkbox}/Checkbox.js +4 -5
  22. package/checkbox/index.d.ts +24 -0
  23. package/{dist/chip → chip}/Chip.js +0 -0
  24. package/chip/index.d.ts +22 -0
  25. package/{dist/common → common}/OpenSans.css +0 -0
  26. package/{dist/common → common}/RequiredComponent.js +0 -0
  27. package/{dist/common → common}/fonts/OpenSans-Bold.ttf +0 -0
  28. package/{dist/common → common}/fonts/OpenSans-BoldItalic.ttf +0 -0
  29. package/{dist/common → common}/fonts/OpenSans-ExtraBold.ttf +0 -0
  30. package/{dist/common → common}/fonts/OpenSans-ExtraBoldItalic.ttf +0 -0
  31. package/{dist/common → common}/fonts/OpenSans-Italic.ttf +0 -0
  32. package/{dist/common → common}/fonts/OpenSans-Light.ttf +0 -0
  33. package/{dist/common → common}/fonts/OpenSans-LightItalic.ttf +0 -0
  34. package/{dist/common → common}/fonts/OpenSans-Regular.ttf +0 -0
  35. package/{dist/common → common}/fonts/OpenSans-SemiBold.ttf +0 -0
  36. package/{dist/common → common}/fonts/OpenSans-SemiBoldItalic.ttf +0 -0
  37. package/{dist/common → common}/utils.js +0 -0
  38. package/{dist/common → common}/variables.js +286 -89
  39. package/{dist/date → date}/Date.js +4 -6
  40. package/date/index.d.ts +27 -0
  41. package/{dist/new-date/NewDate.js → date-input/DateInput.js} +69 -72
  42. package/date-input/index.d.ts +95 -0
  43. package/{dist/dialog → dialog}/Dialog.js +0 -0
  44. package/dialog/index.d.ts +18 -0
  45. package/{dist/dropdown → dropdown}/Dropdown.js +0 -0
  46. package/dropdown/index.d.ts +26 -0
  47. package/file-input/FileInput.js +644 -0
  48. package/file-input/FileItem.js +287 -0
  49. package/file-input/index.d.ts +81 -0
  50. package/{dist/footer → footer}/Footer.js +44 -18
  51. package/footer/Icons.js +77 -0
  52. package/footer/index.d.ts +25 -0
  53. package/{dist/header → header}/Header.js +122 -55
  54. package/header/Icons.js +59 -0
  55. package/header/index.d.ts +25 -0
  56. package/{dist/heading → heading}/Heading.js +12 -0
  57. package/heading/index.d.ts +17 -0
  58. package/input-text/Icons.js +22 -0
  59. package/{dist/input-text → input-text}/InputText.js +7 -9
  60. package/input-text/index.d.ts +36 -0
  61. package/{dist/layout → layout}/ApplicationLayout.js +4 -8
  62. package/layout/Icons.js +55 -0
  63. package/{dist/link → link}/Link.js +0 -0
  64. package/link/index.d.ts +23 -0
  65. package/main.d.ts +40 -0
  66. package/{dist/main.js → main.js} +37 -29
  67. package/{dist/number/Number.js → number-input/NumberInput.js} +9 -11
  68. package/{dist/number/NumberContext.js → number-input/NumberInputContext.js} +2 -2
  69. package/number-input/index.d.ts +113 -0
  70. package/package.json +21 -17
  71. package/paginator/Icons.js +66 -0
  72. package/{dist/paginator → paginator}/Paginator.js +53 -37
  73. package/paginator/index.d.ts +20 -0
  74. package/{dist/password/Password.js → password-input/PasswordInput.js} +17 -14
  75. package/password-input/index.d.ts +94 -0
  76. package/{dist/progress-bar → progress-bar}/ProgressBar.js +0 -0
  77. package/progress-bar/index.d.ts +18 -0
  78. package/{dist/radio → radio}/Radio.js +0 -0
  79. package/radio/index.d.ts +23 -0
  80. package/{dist/resultsetTable → resultsetTable}/ResultsetTable.js +1 -1
  81. package/resultsetTable/index.d.ts +19 -0
  82. package/select/Select.js +1138 -0
  83. package/select/index.d.ts +131 -0
  84. package/{dist/sidenav → sidenav}/Sidenav.js +0 -0
  85. package/sidenav/index.d.ts +13 -0
  86. package/{dist/slider → slider}/Slider.js +104 -19
  87. package/slider/index.d.ts +29 -0
  88. package/{dist/spinner → spinner}/Spinner.js +0 -0
  89. package/spinner/index.d.ts +17 -0
  90. package/{dist/switch → switch}/Switch.js +0 -0
  91. package/switch/index.d.ts +24 -0
  92. package/{dist/table → table}/Table.js +0 -0
  93. package/table/index.d.ts +13 -0
  94. package/{dist/tabs → tabs}/Tabs.js +0 -0
  95. package/tabs/index.d.ts +19 -0
  96. package/{dist/tag → tag}/Tag.js +0 -0
  97. package/tag/index.d.ts +24 -0
  98. package/{dist/new-input-text/NewInputText.js → text-input/TextInput.js} +308 -293
  99. package/text-input/index.d.ts +135 -0
  100. package/{dist/new-textarea/NewTextarea.js → textarea/Textarea.js} +58 -54
  101. package/textarea/index.d.ts +117 -0
  102. package/{dist/toggle → toggle}/Toggle.js +0 -0
  103. package/toggle/index.d.ts +21 -0
  104. package/{dist/toggle-group → toggle-group}/ToggleGroup.js +132 -28
  105. package/toggle-group/index.d.ts +21 -0
  106. package/{dist/upload → upload}/Upload.js +0 -0
  107. package/{dist/upload → upload}/buttons-upload/ButtonsUpload.js +7 -11
  108. package/upload/buttons-upload/Icons.js +40 -0
  109. package/{dist/upload → upload}/dragAndDropArea/DragAndDropArea.js +14 -14
  110. package/upload/dragAndDropArea/Icons.js +39 -0
  111. package/{dist/upload → upload}/file-upload/FileToUpload.js +26 -21
  112. package/upload/file-upload/Icons.js +66 -0
  113. package/{dist/upload → upload}/files-upload/FilesToUpload.js +0 -0
  114. package/upload/index.d.ts +15 -0
  115. package/upload/transaction/Icons.js +160 -0
  116. package/{dist/upload → upload}/transaction/Transaction.js +11 -38
  117. package/{dist/upload → upload}/transactions/Transactions.js +0 -0
  118. package/{dist/useTheme.js → useTheme.js} +0 -0
  119. package/wizard/Icons.js +65 -0
  120. package/{dist/wizard → wizard}/Wizard.js +3 -9
  121. package/wizard/index.d.ts +18 -0
  122. package/README.md +0 -66
  123. package/babel.config.js +0 -8
  124. package/dist/checkbox/Checkbox.stories.js +0 -144
  125. package/dist/checkbox/readme.md +0 -116
  126. package/dist/date/Date.stories.js +0 -205
  127. package/dist/date/readme.md +0 -73
  128. package/dist/footer/Footer.stories.js +0 -94
  129. package/dist/footer/dxc_logo.svg +0 -15
  130. package/dist/footer/readme.md +0 -41
  131. package/dist/header/Header.stories.js +0 -176
  132. package/dist/header/close_icon.svg +0 -1
  133. package/dist/header/dxc_logo_black.svg +0 -8
  134. package/dist/header/hamb_menu_black.svg +0 -1
  135. package/dist/header/hamb_menu_white.svg +0 -1
  136. package/dist/header/readme.md +0 -33
  137. package/dist/input-text/InputText.stories.js +0 -209
  138. package/dist/input-text/error.svg +0 -1
  139. package/dist/input-text/readme.md +0 -91
  140. package/dist/layout/facebook.svg +0 -45
  141. package/dist/layout/linkedin.svg +0 -50
  142. package/dist/layout/twitter.svg +0 -53
  143. package/dist/link/readme.md +0 -51
  144. package/dist/main.d.ts +0 -2
  145. package/dist/paginator/images/next.svg +0 -3
  146. package/dist/paginator/images/nextPage.svg +0 -3
  147. package/dist/paginator/images/previous.svg +0 -3
  148. package/dist/paginator/images/previousPage.svg +0 -3
  149. package/dist/paginator/readme.md +0 -50
  150. package/dist/password/styles.css +0 -3
  151. package/dist/progress-bar/ProgressBar.stories.js +0 -280
  152. package/dist/progress-bar/readme.md +0 -63
  153. package/dist/radio/Radio.stories.js +0 -166
  154. package/dist/radio/readme.md +0 -70
  155. package/dist/resultsetTable/arrow_downward-24px_wht.svg +0 -1
  156. package/dist/resultsetTable/arrow_upward-24px_wht.svg +0 -1
  157. package/dist/resultsetTable/unfold_more-24px_wht.svg +0 -1
  158. package/dist/slider/Slider.stories.js +0 -241
  159. package/dist/slider/readme.md +0 -64
  160. package/dist/spinner/Spinner.stories.js +0 -183
  161. package/dist/spinner/readme.md +0 -65
  162. package/dist/switch/Switch.stories.js +0 -134
  163. package/dist/switch/readme.md +0 -133
  164. package/dist/tabs/Tabs.stories.js +0 -130
  165. package/dist/tabs/readme.md +0 -78
  166. package/dist/tabs-for-sections/TabsForSections.js +0 -92
  167. package/dist/tabs-for-sections/readme.md +0 -78
  168. package/dist/toggle/Toggle.stories.js +0 -297
  169. package/dist/toggle/readme.md +0 -80
  170. package/dist/upload/Upload.stories.js +0 -72
  171. package/dist/upload/buttons-upload/drag-drop-icon.svg +0 -4
  172. package/dist/upload/buttons-upload/upload-button.svg +0 -1
  173. package/dist/upload/dragAndDropArea/upload_drop.svg +0 -4
  174. package/dist/upload/dragAndDropArea/upload_file.svg +0 -4
  175. package/dist/upload/file-upload/audio-icon.svg +0 -4
  176. package/dist/upload/file-upload/close.svg +0 -4
  177. package/dist/upload/file-upload/file-icon.svg +0 -4
  178. package/dist/upload/file-upload/video-icon.svg +0 -4
  179. package/dist/upload/readme.md +0 -37
  180. package/dist/upload/transaction/audio-icon-err.svg +0 -4
  181. package/dist/upload/transaction/audio-icon.svg +0 -4
  182. package/dist/upload/transaction/error-icon.svg +0 -4
  183. package/dist/upload/transaction/file-icon-err.svg +0 -4
  184. package/dist/upload/transaction/file-icon.svg +0 -4
  185. package/dist/upload/transaction/image-icon-err.svg +0 -4
  186. package/dist/upload/transaction/image-icon.svg +0 -4
  187. package/dist/upload/transaction/success-icon.svg +0 -4
  188. package/dist/upload/transaction/video-icon-err.svg +0 -4
  189. package/dist/upload/transaction/video-icon.svg +0 -4
  190. package/dist/wizard/invalid_icon.svg +0 -5
  191. package/dist/wizard/valid_icon.svg +0 -5
  192. package/dist/wizard/validation-wrong.svg +0 -6
  193. package/test/Accordion.test.js +0 -33
  194. package/test/AccordionGroup.test.js +0 -125
  195. package/test/Alert.test.js +0 -53
  196. package/test/Box.test.js +0 -10
  197. package/test/Button.test.js +0 -18
  198. package/test/Card.test.js +0 -30
  199. package/test/Checkbox.test.js +0 -45
  200. package/test/Chip.test.js +0 -25
  201. package/test/Date.test.js +0 -393
  202. package/test/Dialog.test.js +0 -23
  203. package/test/Dropdown.test.js +0 -145
  204. package/test/Footer.test.js +0 -99
  205. package/test/Header.test.js +0 -39
  206. package/test/Heading.test.js +0 -35
  207. package/test/InputText.test.js +0 -240
  208. package/test/Link.test.js +0 -43
  209. package/test/NewDate.test.js +0 -203
  210. package/test/NewInputText.test.js +0 -866
  211. package/test/NewTextarea.test.js +0 -252
  212. package/test/Number.test.js +0 -241
  213. package/test/Paginator.test.js +0 -177
  214. package/test/Password.test.js +0 -76
  215. package/test/ProgressBar.test.js +0 -35
  216. package/test/Radio.test.js +0 -37
  217. package/test/ResultsetTable.test.js +0 -329
  218. package/test/Select.test.js +0 -212
  219. package/test/Sidenav.test.js +0 -45
  220. package/test/Slider.test.js +0 -82
  221. package/test/Spinner.test.js +0 -32
  222. package/test/Switch.test.js +0 -45
  223. package/test/Table.test.js +0 -36
  224. package/test/Tabs.test.js +0 -109
  225. package/test/TabsForSections.test.js +0 -34
  226. package/test/Tag.test.js +0 -32
  227. package/test/TextArea.test.js +0 -52
  228. package/test/ToggleGroup.test.js +0 -81
  229. package/test/Upload.test.js +0 -60
  230. package/test/Wizard.test.js +0 -130
  231. package/test/mocks/pngMock.js +0 -1
  232. 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
- });