@dxc-technology/halstack-react 0.0.0-eb2a4cc → 0.0.0-ebb089f

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 (229) hide show
  1. package/README.md +2 -2
  2. package/babel.config.js +5 -2
  3. package/dist/BackgroundColorContext.js +46 -0
  4. package/dist/ThemeContext.js +237 -2
  5. package/dist/V3Select/V3Select.js +549 -0
  6. package/dist/V3Select/index.d.ts +27 -0
  7. package/dist/V3Textarea/V3Textarea.js +264 -0
  8. package/dist/V3Textarea/index.d.ts +27 -0
  9. package/dist/accordion/Accordion.js +170 -81
  10. package/dist/accordion/index.d.ts +28 -0
  11. package/dist/accordion-group/AccordionGroup.js +186 -0
  12. package/dist/accordion-group/index.d.ts +16 -0
  13. package/dist/alert/Alert.js +184 -83
  14. package/dist/alert/index.d.ts +51 -0
  15. package/dist/badge/Badge.js +63 -0
  16. package/dist/box/Box.js +31 -23
  17. package/dist/box/index.d.ts +25 -0
  18. package/dist/button/Button.js +84 -29
  19. package/dist/button/Button.stories.js +14 -211
  20. package/dist/button/index.d.ts +24 -0
  21. package/dist/card/Card.js +72 -35
  22. package/dist/card/index.d.ts +22 -0
  23. package/dist/checkbox/Checkbox.js +107 -32
  24. package/dist/checkbox/index.d.ts +24 -0
  25. package/dist/chip/Chip.js +135 -40
  26. package/dist/chip/index.d.ts +22 -0
  27. package/dist/common/RequiredComponent.js +2 -8
  28. package/dist/common/utils.js +2 -22
  29. package/dist/common/variables.js +1472 -159
  30. package/dist/date/Date.js +81 -59
  31. package/dist/date/index.d.ts +27 -0
  32. package/dist/date-input/DateInput.js +400 -0
  33. package/dist/date-input/index.d.ts +95 -0
  34. package/dist/dialog/Dialog.js +61 -36
  35. package/dist/dialog/index.d.ts +18 -0
  36. package/dist/dropdown/Dropdown.js +226 -94
  37. package/dist/dropdown/index.d.ts +26 -0
  38. package/dist/file-input/FileInput.js +644 -0
  39. package/dist/file-input/FileItem.js +287 -0
  40. package/dist/file-input/index.d.ts +81 -0
  41. package/dist/footer/Footer.js +122 -47
  42. package/dist/footer/Icons.js +77 -0
  43. package/dist/footer/index.d.ts +25 -0
  44. package/dist/header/Header.js +211 -91
  45. package/dist/header/Icons.js +59 -0
  46. package/dist/header/index.d.ts +25 -0
  47. package/dist/heading/Heading.js +93 -22
  48. package/dist/heading/index.d.ts +17 -0
  49. package/dist/input-text/Icons.js +22 -0
  50. package/dist/input-text/InputText.js +290 -104
  51. package/dist/input-text/index.d.ts +36 -0
  52. package/dist/layout/ApplicationLayout.js +327 -0
  53. package/dist/layout/Icons.js +55 -0
  54. package/dist/link/Link.js +136 -35
  55. package/dist/link/index.d.ts +23 -0
  56. package/dist/main.d.ts +40 -0
  57. package/dist/main.js +112 -16
  58. package/dist/number-input/NumberInput.js +136 -0
  59. package/dist/number-input/NumberInputContext.js +16 -0
  60. package/dist/number-input/index.d.ts +113 -0
  61. package/dist/paginator/Icons.js +66 -0
  62. package/dist/paginator/Paginator.js +184 -57
  63. package/dist/paginator/index.d.ts +20 -0
  64. package/dist/password-input/PasswordInput.js +203 -0
  65. package/dist/password-input/index.d.ts +94 -0
  66. package/dist/progress-bar/ProgressBar.js +97 -44
  67. package/dist/progress-bar/index.d.ts +18 -0
  68. package/dist/radio/Radio.js +39 -21
  69. package/dist/radio/index.d.ts +23 -0
  70. package/dist/resultsetTable/ResultsetTable.js +93 -69
  71. package/dist/resultsetTable/index.d.ts +19 -0
  72. package/dist/select/Select.js +957 -262
  73. package/dist/select/index.d.ts +131 -0
  74. package/dist/sidenav/Sidenav.js +87 -125
  75. package/dist/sidenav/index.d.ts +13 -0
  76. package/dist/slider/Slider.js +219 -73
  77. package/dist/slider/index.d.ts +29 -0
  78. package/dist/spinner/Spinner.js +249 -64
  79. package/dist/spinner/index.d.ts +17 -0
  80. package/dist/switch/Switch.js +51 -26
  81. package/dist/switch/index.d.ts +24 -0
  82. package/dist/table/Table.js +63 -15
  83. package/dist/table/index.d.ts +13 -0
  84. package/dist/tabs/Tabs.js +208 -35
  85. package/dist/tabs/index.d.ts +19 -0
  86. package/dist/tag/Tag.js +100 -35
  87. package/dist/tag/index.d.ts +24 -0
  88. package/dist/text-input/TextInput.js +974 -0
  89. package/dist/text-input/index.d.ts +135 -0
  90. package/dist/textarea/Textarea.js +250 -107
  91. package/dist/textarea/index.d.ts +117 -0
  92. package/dist/toggle/Toggle.js +16 -19
  93. package/dist/toggle/index.d.ts +21 -0
  94. package/dist/toggle-group/ToggleGroup.js +327 -0
  95. package/dist/toggle-group/index.d.ts +21 -0
  96. package/dist/upload/Upload.js +13 -8
  97. package/dist/upload/buttons-upload/ButtonsUpload.js +35 -25
  98. package/dist/upload/buttons-upload/Icons.js +40 -0
  99. package/dist/upload/dragAndDropArea/DragAndDropArea.js +84 -37
  100. package/dist/upload/dragAndDropArea/Icons.js +39 -0
  101. package/dist/upload/file-upload/FileToUpload.js +64 -33
  102. package/dist/upload/file-upload/Icons.js +66 -0
  103. package/dist/upload/files-upload/FilesToUpload.js +16 -16
  104. package/dist/upload/index.d.ts +15 -0
  105. package/dist/upload/transaction/Icons.js +160 -0
  106. package/dist/upload/transaction/Transaction.js +42 -49
  107. package/dist/upload/transactions/Transactions.js +38 -20
  108. package/dist/useTheme.js +22 -0
  109. package/dist/wizard/Icons.js +65 -0
  110. package/dist/wizard/Wizard.js +156 -61
  111. package/dist/wizard/index.d.ts +18 -0
  112. package/package.json +26 -14
  113. package/test/AccordionGroup.test.js +125 -0
  114. package/test/Date.test.js +15 -13
  115. package/test/DateInput.test.js +242 -0
  116. package/test/Dropdown.test.js +19 -4
  117. package/test/FileInput.test.js +201 -0
  118. package/test/Footer.test.js +2 -7
  119. package/test/Header.test.js +5 -10
  120. package/test/Heading.test.js +60 -12
  121. package/test/InputText.test.js +31 -28
  122. package/test/Link.test.js +25 -7
  123. package/test/NumberInput.test.js +259 -0
  124. package/test/Paginator.test.js +76 -60
  125. package/test/PasswordInput.test.js +83 -0
  126. package/test/ResultsetTable.test.js +65 -17
  127. package/test/Select.test.js +869 -132
  128. package/test/Sidenav.test.js +22 -64
  129. package/test/Slider.test.js +24 -15
  130. package/test/Spinner.test.js +5 -0
  131. package/test/Tabs.test.js +21 -0
  132. package/test/TextInput.test.js +731 -0
  133. package/test/Textarea.test.js +193 -0
  134. package/test/ToggleGroup.test.js +85 -0
  135. package/test/Upload.test.js +1 -1
  136. package/test/V3Select.test.js +212 -0
  137. package/test/{TextArea.test.js → V3TextArea.test.js} +6 -7
  138. package/dist/accordion/Accordion.stories.js +0 -207
  139. package/dist/accordion/readme.md +0 -96
  140. package/dist/alert/Alert.stories.js +0 -158
  141. package/dist/alert/close.svg +0 -4
  142. package/dist/alert/error.svg +0 -4
  143. package/dist/alert/info.svg +0 -4
  144. package/dist/alert/readme.md +0 -43
  145. package/dist/alert/success.svg +0 -4
  146. package/dist/alert/warning.svg +0 -4
  147. package/dist/button/readme.md +0 -93
  148. package/dist/checkbox/Checkbox.stories.js +0 -144
  149. package/dist/checkbox/readme.md +0 -116
  150. package/dist/common/services/example-service.js +0 -10
  151. package/dist/common/services/example-service.test.js +0 -12
  152. package/dist/date/Date.stories.js +0 -205
  153. package/dist/date/calendar.svg +0 -1
  154. package/dist/date/calendar_dark.svg +0 -1
  155. package/dist/date/readme.md +0 -73
  156. package/dist/dialog/Dialog.stories.js +0 -217
  157. package/dist/dialog/readme.md +0 -32
  158. package/dist/dropdown/Dropdown.stories.js +0 -249
  159. package/dist/dropdown/baseline-arrow_drop_down.svg +0 -1
  160. package/dist/dropdown/baseline-arrow_drop_down_wh.svg +0 -4
  161. package/dist/dropdown/baseline-arrow_drop_up.svg +0 -1
  162. package/dist/dropdown/baseline-arrow_drop_up_wh.svg +0 -4
  163. package/dist/dropdown/readme.md +0 -69
  164. package/dist/footer/Footer.stories.js +0 -94
  165. package/dist/footer/dxc_logo_wht.png +0 -0
  166. package/dist/footer/readme.md +0 -41
  167. package/dist/header/Header.stories.js +0 -176
  168. package/dist/header/close_icon.svg +0 -1
  169. package/dist/header/dxc_logo_black.png +0 -0
  170. package/dist/header/dxc_logo_white.png +0 -0
  171. package/dist/header/hamb_menu_black.svg +0 -1
  172. package/dist/header/hamb_menu_white.svg +0 -1
  173. package/dist/header/readme.md +0 -33
  174. package/dist/input-text/InputText.stories.js +0 -209
  175. package/dist/input-text/error.svg +0 -1
  176. package/dist/input-text/readme.md +0 -91
  177. package/dist/link/readme.md +0 -51
  178. package/dist/paginator/images/next.svg +0 -3
  179. package/dist/paginator/images/nextPage.svg +0 -3
  180. package/dist/paginator/images/previous.svg +0 -3
  181. package/dist/paginator/images/previousPage.svg +0 -3
  182. package/dist/paginator/readme.md +0 -50
  183. package/dist/progress-bar/ProgressBar.stories.js +0 -280
  184. package/dist/progress-bar/readme.md +0 -63
  185. package/dist/radio/Radio.stories.js +0 -166
  186. package/dist/radio/readme.md +0 -70
  187. package/dist/resultsetTable/arrow_downward-24px_wht.svg +0 -1
  188. package/dist/resultsetTable/arrow_upward-24px_wht.svg +0 -1
  189. package/dist/resultsetTable/unfold_more-24px_wht.svg +0 -1
  190. package/dist/select/Select.stories.js +0 -235
  191. package/dist/select/readme.md +0 -72
  192. package/dist/sidenav/arrow_icon.svg +0 -3
  193. package/dist/slider/Slider.stories.js +0 -241
  194. package/dist/slider/readme.md +0 -64
  195. package/dist/spinner/Spinner.stories.js +0 -183
  196. package/dist/spinner/readme.md +0 -65
  197. package/dist/switch/Switch.stories.js +0 -134
  198. package/dist/switch/readme.md +0 -133
  199. package/dist/tabs/Tabs.stories.js +0 -130
  200. package/dist/tabs/readme.md +0 -78
  201. package/dist/tabs-for-sections/TabsForSections.js +0 -107
  202. package/dist/tabs-for-sections/readme.md +0 -78
  203. package/dist/toggle/Toggle.stories.js +0 -297
  204. package/dist/toggle/readme.md +0 -80
  205. package/dist/upload/Upload.stories.js +0 -72
  206. package/dist/upload/buttons-upload/drag-drop-icon.svg +0 -4
  207. package/dist/upload/buttons-upload/upload-button.svg +0 -1
  208. package/dist/upload/dragAndDropArea/upload_drop.svg +0 -4
  209. package/dist/upload/dragAndDropArea/upload_file.svg +0 -4
  210. package/dist/upload/file-upload/audio-icon.svg +0 -4
  211. package/dist/upload/file-upload/close.svg +0 -4
  212. package/dist/upload/file-upload/file-icon.svg +0 -4
  213. package/dist/upload/file-upload/video-icon.svg +0 -4
  214. package/dist/upload/readme.md +0 -37
  215. package/dist/upload/transaction/audio-icon-err.svg +0 -4
  216. package/dist/upload/transaction/audio-icon.svg +0 -4
  217. package/dist/upload/transaction/error-icon.svg +0 -4
  218. package/dist/upload/transaction/file-icon-err.svg +0 -4
  219. package/dist/upload/transaction/file-icon.svg +0 -4
  220. package/dist/upload/transaction/image-icon-err.svg +0 -4
  221. package/dist/upload/transaction/image-icon.svg +0 -4
  222. package/dist/upload/transaction/success-icon.svg +0 -4
  223. package/dist/upload/transaction/video-icon-err.svg +0 -4
  224. package/dist/upload/transaction/video-icon.svg +0 -4
  225. package/dist/wizard/invalid_icon.svg +0 -6
  226. package/dist/wizard/valid_icon.svg +0 -6
  227. package/dist/wizard/validation-wrong.svg +0 -6
  228. package/test/TabsForSections.test.js +0 -34
  229. package/test/Toggle.test.js +0 -43
@@ -0,0 +1,731 @@
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 DxcTextInput from "../src/text-input/TextInput";
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("TextInput component tests", () => {
33
+ test("Renders with correct label", () => {
34
+ const { getByText } = render(<DxcTextInput label="Example label" />);
35
+ expect(getByText("Example label")).toBeTruthy();
36
+ });
37
+ test("Renders with correct label and helper text", () => {
38
+ const { getByText } = render(<DxcTextInput 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(<DxcTextInput 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(<DxcTextInput 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(<DxcTextInput label="Error label" placeholder="Placeholder" error="Error message." />);
55
+ expect(getByText("Error message.")).toBeTruthy();
56
+ });
57
+ test("Not optional constraint (onBlur)", () => {
58
+ const onChange = jest.fn();
59
+ const onBlur = jest.fn();
60
+ const { getByRole } = render(
61
+ <DxcTextInput label="Input label" placeholder="Placeholder" onChange={onChange} onBlur={onBlur} clearable />
62
+ );
63
+ const input = getByRole("textbox");
64
+
65
+ fireEvent.focus(input);
66
+ fireEvent.blur(input);
67
+ expect(onBlur).toHaveBeenCalled();
68
+ expect(onBlur).toHaveBeenCalledWith({ value: "", error: "This field is required. Please, enter a value." });
69
+ fireEvent.change(input, { target: { value: "Test" } });
70
+ fireEvent.blur(input);
71
+ expect(onBlur).toHaveBeenCalled();
72
+ expect(onBlur).toHaveBeenCalledWith({ value: "Test", error: null });
73
+ });
74
+ test("Not optional constraint (onChange)", () => {
75
+ const onChange = jest.fn();
76
+ const { getByRole } = render(
77
+ <DxcTextInput label="Input label" placeholder="Placeholder" onChange={onChange} clearable />
78
+ );
79
+ const input = getByRole("textbox");
80
+
81
+ fireEvent.change(input, { target: { value: "Test" } });
82
+ expect(onChange).toHaveBeenCalled();
83
+ expect(onChange).toHaveBeenCalledWith({ value: "Test", error: null });
84
+ userEvent.clear(input);
85
+ expect(onChange).toHaveBeenCalled();
86
+ expect(onChange).toHaveBeenCalledWith({ value: "", error: "This field is required. Please, enter a value." });
87
+ });
88
+ test("Pattern constraint", () => {
89
+ const onChange = jest.fn();
90
+ const onBlur = jest.fn();
91
+ const { getByRole } = render(
92
+ <DxcTextInput
93
+ label="Input label"
94
+ placeholder="Placeholder"
95
+ onChange={onChange}
96
+ onBlur={onBlur}
97
+ margin={{ left: "medium", right: "medium" }}
98
+ clearable
99
+ pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
100
+ />
101
+ );
102
+ const input = getByRole("textbox");
103
+
104
+ fireEvent.change(input, { target: { value: "pattern test" } });
105
+ expect(onChange).toHaveBeenCalled();
106
+ expect(onChange).toHaveBeenCalledWith({ value: "pattern test", error: "Please match the format requested." });
107
+ fireEvent.blur(input);
108
+ expect(onBlur).toHaveBeenCalled();
109
+ expect(onBlur).toHaveBeenCalledWith({ value: "pattern test", error: "Please match the format requested." });
110
+ userEvent.clear(input);
111
+ fireEvent.change(input, { target: { value: "pattern4&" } });
112
+ expect(onChange).toHaveBeenCalled();
113
+ expect(onChange).toHaveBeenCalledWith({ value: "pattern4&", error: null });
114
+ fireEvent.blur(input);
115
+ expect(onBlur).toHaveBeenCalled();
116
+ expect(onBlur).toHaveBeenCalledWith({ value: "pattern4&", error: null });
117
+ });
118
+ test("Length constraint", () => {
119
+ const onChange = jest.fn();
120
+ const onBlur = jest.fn();
121
+ const { getByRole } = render(
122
+ <DxcTextInput
123
+ label="Input label"
124
+ placeholder="Placeholder"
125
+ onChange={onChange}
126
+ onBlur={onBlur}
127
+ margin={{ left: "medium", right: "medium" }}
128
+ clearable
129
+ length={{ min: 5, max: 10 }}
130
+ />
131
+ );
132
+ const input = getByRole("textbox");
133
+
134
+ fireEvent.change(input, { target: { value: "test" } });
135
+ expect(onChange).toHaveBeenCalled();
136
+ expect(onChange).toHaveBeenCalledWith({ value: "test", error: "Min length 5, max length 10." });
137
+ fireEvent.blur(input);
138
+ expect(onBlur).toHaveBeenCalled();
139
+ expect(onBlur).toHaveBeenCalledWith({ value: "test", error: "Min length 5, max length 10." });
140
+ userEvent.clear(input);
141
+ fireEvent.change(input, { target: { value: "length" } });
142
+ expect(onChange).toHaveBeenCalled();
143
+ expect(onChange).toHaveBeenCalledWith({ value: "length", error: null });
144
+ fireEvent.blur(input);
145
+ expect(onBlur).toHaveBeenCalled();
146
+ expect(onBlur).toHaveBeenCalledWith({ value: "length", error: null });
147
+ });
148
+ test("Pattern and length constraints", () => {
149
+ const onChange = jest.fn();
150
+ const onBlur = jest.fn();
151
+ const { getByRole } = render(
152
+ <DxcTextInput
153
+ label="Input label"
154
+ placeholder="Placeholder"
155
+ onChange={onChange}
156
+ onBlur={onBlur}
157
+ margin={{ left: "medium", right: "medium" }}
158
+ clearable
159
+ pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
160
+ length={{ min: 5, max: 10 }}
161
+ />
162
+ );
163
+ const input = getByRole("textbox");
164
+
165
+ fireEvent.change(input, { target: { value: "test" } });
166
+ expect(onChange).toHaveBeenCalled();
167
+ expect(onChange).toHaveBeenCalledWith({ value: "test", error: "Min length 5, max length 10." });
168
+ fireEvent.blur(input);
169
+ expect(onBlur).toHaveBeenCalled();
170
+ expect(onBlur).toHaveBeenCalledWith({ value: "test", error: "Min length 5, max length 10." });
171
+ fireEvent.change(input, { target: { value: "tests" } });
172
+ expect(onChange).toHaveBeenCalled();
173
+ expect(onChange).toHaveBeenCalledWith({ value: "tests", error: "Please match the format requested." });
174
+ fireEvent.blur(input);
175
+ expect(onBlur).toHaveBeenCalled();
176
+ expect(onBlur).toHaveBeenCalledWith({ value: "tests", error: "Please match the format requested." });
177
+ fireEvent.change(input, { target: { value: "tests4&" } });
178
+ expect(onChange).toHaveBeenCalled();
179
+ expect(onChange).toHaveBeenCalledWith({ value: "tests4&", error: null });
180
+ fireEvent.blur(input);
181
+ expect(onBlur).toHaveBeenCalled();
182
+ expect(onBlur).toHaveBeenCalledWith({ value: "tests4&", error: null });
183
+ });
184
+ test("onChange function is called correctly", () => {
185
+ const onChange = jest.fn();
186
+ const { getByRole } = render(<DxcTextInput label="Input label" onChange={onChange} />);
187
+ const input = getByRole("textbox");
188
+ userEvent.type(input, "onchange event test");
189
+ expect(input.value).toBe("onchange event test");
190
+ expect(onChange).toHaveBeenCalled();
191
+ expect(onChange).toHaveBeenCalledWith({ value: "onchange event test", error: null });
192
+ });
193
+ test("onBlur function is called correctly", () => {
194
+ const onBlur = jest.fn();
195
+ const onChange = jest.fn();
196
+ const { getByRole } = render(<DxcTextInput label="Input label" onChange={onChange} onBlur={onBlur} />);
197
+ const input = getByRole("textbox");
198
+ fireEvent.change(input, { target: { value: "Blur test" } });
199
+ fireEvent.blur(input);
200
+ expect(onBlur).toHaveBeenCalled();
201
+ expect(onBlur).toHaveBeenCalledWith({ value: "Blur test", error: null });
202
+ });
203
+ test("Clear action onClick cleans the input", () => {
204
+ const { getByRole } = render(<DxcTextInput label="Input label" clearable />);
205
+ const input = getByRole("textbox");
206
+ userEvent.type(input, "Test");
207
+ const closeAction = getByRole("button");
208
+ userEvent.click(closeAction);
209
+ expect(input.value).toBe("");
210
+ });
211
+ test("Disabled input (action must be shown but not clickable)", () => {
212
+ const onClick = jest.fn();
213
+ const action = {
214
+ onClick: onClick,
215
+ icon: (
216
+ <svg
217
+ data-testid="image"
218
+ xmlns="http://www.w3.org/2000/svg"
219
+ height="24px"
220
+ viewBox="0 0 24 24"
221
+ width="24px"
222
+ fill="currentColor"
223
+ >
224
+ <path d="M0 0h24v24H0V0z" fill="none" />
225
+ <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" />
226
+ </svg>
227
+ ),
228
+ };
229
+ const { getByRole } = render(<DxcTextInput label="Disabled input label" action={action} disabled />);
230
+ const input = getByRole("textbox");
231
+ expect(input.disabled).toBeTruthy();
232
+ userEvent.click(getByRole("button"));
233
+ expect(onClick).not.toHaveBeenCalled();
234
+ });
235
+ test("Disabled input (clear default action should not be displayed, even with text written on the input)", () => {
236
+ const { getByRole, queryByRole } = render(
237
+ <DxcTextInput label="Disabled input label" value="Sample text" disabled clearable />
238
+ );
239
+ const input = getByRole("textbox");
240
+ expect(input.disabled).toBeTruthy();
241
+ expect(queryByRole("button")).toBeFalsy();
242
+ });
243
+ test("Disabled input (suffix and preffix must be displayed)", () => {
244
+ const { getByRole, getByText } = render(
245
+ <DxcTextInput label="Disabled input label" value="Sample text" prefix="+34" suffix="USD" disabled />
246
+ );
247
+ const input = getByRole("textbox");
248
+ expect(input.disabled).toBeTruthy();
249
+ expect(getByText("+34")).toBeTruthy();
250
+ expect(getByText("USD")).toBeTruthy();
251
+ });
252
+ test("Action prop: image displayed and onClick event", () => {
253
+ const onClick = jest.fn();
254
+ const action = {
255
+ onClick: onClick,
256
+ icon: (
257
+ <svg
258
+ data-testid="image"
259
+ xmlns="http://www.w3.org/2000/svg"
260
+ height="24px"
261
+ viewBox="0 0 24 24"
262
+ width="24px"
263
+ fill="currentColor"
264
+ >
265
+ <path d="M0 0h24v24H0V0z" fill="none" />
266
+ <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" />
267
+ </svg>
268
+ ),
269
+ };
270
+ const { getByRole, getByTestId } = render(<DxcTextInput label="Input label" action={action} />);
271
+ expect(getByTestId("image")).toBeTruthy();
272
+ userEvent.click(getByRole("button"));
273
+ expect(onClick).toHaveBeenCalled();
274
+ });
275
+ test("Renders with correct prefix and suffix", () => {
276
+ const { getByText } = render(<DxcTextInput label="Input label" prefix="+34" suffix="USD" />);
277
+ expect(getByText("+34")).toBeTruthy();
278
+ expect(getByText("USD")).toBeTruthy();
279
+ });
280
+ test("Input has correct accesibility attributes", () => {
281
+ const onClick = jest.fn();
282
+ const action = {
283
+ onClick: onClick,
284
+ icon: (
285
+ <svg
286
+ data-testid="image"
287
+ xmlns="http://www.w3.org/2000/svg"
288
+ height="24px"
289
+ viewBox="0 0 24 24"
290
+ width="24px"
291
+ fill="currentColor"
292
+ >
293
+ <path d="M0 0h24v24H0V0z" fill="none" />
294
+ <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" />
295
+ </svg>
296
+ ),
297
+ };
298
+ const { getByRole, getAllByRole } = render(<DxcTextInput label="Example label" clearable action={action} />);
299
+ const input = getByRole("textbox");
300
+ expect(input.getAttribute("aria-autocomplete")).toBeNull();
301
+ expect(input.getAttribute("aria-controls")).toBeNull();
302
+ expect(input.getAttribute("aria-expanded")).toBeNull();
303
+ expect(input.getAttribute("aria-activedescendant")).toBeNull();
304
+ expect(input.getAttribute("aria-invalid")).toBe("false");
305
+ expect(input.getAttribute("aria-describedBy")).toBeNull();
306
+ expect(input.getAttribute("aria-required")).toBe("true");
307
+ userEvent.type(input, "Text");
308
+ const clear = getAllByRole("button")[0];
309
+ expect(clear.getAttribute("aria-label")).toBe("Clear");
310
+ });
311
+ test("Autosuggest has correct accesibility attributes", () => {
312
+ const { getByRole, getAllByRole } = render(
313
+ <DxcTextInput label="Autocomplete Countries" optional suggestions={countries} />
314
+ );
315
+ const input = getByRole("combobox");
316
+ expect(input.getAttribute("aria-autocomplete")).toBe("list");
317
+ expect(input.getAttribute("aria-expanded")).toBe("false");
318
+ expect(input.getAttribute("aria-required")).toBe("false");
319
+ fireEvent.focus(input);
320
+ const list = getByRole("listbox");
321
+ expect(input.getAttribute("aria-controls")).toBe(list.id);
322
+ expect(input.getAttribute("aria-expanded")).toBe("true");
323
+ expect(list.getAttribute("aria-label")).toBe("Autocomplete Countries");
324
+ const options = getAllByRole("option");
325
+ expect(options[0].getAttribute("aria-selected")).toBe("false");
326
+ });
327
+ });
328
+
329
+ describe("TextInput component synchronous autosuggest tests", () => {
330
+ test("Autosuggest is displayed when the input gains focus", async () => {
331
+ const onChange = jest.fn();
332
+ const { getByRole, getByText } = render(
333
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
334
+ );
335
+ const input = getByRole("combobox");
336
+ fireEvent.focus(input);
337
+ const list = getByRole("listbox");
338
+ expect(list).toBeTruthy();
339
+ expect(getByText("Afghanistan")).toBeTruthy();
340
+ expect(getByText("Albania")).toBeTruthy();
341
+ expect(getByText("Algeria")).toBeTruthy();
342
+ expect(getByText("Andorra")).toBeTruthy();
343
+ });
344
+ test("Autosuggest is displayed when the user clicks the input", async () => {
345
+ const onChange = jest.fn();
346
+ const { getByRole, getByText } = render(
347
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
348
+ );
349
+ const input = getByRole("combobox");
350
+ userEvent.click(input);
351
+ const list = getByRole("listbox");
352
+ expect(list).toBeTruthy();
353
+ expect(getByText("Afghanistan")).toBeTruthy();
354
+ expect(getByText("Albania")).toBeTruthy();
355
+ expect(getByText("Algeria")).toBeTruthy();
356
+ expect(getByText("Andorra")).toBeTruthy();
357
+ });
358
+ test("Autosuggest is displayed while the user is writing (if closed previously, if open stays open)", async () => {
359
+ const onChange = jest.fn();
360
+ const { getByRole, queryByRole, getByText, getAllByText } = render(
361
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
362
+ );
363
+ const input = getByRole("combobox");
364
+ fireEvent.focus(input);
365
+ const list = getByRole("listbox");
366
+ expect(list).toBeTruthy();
367
+ fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
368
+ expect(queryByRole("listbox")).toBeFalsy();
369
+ userEvent.type(input, "B");
370
+ expect(list).toBeTruthy();
371
+ expect(getAllByText("B").length).toBe(4);
372
+ expect(getByText("ahamas")).toBeTruthy();
373
+ expect(getByText("ahrain")).toBeTruthy();
374
+ expect(getByText("angladesh")).toBeTruthy();
375
+ expect(getByText("arbados")).toBeTruthy();
376
+ });
377
+ test("Autosuggest is not displayed when prop suggestions is an empty array", async () => {
378
+ const onChange = jest.fn();
379
+ const { queryByRole } = render(
380
+ <DxcTextInput label="Autocomplete Countries" suggestions={[]} onChange={onChange} />
381
+ );
382
+ const input = queryByRole("textbox");
383
+ fireEvent.focus(input);
384
+ expect(queryByRole("listbox")).toBeFalsy();
385
+ });
386
+ test("Autosuggest shows 'No results found' message when there are no matches with the user's input", async () => {
387
+ const onChange = jest.fn();
388
+ const { getByRole, getByText } = render(
389
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
390
+ );
391
+ const input = getByRole("combobox");
392
+ fireEvent.focus(input);
393
+ const list = getByRole("listbox");
394
+ expect(list).toBeTruthy();
395
+ expect(getByText("Afghanistan")).toBeTruthy();
396
+ userEvent.type(input, "x");
397
+ expect(list).toBeTruthy();
398
+ expect(getByText("No results found")).toBeTruthy();
399
+ });
400
+ test("Autosuggest uncontrolled - Suggestion selected by click", async () => {
401
+ const onChange = jest.fn();
402
+ const { getByRole, getByText, queryByRole } = render(
403
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
404
+ );
405
+ const input = getByRole("combobox");
406
+ fireEvent.focus(input);
407
+ userEvent.type(input, "Alba");
408
+ expect(onChange).toHaveBeenCalled();
409
+ expect(getByText("Alba")).toBeTruthy();
410
+ expect(getByText("nia")).toBeTruthy();
411
+ fireEvent.mouseDown(getByRole("option"));
412
+ fireEvent.mouseUp(getByRole("option"));
413
+ expect(input.value).toBe("Albania");
414
+ expect(queryByRole("listbox")).toBeFalsy();
415
+ });
416
+ test("Autosuggest controlled - Suggestion selected by click", async () => {
417
+ const onChange = jest.fn();
418
+ const { getByRole, getByText, queryByRole } = render(
419
+ <DxcTextInput label="Autocomplete Countries" value="Andor" suggestions={countries} onChange={onChange} />
420
+ );
421
+ const input = getByRole("combobox");
422
+ fireEvent.focus(input);
423
+ expect(input.value).toBe("Andor");
424
+ expect(getByText("Andor")).toBeTruthy();
425
+ expect(getByText("ra")).toBeTruthy();
426
+ fireEvent.mouseDown(getByRole("option"));
427
+ fireEvent.mouseUp(getByRole("option"));
428
+ expect(onChange).toHaveBeenCalledWith({ value: "Andorra", error: null });
429
+ expect(queryByRole("listbox")).toBeFalsy();
430
+ });
431
+ test("Autosuggest - Pattern constraint", async () => {
432
+ const onChange = jest.fn();
433
+ const onBlur = jest.fn();
434
+ const { getByRole, getByText } = render(
435
+ <DxcTextInput
436
+ label="Autocomplete Countries"
437
+ suggestions={countries}
438
+ onChange={onChange}
439
+ onBlur={onBlur}
440
+ pattern='^.*(?=.*[a-zA-Z])(?=.*\d)(?=.*[!&$%&? "]).*$'
441
+ />
442
+ );
443
+ const input = getByRole("combobox");
444
+ fireEvent.focus(input);
445
+ userEvent.type(input, "Andor");
446
+ expect(getByText("Andor")).toBeTruthy();
447
+ expect(getByText("ra")).toBeTruthy();
448
+ fireEvent.mouseDown(getByRole("option"));
449
+ fireEvent.mouseUp(getByRole("option"));
450
+ expect(onChange).toHaveBeenCalledWith({ value: "Andorra", error: "Please match the format requested." });
451
+ fireEvent.blur(input);
452
+ expect(onBlur).toHaveBeenCalledWith({ value: "Andorra", error: "Please match the format requested." });
453
+ });
454
+ test("Autosuggest - Length constraint", async () => {
455
+ const onChange = jest.fn();
456
+ const onBlur = jest.fn();
457
+ const { getByText, getByRole } = render(
458
+ <DxcTextInput
459
+ label="Autocomplete Countries"
460
+ suggestions={countries}
461
+ onChange={onChange}
462
+ onBlur={onBlur}
463
+ length={{ min: 5, max: 10 }}
464
+ />
465
+ );
466
+ const input = getByRole("combobox");
467
+ fireEvent.focus(input);
468
+ userEvent.type(input, "Cha");
469
+ expect(getByText("Cha")).toBeTruthy();
470
+ expect(getByText("d")).toBeTruthy();
471
+ fireEvent.mouseDown(getByRole("option"));
472
+ fireEvent.mouseUp(getByRole("option"));
473
+ expect(onChange).toHaveBeenCalledWith({ value: "Cha", error: "Min length 5, max length 10." });
474
+ fireEvent.blur(input);
475
+ expect(onBlur).toHaveBeenCalledWith({ value: "Chad", error: "Min length 5, max length 10." });
476
+ });
477
+ test("Autosuggest keys: arrow down key opens autosuggest, active first option is selected with Enter and closes the autosuggest", async () => {
478
+ Element.prototype.scrollTo = () => {};
479
+ const onChange = jest.fn();
480
+ const { getByRole, queryByRole } = render(
481
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
482
+ );
483
+ const input = getByRole("combobox");
484
+ fireEvent.keyDown(input, { key: "ArrowDown", code: "ArrowDown", keyCode: 40, charCode: 40 });
485
+ const list = getByRole("listbox");
486
+ expect(list).toBeTruthy();
487
+ fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
488
+ expect(input.value).toBe("Afghanistan");
489
+ expect(queryByRole("list")).toBeFalsy();
490
+ });
491
+ test("Autosuggest keys: arrow up key opens autosuggest, active last option is selected with Enter and closes the autosuggest", async () => {
492
+ Element.prototype.scrollTo = () => {};
493
+ const onChange = jest.fn();
494
+ const { getByRole, queryByRole } = render(
495
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
496
+ );
497
+ const input = getByRole("combobox");
498
+ fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
499
+ const list = getByRole("listbox");
500
+ expect(list).toBeTruthy();
501
+ fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
502
+ expect(input.value).toBe("Djibouti");
503
+ expect(queryByRole("list")).toBeFalsy();
504
+ });
505
+ test("Autosuggest keys: Esc key closes the autosuggest and cleans the input", async () => {
506
+ Element.prototype.scrollTo = () => {};
507
+ const onChange = jest.fn();
508
+ const { getByRole, queryByRole } = render(
509
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
510
+ );
511
+ const input = getByRole("combobox");
512
+ fireEvent.focus(input);
513
+ userEvent.type(input, "Bangla");
514
+ const list = getByRole("listbox");
515
+ expect(list).toBeTruthy();
516
+ fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
517
+ expect(input.value).toBe("");
518
+ expect(queryByRole("listbox")).toBeFalsy();
519
+ });
520
+ test("Autosuggest keys: Enter, if no active suggestion closes the autosuggest", async () => {
521
+ Element.prototype.scrollTo = () => {};
522
+ const onChange = jest.fn();
523
+ const { getByRole, queryByRole } = render(
524
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
525
+ );
526
+ const input = getByRole("combobox");
527
+ fireEvent.focus(input);
528
+ const list = getByRole("listbox");
529
+ expect(list).toBeTruthy();
530
+ fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 27, charCode: 27 });
531
+ expect(input.value).toBe("");
532
+ expect(queryByRole("list")).toBeFalsy();
533
+ });
534
+ test("Autosuggest complex key secuence: write, arrow up two times, arrow down and select with Enter. Then, clean with Esc.", async () => {
535
+ Element.prototype.scrollTo = () => {};
536
+ const onChange = jest.fn();
537
+ const { getByRole, queryByRole } = render(
538
+ <DxcTextInput label="Autocomplete Countries" suggestions={countries} onChange={onChange} />
539
+ );
540
+ const input = getByRole("combobox");
541
+ fireEvent.focus(input);
542
+ userEvent.type(input, "Ba");
543
+ fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
544
+ fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUpp", keyCode: 38, charCode: 38 });
545
+ fireEvent.keyDown(input, { key: "ArrowDown", code: "ArrowDown", keyCode: 40, charCode: 40 });
546
+ fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
547
+ expect(input.value).toBe("Barbados");
548
+ expect(queryByRole("listbox")).toBeFalsy();
549
+ fireEvent.keyDown(input, { key: "Esc", code: "Esp", keyCode: 27, charCode: 27 });
550
+ expect(input.value).toBe("");
551
+ expect(queryByRole("listbox")).toBeFalsy();
552
+ });
553
+ });
554
+
555
+ describe("TextInput component asynchronous autosuggest tests", () => {
556
+ test("Autosuggest 'Searching...' message is shown", async () => {
557
+ const callbackFunc = jest.fn((newValue) => {
558
+ const result = new Promise((resolve) =>
559
+ setTimeout(() => {
560
+ resolve(
561
+ newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
562
+ );
563
+ }, 1000)
564
+ );
565
+ return result;
566
+ });
567
+ const onChange = jest.fn();
568
+ const { getByRole, getByText } = render(
569
+ <DxcTextInput label="Autosuggest Countries" suggestions={callbackFunc} onChange={onChange} />
570
+ );
571
+ const input = getByRole("combobox");
572
+ fireEvent.focus(input);
573
+ expect(getByRole("listbox")).toBeTruthy();
574
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
575
+ expect(getByText("Afghanistan")).toBeTruthy();
576
+ expect(getByText("Albania")).toBeTruthy();
577
+ expect(getByText("Algeria")).toBeTruthy();
578
+ expect(getByText("Andorra")).toBeTruthy();
579
+ userEvent.type(input, "Ab");
580
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
581
+ expect(getByText("Cabo Verde")).toBeTruthy();
582
+ fireEvent.keyDown(input, { key: "ArrowUp", code: "ArrowUp", keyCode: 38, charCode: 38 });
583
+ fireEvent.keyDown(input, { key: "Enter", code: "Enter", keyCode: 13, charCode: 13 });
584
+ expect(input.value).toBe("Cabo Verde");
585
+ });
586
+ test("Autosuggest Esc key works while 'Searching...' message is shown", async () => {
587
+ const callbackFunc = jest.fn((newValue) => {
588
+ const result = new Promise((resolve) =>
589
+ setTimeout(() => {
590
+ resolve(
591
+ newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
592
+ );
593
+ }, 1000)
594
+ );
595
+ return result;
596
+ });
597
+ const onChange = jest.fn();
598
+ const { getByRole, queryByText, queryByRole } = render(
599
+ <DxcTextInput label="Autosuggest Countries" suggestions={callbackFunc} onChange={onChange} />
600
+ );
601
+ const input = getByRole("combobox");
602
+ fireEvent.focus(input);
603
+ expect(getByRole("listbox")).toBeTruthy();
604
+ userEvent.type(input, "Ab");
605
+ fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
606
+ expect(queryByRole("listbox")).toBeFalsy();
607
+ expect(queryByText("Searching...")).toBeFalsy();
608
+ expect(input.value).toBe("");
609
+ });
610
+ test("Autosuggest Esc + arrow down working while 'Searching...' message is shown", async () => {
611
+ const callbackFunc = jest.fn((newValue) => {
612
+ const result = new Promise((resolve) =>
613
+ setTimeout(() => {
614
+ resolve(
615
+ newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
616
+ );
617
+ }, 1000)
618
+ );
619
+ return result;
620
+ });
621
+ const onChange = jest.fn();
622
+ const { getByRole, getByText, queryByText, queryByRole } = render(
623
+ <DxcTextInput label="Autosuggest Countries" suggestions={callbackFunc} onChange={onChange} />
624
+ );
625
+ const input = getByRole("combobox");
626
+ fireEvent.focus(input);
627
+ const list = getByRole("listbox");
628
+ expect(list).toBeTruthy();
629
+ userEvent.type(input, "Ab");
630
+ fireEvent.keyDown(input, { key: "Esc", code: "Esc", keyCode: 27, charCode: 27 });
631
+ expect(queryByRole("listbox")).toBeFalsy();
632
+ expect(queryByText("Searching...")).toBeFalsy();
633
+ expect(input.value).toBe("");
634
+ fireEvent.keyDown(input, { key: "ArrowDown", code: "ArrowDown", keyCode: 40, charCode: 40 });
635
+ expect(list).toBeTruthy();
636
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
637
+ expect(getByText("Afghanistan")).toBeTruthy();
638
+ expect(getByText("Albania")).toBeTruthy();
639
+ expect(getByText("Algeria")).toBeTruthy();
640
+ expect(getByText("Andorra")).toBeTruthy();
641
+ });
642
+ test("Asynchronous uncontrolled autosuggest test", async () => {
643
+ const callbackFunc = jest.fn((newValue) => {
644
+ const result = new Promise((resolve) =>
645
+ setTimeout(() => {
646
+ resolve(
647
+ newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
648
+ );
649
+ }, 1000)
650
+ );
651
+ return result;
652
+ });
653
+ const onChange = jest.fn();
654
+ const { getByRole, getByText } = render(
655
+ <DxcTextInput label="Autosuggest Countries" onChange={onChange} suggestions={callbackFunc} />
656
+ );
657
+ const input = getByRole("combobox");
658
+ fireEvent.focus(input);
659
+ userEvent.type(input, "Den");
660
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
661
+ expect(getByText("Denmark")).toBeTruthy();
662
+ fireEvent.mouseDown(getByRole("option"));
663
+ fireEvent.mouseUp(getByRole("option"));
664
+ expect(onChange).toHaveBeenCalledWith({ value: "Denmark", error: null });
665
+ expect(input.value).toBe("Denmark");
666
+ });
667
+ test("Asynchronous controlled autosuggest test", async () => {
668
+ const callbackFunc = jest.fn((newValue) => {
669
+ const result = new Promise((resolve) =>
670
+ setTimeout(() => {
671
+ resolve(
672
+ newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
673
+ );
674
+ }, 1000)
675
+ );
676
+ return result;
677
+ });
678
+ const onChange = jest.fn();
679
+ const { getByRole, getByText, queryByRole } = render(
680
+ <DxcTextInput label="Autosuggest Countries" value="Denm" onChange={onChange} suggestions={callbackFunc} />
681
+ );
682
+ const input = getByRole("combobox");
683
+ fireEvent.focus(input);
684
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
685
+ expect(input.value).toBe("Denm");
686
+ expect(getByText("Denmark")).toBeTruthy();
687
+ fireEvent.mouseDown(getByRole("option"));
688
+ fireEvent.mouseUp(getByRole("option"));
689
+ expect(onChange).toHaveBeenCalledWith({ value: "Denmark", error: null });
690
+ expect(queryByRole("listbox")).toBeFalsy();
691
+ });
692
+ test("Asynchronous autosuggest shows 'No results found' after finishing no matches search", async () => {
693
+ const callbackFunc = jest.fn((newValue) => {
694
+ const result = new Promise((resolve) =>
695
+ setTimeout(() => {
696
+ resolve(
697
+ newValue ? countries.filter((option) => option.toUpperCase().includes(newValue.toUpperCase())) : countries
698
+ );
699
+ }, 1000)
700
+ );
701
+ return result;
702
+ });
703
+ const onChange = jest.fn();
704
+ const { getByRole, getByText } = render(
705
+ <DxcTextInput label="Autosuggest Countries" onChange={onChange} suggestions={callbackFunc} />
706
+ );
707
+ const input = getByRole("combobox");
708
+ fireEvent.focus(input);
709
+ userEvent.type(input, "Example text");
710
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
711
+ expect(getByText("No results found")).toBeTruthy();
712
+ });
713
+ test("Asynchronous autosuggest request failed, shows 'Error fetching data' message", async () => {
714
+ const errorCallbackFunc = jest.fn(() => {
715
+ const result = new Promise((resolve, reject) =>
716
+ setTimeout(() => {
717
+ reject("err");
718
+ }, 1000)
719
+ );
720
+ return result;
721
+ });
722
+ const onChange = jest.fn();
723
+ const { getByRole, getByText } = render(
724
+ <DxcTextInput label="Autosuggest Countries" onChange={onChange} suggestions={errorCallbackFunc} />
725
+ );
726
+ const input = getByRole("combobox");
727
+ fireEvent.focus(input);
728
+ await waitForElementToBeRemoved(() => getByText("Searching..."));
729
+ expect(getByText("Error fetching data")).toBeTruthy();
730
+ });
731
+ });