-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSelect.test.jsx
More file actions
350 lines (305 loc) · 11.1 KB
/
Select.test.jsx
File metadata and controls
350 lines (305 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import React from "react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { useField, Formik } from "formik";
import { evolve } from "ramda";
import { Select } from "components";
import SelectField from "formikcomponents/Select";
const options = [
{ label: "Option 1", value: "option-1" },
{ label: "Option 2", value: "option-2" },
];
const remappedLabelAndValueOptions = [
{ lab: "Option 1", val: "option-1" },
{ lab: "Option 2", val: "option-2" },
];
const remappedLabelOptions = [
{ lab: "Option 1", value: "option-1" },
{ lab: "Option 2", value: "option-2" },
];
const intersectionObserverMock = () => ({ observe: () => null });
window.IntersectionObserver = jest
.fn()
.mockImplementation(intersectionObserverMock);
describe("Select", () => {
it("should render without error", () => {
const { getByText } = render(<Select {...{ options }} label="Select" />);
expect(getByText("Select")).toBeInTheDocument();
});
it("should show option list on clicking", async () => {
const { getByRole, getByText } = render(
<Select {...{ options }} label="Select" />
);
const select = getByRole("combobox");
await userEvent.click(select);
expect(getByText("Option 1")).toBeInTheDocument();
expect(getByText("Option 2")).toBeInTheDocument();
});
it("should call onChange on select option", async () => {
const onChange = jest.fn();
const { getByRole, getByText } = render(
<Select {...{ onChange, options }} label="Select" />
);
const select = getByRole("combobox");
await userEvent.click(select);
await userEvent.click(getByText("Option 2"));
expect(onChange).toHaveBeenCalledTimes(1);
});
it("should change selected option value when an option is selected", async () => {
const { getByRole, getByText } = render(
<Select {...{ options }} label="Select" />
);
const select = getByRole("combobox");
await userEvent.click(select);
await userEvent.click(getByText("Option 2"));
expect(getByText("Option 2")).toBeInTheDocument();
});
it("should not render label if label is not provided", () => {
const { queryByTestId } = render(<Select {...{ options }} />);
expect(queryByTestId("select-label")).not.toBeInTheDocument();
});
it("should show error message if provided", () => {
const { getByText, getByTestId } = render(
<Select {...{ options }} error="Error message" label="Select" />
);
expect(getByTestId("select-error")).toBeInTheDocument();
expect(getByText("Error message")).toBeInTheDocument();
});
it("should show help text if provided", () => {
const { getByText, getByTestId } = render(
<Select {...{ options }} helpText="Help text" label="Select" />
);
expect(getByTestId("select-help-text")).toBeInTheDocument();
expect(getByText("Help text")).toBeInTheDocument();
});
it("should create new element when Select is creatable", async () => {
const { getByRole, getByTestId } = render(<Select isCreateable />);
const select = getByRole("combobox");
const selectBox = getByTestId("select");
await userEvent.click(select);
expect(selectBox).toHaveTextContent("No options", { exact: false });
await userEvent.type(select, "hello");
await userEvent.type(select, "{enter}");
expect(selectBox).toHaveTextContent("hello", { exact: false });
});
it("should be searchable with isCreatable", async () => {
const { getByRole } = render(
<Select {...{ options }} isCreateable isSearchable label="Select" />
);
const select = getByRole("combobox");
await userEvent.click(select);
await userEvent.type(select, "option 2");
expect(screen.getByText("Option 2")).toBeInTheDocument();
expect(screen.queryByText("Option 1")).not.toBeInTheDocument();
});
it("should lazy load options when enabled", async () => {
const handleFetchMore = jest.fn();
const totalValues = 10;
const { getByRole } = render(
<Select
{...{ options }}
isAsyncLoadOptionEnabled
fetchMore={handleFetchMore}
totalOptionsCount={totalValues}
/>
);
const select = getByRole("combobox");
await userEvent.click(select);
const menuList = screen.getByTestId("menu-list");
menuList.scrollTop = menuList.scrollHeight;
const loader = screen.getByTestId("loader");
expect(loader).toBeInTheDocument();
});
it("should not lazy load options when isAsyncLoadOptionEnabled is disabled", async () => {
const handleFetchMore = jest.fn();
const totalValues = 10;
const { getByRole } = render(
<Select
{...{ options }}
fetchMore={handleFetchMore}
totalOptionsCount={totalValues}
/>
);
const select = getByRole("combobox");
await userEvent.click(select);
const menuList = screen.getByTestId("menu-list");
menuList.scrollTop = menuList.scrollHeight;
const loader = screen.queryByTestId("loader");
expect(loader).toBeNull();
});
it("should show option list on clicking when remapped label and value is provided with optionRemapping", async () => {
const { getByRole, getByText } = render(
<Select
label="Select"
optionRemapping={{ label: "lab", value: "val" }}
options={remappedLabelAndValueOptions}
/>
);
const select = getByRole("combobox");
await userEvent.click(select);
expect(getByText("Option 1")).toBeInTheDocument();
expect(getByText("Option 2")).toBeInTheDocument();
});
it("should show option list on clicking when remapped label is provided with optionRemapping", async () => {
const { getByRole, getByText } = render(
<Select
label="Select"
optionRemapping={{ label: "lab" }}
options={remappedLabelOptions}
/>
);
const select = getByRole("combobox");
await userEvent.click(select);
expect(getByText("Option 1")).toBeInTheDocument();
expect(getByText("Option 2")).toBeInTheDocument();
});
it("should not show option list on clicking when remapped label is provided without optionRemapping", async () => {
const { getByRole, queryByText } = render(
<Select label="Select" options={remappedLabelOptions} />
);
const select = getByRole("combobox");
await userEvent.click(select);
expect(queryByText("Option 1")).toBeNull();
expect(queryByText("Option 2")).toBeNull();
});
it("should not show the add button when select is multi and no options are selected", async () => {
const { getByRole } = render(
<Select {...{ options }} isMulti label="Select" />
);
const select = getByRole("combobox");
await userEvent.click(select);
expect(screen.queryByText("Add")).not.toBeInTheDocument();
});
it("should show the add button when select is multi and at least one option is selected", async () => {
const { getByRole, getByText } = render(
<Select {...{ options }} isMulti label="Select" />
);
const select = getByRole("combobox");
await userEvent.click(select);
await userEvent.click(getByText("Option 1"));
const addButton = screen.getByText("Add");
expect(addButton).toBeInTheDocument();
expect(addButton).toHaveClass("neeto-ui-btn--style-secondary");
});
it("should show the custom label for add button when select is multi and at least one option is selected", async () => {
const { getByRole, getByText } = render(
<Select
{...{ options }}
isMulti
addButtonLabel="Add more"
label="Select"
/>
);
const select = getByRole("combobox");
await userEvent.click(select);
await userEvent.click(getByText("Option 1"));
const addButton = screen.getByText("Add more");
expect(addButton).toBeInTheDocument();
expect(addButton).toHaveClass("neeto-ui-btn--style-secondary");
});
it("should set the default value for grouped Select", () => {
render(
<Select
defaultValue={[
{
label: "Group 2 - Option 1",
value: "Group 2 - option-1",
},
]}
options={[
{ label: "Group 1", options },
{
label: "Group 2",
options: options.map(
evolve({
label: val => `Group 2 - ${val}`,
value: val => `Group 2 - ${val}`,
})
),
},
]}
/>
);
expect(screen.getByText("Group 2 - Option 1")).toBeInTheDocument();
expect(screen.queryByText("Group 2 - Option 2")).not.toBeInTheDocument();
});
it("should correctly fetch initial value for non-grouped options", () => {
const FormikSelectWrapper = () => {
const [field] = useField("test-select");
return (
<div>
<span data-testid="field-value">{field.value}</span>
<SelectField
{...{ options }}
label="Test Select"
name="test-select"
/>
</div>
);
};
const TestComponent = () => (
<Formik initialValues={{ "test-select": "option-2" }} onSubmit={() => {}}>
<FormikSelectWrapper />
</Formik>
);
render(<TestComponent />);
expect(screen.getByText("Option 2")).toBeInTheDocument();
expect(screen.getByTestId("field-value")).toHaveTextContent("option-2");
});
it("should correctly fetch initial value for grouped options", () => {
const groupedOptions = [
{
label: "Group 1",
options: [
{ label: "Group 1 - Option 1", value: "group1-option1" },
{ label: "Group 1 - Option 2", value: "group1-option2" },
],
},
{
label: "Group 2",
options: [
{ label: "Group 2 - Option 1", value: "group2-option1" },
{ label: "Group 2 - Option 2", value: "group2-option2" },
],
},
];
const FormikSelectWrapper = () => {
const [field] = useField("test-grouped-select");
return (
<div>
<span data-testid="field-value">{field.value}</span>
<SelectField
label="Test Grouped Select"
name="test-grouped-select"
options={groupedOptions}
/>
</div>
);
};
const TestComponent = () => (
<Formik
initialValues={{ "test-grouped-select": "group2-option1" }}
onSubmit={() => {}}
>
<FormikSelectWrapper />
</Formik>
);
render(<TestComponent />);
expect(screen.getByText("Group 2 - Option 1")).toBeInTheDocument();
expect(screen.getByTestId("field-value")).toHaveTextContent(
"group2-option1"
);
});
it("should show tooltip when hovering over an option with tooltipContent", async () => {
const tooltipText = "Tooltip content";
const optionsWithTooltip = [
{ label: "Option 1", value: "option-1", tooltipContent: tooltipText },
];
render(<Select label="Select" options={optionsWithTooltip} />);
const select = screen.getByRole("combobox");
await userEvent.click(select);
const option = screen.getByText("Option 1");
await userEvent.hover(option);
expect(await screen.findByText(tooltipText)).toBeInTheDocument();
});
});