Skip to content

Commit

Permalink
fix(listbox): render 0 as a valid display value
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaursen committed Aug 11, 2020
1 parent 561c4ba commit d02b7a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 35 additions & 1 deletion packages/form/src/select/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createElement } from "react";
import { getOptionId, getOptionLabel } from "../utils";
import { getOptionId, getOptionLabel, getDisplayLabel } from "../utils";

describe("getOptionId", () => {
it("should return the correct id starting from 1 instead of 0", () => {
Expand Down Expand Up @@ -35,3 +35,37 @@ describe("getOptionLabel", () => {
expect(getOptionLabel({ value: "A" }, "label")).toBe(null);
});
});

describe("getDisplayLabel", () => {
it("should return null if there is no option", () => {
expect(getDisplayLabel(null, "label", true)).toBe(null);
expect(getDisplayLabel(null, "label", false)).toBe(null);
expect(getDisplayLabel("", "label", true)).toBe(null);
expect(getDisplayLabel("", "label", false)).toBe(null);
});

it("should return the label if the includeLeft option is false or the option is not an object", () => {
expect(getDisplayLabel(0, "label", true)).toBe(0);
expect(getDisplayLabel(0, "label", false)).toBe(0);
expect(getDisplayLabel("0", "label", true)).toBe("0");
expect(getDisplayLabel("0", "label", false)).toBe("0");
expect(getDisplayLabel("Hello", "label", true)).toBe("Hello");
expect(getDisplayLabel("Hello", "label", false)).toBe("Hello");
});

it("should return the TextIconSpacing component when includeLeft is enabled and the option is an prop object", () => {
const option = {
leftAddon: "Addon",
label: "Some Words",
};

const result = getDisplayLabel(option, "label", true);
expect(result).toMatchInlineSnapshot(`
<TextIconSpacing
icon="Addon"
>
Some Words
</TextIconSpacing>
`);
});
});
2 changes: 1 addition & 1 deletion packages/form/src/select/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function getDisplayLabel(
labelKey: string,
includeLeft: boolean
): ReactNode {
if (!option) {
if (!option && option !== 0) {
return null;
}

Expand Down

0 comments on commit d02b7a9

Please sign in to comment.