|
8 | 8 | TestComboboxControlled, |
9 | 9 | TestComboboxWithGroups, |
10 | 10 | TestComboboxWithClear, |
| 11 | + TestComboboxWithClearClassNameCallback, |
11 | 12 | TestComboboxChipPassThrough, |
12 | 13 | ConformanceInputWrapper, |
13 | 14 | ConformanceActionButtons, |
@@ -270,14 +271,103 @@ test.describe("Combobox", () => { |
270 | 271 | }); |
271 | 272 |
|
272 | 273 | test.describe("clear button", () => { |
273 | | - test("clears selection on click", async ({ mount }) => { |
| 274 | + test("defaults to active visibility and clears selection on click", async ({ |
| 275 | + mount, |
| 276 | + page, |
| 277 | + }) => { |
274 | 278 | const component = await mount(<TestComboboxWithClear />); |
275 | 279 | const input = component.getByPlaceholder("Select a fruit..."); |
276 | | - const clear = component.locator('button[class*="clear"]'); |
| 280 | + const clear = component.getByRole("button", { |
| 281 | + name: "Clear selection", |
| 282 | + includeHidden: true, |
| 283 | + }); |
| 284 | + |
| 285 | + await expect(input).toHaveValue("Apple"); |
| 286 | + await expect(clear).toHaveCSS("display", "none"); |
| 287 | + |
| 288 | + await input.click(); |
| 289 | + await expect(page.getByRole("listbox")).toBeVisible(); |
| 290 | + await expect(clear).toBeVisible(); |
| 291 | + await clear.click(); |
| 292 | + await expect(input).toHaveValue(""); |
| 293 | + }); |
| 294 | + |
| 295 | + test("always clear remains visible while clearable", async ({ mount }) => { |
| 296 | + const component = await mount( |
| 297 | + <TestComboboxWithClear visibility="always" />, |
| 298 | + ); |
| 299 | + const input = component.getByPlaceholder("Select a fruit..."); |
| 300 | + const clear = component.getByRole("button", { name: "Clear selection" }); |
277 | 301 |
|
278 | 302 | await expect(input).toHaveValue("Apple"); |
| 303 | + await expect(clear).toBeVisible(); |
| 304 | + await expect(clear).not.toHaveAttribute("visibility", "always"); |
| 305 | + await expect(clear).not.toHaveAttribute( |
| 306 | + "data-clear-visibility", |
| 307 | + "always", |
| 308 | + ); |
279 | 309 | await clear.click(); |
280 | 310 | await expect(input).toHaveValue(""); |
| 311 | + await expect(clear).toBeHidden(); |
| 312 | + }); |
| 313 | + |
| 314 | + test("preserves Base UI stateful clear className callback", async ({ |
| 315 | + mount, |
| 316 | + }) => { |
| 317 | + const component = await mount(<TestComboboxWithClearClassNameCallback />); |
| 318 | + const clear = component.getByRole("button", { name: "Clear selection" }); |
| 319 | + |
| 320 | + await expect(clear).toHaveClass(/clear-closed/); |
| 321 | + }); |
| 322 | + |
| 323 | + test("active clear is hidden at rest and appears while focused or open", async ({ |
| 324 | + mount, |
| 325 | + page, |
| 326 | + }) => { |
| 327 | + const component = await mount( |
| 328 | + <TestComboboxWithClear visibility="active" />, |
| 329 | + ); |
| 330 | + const input = component.getByPlaceholder("Select a fruit..."); |
| 331 | + const wrapper = page.getByTestId("combobox-clear-wrapper"); |
| 332 | + const clear = component.getByRole("button", { |
| 333 | + name: "Clear selection", |
| 334 | + includeHidden: true, |
| 335 | + }); |
| 336 | + |
| 337 | + const restingPadding = await wrapper.evaluate((element) => |
| 338 | + Number.parseFloat(window.getComputedStyle(element).paddingRight), |
| 339 | + ); |
| 340 | + |
| 341 | + await expect(input).toHaveValue("Apple"); |
| 342 | + await expect(clear).toHaveCSS("display", "none"); |
| 343 | + await expect( |
| 344 | + component.getByRole("button", { name: "Clear selection" }), |
| 345 | + ).toBeHidden(); |
| 346 | + |
| 347 | + await input.click(); |
| 348 | + await expect(page.getByRole("listbox")).toBeVisible(); |
| 349 | + await expect(clear).toBeVisible(); |
| 350 | + |
| 351 | + const activePadding = await wrapper.evaluate((element) => |
| 352 | + Number.parseFloat(window.getComputedStyle(element).paddingRight), |
| 353 | + ); |
| 354 | + expect(activePadding).toBeGreaterThan(restingPadding); |
| 355 | + |
| 356 | + await clear.click(); |
| 357 | + await expect(input).toHaveValue(""); |
| 358 | + }); |
| 359 | + |
| 360 | + test("omitting Clear does not render the clear affordance", async ({ |
| 361 | + mount, |
| 362 | + }) => { |
| 363 | + const component = await mount(<TestCombobox />); |
| 364 | + |
| 365 | + await expect( |
| 366 | + component.getByRole("button", { |
| 367 | + name: "Clear selection", |
| 368 | + includeHidden: true, |
| 369 | + }), |
| 370 | + ).toHaveCount(0); |
281 | 371 | }); |
282 | 372 | }); |
283 | 373 |
|
|
0 commit comments