-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathMultiSelect.test.tsx
More file actions
188 lines (161 loc) · 5.51 KB
/
MultiSelect.test.tsx
File metadata and controls
188 lines (161 loc) · 5.51 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
import { render } from "../../test/render";
import { screen } from "@testing-library/react";
import { MultiSelect } from "./MultiSelect";
import type { TicketFieldObject } from "../data-types/TicketFieldObject";
describe("MultiSelect", () => {
const mockOnChange = jest.fn();
const baseField: TicketFieldObject = {
id: 1,
name: "test_multiselect",
label: "Test MultiSelect",
type: "multiselect",
value: [],
error: null,
required: false,
description: "",
options: [
{ name: "Option 1", value: "option_1" },
{ name: "Option 2", value: "option_2" },
{ name: "Option 3", value: "option_3" },
{ name: "Option 4", value: "option_4" },
],
};
beforeEach(() => {
jest.clearAllMocks();
});
it("renders field with label", () => {
render(<MultiSelect field={baseField} onChange={mockOnChange} />);
expect(screen.getByText("Test MultiSelect")).toBeInTheDocument();
});
it("shows required indicator when field is required", () => {
const requiredField = { ...baseField, required: true };
render(<MultiSelect field={requiredField} onChange={mockOnChange} />);
expect(screen.getByText("*")).toBeInTheDocument();
});
it("displays description when provided", () => {
const fieldWithDescription = {
...baseField,
description: "<p>Select multiple options</p>",
};
render(
<MultiSelect field={fieldWithDescription} onChange={mockOnChange} />
);
expect(screen.getByText("Select multiple options")).toBeInTheDocument();
});
it("displays error message when error is present", () => {
const fieldWithError = {
...baseField,
error: "At least one option is required",
};
render(<MultiSelect field={fieldWithError} onChange={mockOnChange} />);
expect(
screen.getByText("At least one option is required")
).toBeInTheDocument();
});
it("renders hidden inputs for each selected value for form submission", () => {
const fieldWithValues = {
...baseField,
value: ["option_1", "option_2"],
};
const { container } = render(
<MultiSelect field={fieldWithValues} onChange={mockOnChange} />
);
const hiddenInputs = container.querySelectorAll(
'input[type="hidden"][name="test_multiselect[]"]'
);
expect(hiddenInputs).toHaveLength(2);
expect(hiddenInputs[0]).toHaveValue("option_1");
expect(hiddenInputs[1]).toHaveValue("option_2");
});
it("renders with no hidden inputs when no values selected", () => {
const { container } = render(
<MultiSelect field={baseField} onChange={mockOnChange} />
);
const hiddenInputs = container.querySelectorAll(
'input[type="hidden"][name="test_multiselect[]"]'
);
expect(hiddenInputs).toHaveLength(0);
});
it("handles nested options correctly", () => {
const fieldWithNestedOptions: TicketFieldObject = {
...baseField,
options: [
{ name: "Category::SubCategory::Item", value: "cat_subcat_item" },
{ name: "Another Option", value: "another" },
],
};
const { container } = render(
<MultiSelect field={fieldWithNestedOptions} onChange={mockOnChange} />
);
// Component should render without errors
expect(container).toBeInTheDocument();
});
it("renders with deeply nested selected options", () => {
const fieldWithNestedOptions: TicketFieldObject = {
...baseField,
value: ["cameras_dslr_consumer", "cameras_dslr_pro"],
options: [
{
name: "Cameras::Digital SLR::Consumer",
value: "cameras_dslr_consumer",
},
{
name: "Cameras::Digital SLR::Professional",
value: "cameras_dslr_pro",
},
{ name: "Lenses", value: "lenses" },
],
};
const { container } = render(
<MultiSelect field={fieldWithNestedOptions} onChange={mockOnChange} />
);
const hiddenInputs = container.querySelectorAll(
'input[type="hidden"][name="test_multiselect[]"]'
);
expect(hiddenInputs).toHaveLength(2);
expect(hiddenInputs[0]).toHaveValue("cameras_dslr_consumer");
expect(hiddenInputs[1]).toHaveValue("cameras_dslr_pro");
});
it("initializes with correct values from props", () => {
const fieldWithValues = {
...baseField,
value: ["option_1", "option_4"],
};
const { container } = render(
<MultiSelect field={fieldWithValues} onChange={mockOnChange} />
);
const hiddenInputs = container.querySelectorAll(
'input[type="hidden"][name="test_multiselect[]"]'
);
expect(hiddenInputs).toHaveLength(2);
expect(hiddenInputs[0]).toHaveValue("option_1");
expect(hiddenInputs[1]).toHaveValue("option_4");
});
it("handles empty array value", () => {
const fieldWithEmptyArray = {
...baseField,
value: [],
};
const { container } = render(
<MultiSelect field={fieldWithEmptyArray} onChange={mockOnChange} />
);
const hiddenInputs = container.querySelectorAll(
'input[type="hidden"][name="test_multiselect[]"]'
);
expect(hiddenInputs).toHaveLength(0);
});
it("renders with single selected value", () => {
const fieldWithSingleValue = {
...baseField,
value: ["option_1"],
};
const { container } = render(
<MultiSelect field={fieldWithSingleValue} onChange={mockOnChange} />
);
const hiddenInputs = container.querySelectorAll(
'input[type="hidden"][name="test_multiselect[]"]'
);
expect(hiddenInputs).toHaveLength(1);
expect(hiddenInputs[0]).toHaveValue("option_1");
});
});