Skip to content

Commit d30c2d4

Browse files
committed
fix: combobox tests * 2
1 parent 6e177a5 commit d30c2d4

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

src/components/fields/ComboBox/ComboBox.test.tsx

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,8 @@ describe('<ComboBox />', () => {
6868
});
6969
});
7070

71-
it('should filter options correctly', async () => {
72-
const customFilterFn = jest.fn((textValue, inputValue) =>
73-
textValue.toLowerCase().startsWith(inputValue.toLowerCase()),
74-
);
75-
76-
const { getByRole, getAllByRole, queryByRole, rerender } = renderWithRoot(
71+
it('should filter options with default contains filter', async () => {
72+
const { getByRole, getAllByRole, queryByRole } = renderWithRoot(
7773
<ComboBox label="test">
7874
{items.map((item) => (
7975
<ComboBox.Item key={item.key}>{item.children}</ComboBox.Item>
@@ -83,7 +79,6 @@ describe('<ComboBox />', () => {
8379

8480
const combobox = getByRole('combobox');
8581

86-
// Test default contains filter
8782
await userEvent.type(combobox, 're');
8883

8984
await waitFor(() => {
@@ -95,70 +90,62 @@ describe('<ComboBox />', () => {
9590
expect(options[1]).toHaveTextContent('Green');
9691
});
9792

98-
// Clear and test with custom filter
99-
await userEvent.clear(combobox);
93+
// Close popover
94+
await userEvent.keyboard('{Escape}');
10095

101-
// Wait for clear to complete and popover to close
10296
await waitFor(() => {
103-
expect(combobox).toHaveValue('');
10497
expect(queryByRole('listbox')).not.toBeInTheDocument();
10598
});
99+
});
106100

107-
rerender(
101+
it('should support custom filter function', async () => {
102+
const customFilterFn = jest.fn((textValue, inputValue) =>
103+
textValue.toLowerCase().startsWith(inputValue.toLowerCase()),
104+
);
105+
106+
const { getByRole, getAllByRole } = renderWithRoot(
108107
<ComboBox label="test" filter={customFilterFn}>
109108
{items.map((item) => (
110109
<ComboBox.Item key={item.key}>{item.children}</ComboBox.Item>
111110
))}
112111
</ComboBox>,
113112
);
114113

114+
const combobox = getByRole('combobox');
115+
115116
await userEvent.type(combobox, 're');
116117

117118
await waitFor(() => {
118-
expect(getByRole('listbox')).toBeInTheDocument();
119119
const options = getAllByRole('option');
120120
// Should only match "Red" (starts with 're')
121121
expect(options).toHaveLength(1);
122122
expect(options[0]).toHaveTextContent('Red');
123123
expect(customFilterFn).toHaveBeenCalled();
124124
});
125+
});
125126

126-
// Clear and test with filter disabled
127-
await userEvent.clear(combobox);
128-
129-
// Wait for clear to complete and popover to close
130-
await waitFor(() => {
131-
expect(combobox).toHaveValue('');
132-
expect(queryByRole('listbox')).not.toBeInTheDocument();
133-
});
134-
135-
rerender(
127+
it('should show all items when filter is disabled', async () => {
128+
const { getByRole, getAllByRole } = renderWithRoot(
136129
<ComboBox label="test" filter={false}>
137130
{items.map((item) => (
138131
<ComboBox.Item key={item.key}>{item.children}</ComboBox.Item>
139132
))}
140133
</ComboBox>,
141134
);
142135

136+
const combobox = getByRole('combobox');
137+
143138
await userEvent.type(combobox, 're');
144139

145140
await waitFor(() => {
146-
expect(getByRole('listbox')).toBeInTheDocument();
147141
const options = getAllByRole('option');
148142
// Should show all items when filter is disabled
149143
expect(options).toHaveLength(7);
150144
});
145+
});
151146

152-
// Test filtering with sections
153-
await userEvent.clear(combobox);
154-
155-
// Wait for clear to complete and popover to close
156-
await waitFor(() => {
157-
expect(combobox).toHaveValue('');
158-
expect(queryByRole('listbox')).not.toBeInTheDocument();
159-
});
160-
161-
rerender(
147+
it('should filter options within sections', async () => {
148+
const { getByRole, getAllByRole } = renderWithRoot(
162149
<ComboBox label="test">
163150
<ComboBox.Section key="warm" title="Warm Colors">
164151
<ComboBox.Item key="red">Red</ComboBox.Item>
@@ -173,22 +160,27 @@ describe('<ComboBox />', () => {
173160
</ComboBox>,
174161
);
175162

163+
const combobox = getByRole('combobox');
164+
176165
await userEvent.type(combobox, 'e');
177166

178167
await waitFor(() => {
179-
expect(getByRole('listbox')).toBeInTheDocument();
180168
const options = getAllByRole('option');
181169
// Should match "Red", "Orange", "Yellow", "Green", "Blue", "Purple"
182170
expect(options.length).toBeGreaterThan(0);
183171
});
172+
});
184173

185-
// Test no results
186-
await userEvent.clear(combobox);
174+
it('should close popover when no results match', async () => {
175+
const { getByRole, queryByRole } = renderWithRoot(
176+
<ComboBox label="test">
177+
{items.map((item) => (
178+
<ComboBox.Item key={item.key}>{item.children}</ComboBox.Item>
179+
))}
180+
</ComboBox>,
181+
);
187182

188-
// Wait for clear to complete
189-
await waitFor(() => {
190-
expect(combobox).toHaveValue('');
191-
});
183+
const combobox = getByRole('combobox');
192184

193185
await userEvent.type(combobox, 'zzz');
194186

0 commit comments

Comments
 (0)