|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { userEvent } from "@testing-library/user-event"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { CompanySizeFilter } from "../CompanySizeFilter"; |
| 6 | + |
| 7 | +describe("CompanySizeFilter", () => { |
| 8 | + it("renders the six options in order", () => { |
| 9 | + render(<CompanySizeFilter onChange={vi.fn()} value={undefined} />); |
| 10 | + |
| 11 | + const select = screen.getByRole("combobox", { |
| 12 | + name: "Filtrer par effectif", |
| 13 | + }); |
| 14 | + const optionTexts = Array.from(select.querySelectorAll("option")).map( |
| 15 | + (option) => option.textContent, |
| 16 | + ); |
| 17 | + expect(optionTexts).toEqual([ |
| 18 | + "Toutes tailles", |
| 19 | + "Moins de 50 salariés", |
| 20 | + "50 à 99 salariés", |
| 21 | + "100 à 149 salariés", |
| 22 | + "150 à 249 salariés", |
| 23 | + "250 salariés et plus", |
| 24 | + ]); |
| 25 | + }); |
| 26 | + |
| 27 | + it("reflects the controlled value", () => { |
| 28 | + render(<CompanySizeFilter onChange={vi.fn()} value="100-149" />); |
| 29 | + |
| 30 | + const select = screen.getByRole<HTMLSelectElement>("combobox", { |
| 31 | + name: "Filtrer par effectif", |
| 32 | + }); |
| 33 | + expect(select.value).toBe("100-149"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("calls onChange with the selected range", async () => { |
| 37 | + const onChange = vi.fn(); |
| 38 | + const user = userEvent.setup(); |
| 39 | + render(<CompanySizeFilter onChange={onChange} value={undefined} />); |
| 40 | + |
| 41 | + await user.selectOptions( |
| 42 | + screen.getByRole("combobox", { name: "Filtrer par effectif" }), |
| 43 | + "50-99", |
| 44 | + ); |
| 45 | + |
| 46 | + expect(onChange).toHaveBeenCalledWith("50-99"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("calls onChange with undefined when 'Toutes tailles' is selected", async () => { |
| 50 | + const onChange = vi.fn(); |
| 51 | + const user = userEvent.setup(); |
| 52 | + render(<CompanySizeFilter onChange={onChange} value="250+" />); |
| 53 | + |
| 54 | + await user.selectOptions( |
| 55 | + screen.getByRole("combobox", { name: "Filtrer par effectif" }), |
| 56 | + "Toutes tailles", |
| 57 | + ); |
| 58 | + |
| 59 | + expect(onChange).toHaveBeenCalledWith(undefined); |
| 60 | + }); |
| 61 | + |
| 62 | + it("links the label to the select via htmlFor/id", () => { |
| 63 | + render( |
| 64 | + <CompanySizeFilter id="effectif" onChange={vi.fn()} value={undefined} />, |
| 65 | + ); |
| 66 | + |
| 67 | + const select = screen.getByRole("combobox", { |
| 68 | + name: "Filtrer par effectif", |
| 69 | + }); |
| 70 | + expect(select).toHaveAttribute("id", "effectif"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("accepts a custom label", () => { |
| 74 | + render( |
| 75 | + <CompanySizeFilter |
| 76 | + label="Tranche d'effectif" |
| 77 | + onChange={vi.fn()} |
| 78 | + value={undefined} |
| 79 | + />, |
| 80 | + ); |
| 81 | + |
| 82 | + expect( |
| 83 | + screen.getByRole("combobox", { name: "Tranche d'effectif" }), |
| 84 | + ).toBeInTheDocument(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments