fix(select): falsy item text should fallback to value (#2152)

This commit is contained in:
Nick Wing 2025-04-18 12:34:12 -05:00 committed by GitHub
commit 61ea8dd82c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 32 additions and 15 deletions

View file

@ -7,3 +7,9 @@
<SelectItem value={0} text="Zero" />
<SelectItem value={1} text="One" />
</Select>
<Select labelText="Undefined text">
<SelectItem value={2} />
<SelectItem value={0} text="Zero" />
<SelectItem value={1} text="One" />
</Select>

View file

@ -240,10 +240,17 @@ describe("Select", () => {
expect(skeleton.children[0]).toHaveClass("bx--skeleton");
});
it("renders value if `text` is falsy", () => {
it("renders `text` instead of `value` if `text` is an empty string", () => {
render(SelectFalsy);
expect(screen.getByLabelText("Falsy text")).toHaveValue("-1");
expect(screen.getByDisplayValue("")).toBeInTheDocument();
expect(screen.getByRole("option", { name: "" })).toBeInTheDocument();
});
it("renders value if `text` is undefined", () => {
render(SelectFalsy);
expect(screen.getByLabelText("Undefined text")).toHaveValue("2");
expect(screen.getByRole("option", { name: "2" })).toBeInTheDocument();
});
});