Skip to content

Commit 8480c5c

Browse files
authored
Merge pull request #384 from eccenca/bugfix/numberZeroBadge-CMEM-7353
Display number zero badge correctly on icon button (CMEM-7353)
2 parents 56724a5 + 7e1186e commit 8480c5c

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
6262
- `<MultiSuggestField />`
6363
- `onSelection` now sets `newlySelected` only for add actions and no longer sets it to the last element
6464
- border of the BlueprintJS `Tag` elements were fixed
65+
- `<Button />`
66+
- badge is correctly displayed when `badge={0}`
6567
- Focus outlines
6668
- we use again bold focus outlines for input elements
6769
- they are also used for clickable elements that are focused via keyboard (tab navigation)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
4+
import "@testing-library/jest-dom";
5+
6+
import { Badge } from "../../../index";
7+
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
8+
9+
describe("Badge", () => {
10+
it("should shorten a number badge exceeding maxLength to a 9+ notation", () => {
11+
const { container } = render(<Badge maxLength={2}>{42}</Badge>);
12+
const badge = container.querySelector(`.${eccgui}-badge__tag`);
13+
expect(badge).not.toBeNull();
14+
expect(badge).toHaveTextContent("9+");
15+
});
16+
it("should apply maxWidth style to a string badge when maxLength is set", () => {
17+
const { container } = render(<Badge maxLength={4}>forty two</Badge>);
18+
const tag = container.querySelector(`.${eccgui}-badge__tag`);
19+
expect(tag).not.toBeNull();
20+
expect((tag as HTMLElement).style.maxWidth).toBe("calc((3em + 3ch)/2)");
21+
});
22+
});

src/components/Button/Button.test.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { render, screen } from "@testing-library/react";
33

44
import "@testing-library/jest-dom";
55

6+
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
67
import Icon from "../Icon/Icon";
78

89
import Button from "./Button";
@@ -21,7 +22,7 @@ describe("Button", () => {
2122
</Button>
2223
);
2324
expect(screen.getByRole("button").lastChild).toEqual(screen.getByText(/left icon/i));
24-
expect(container.getElementsByClassName("eccgui-icon").length).toBe(1);
25+
expect(container.getElementsByClassName(`${eccgui}-icon`).length).toBe(1);
2526
});
2627

2728
it("should have icon at the right after the text", () => {
@@ -31,6 +32,19 @@ describe("Button", () => {
3132
</Button>
3233
);
3334
expect(screen.getByRole("button").firstChild).toEqual(screen.getByText(/right icon/i));
34-
expect(container.getElementsByClassName("eccgui-icon").length).toBe(1);
35+
expect(container.getElementsByClassName(`${eccgui}-icon`).length).toBe(1);
36+
});
37+
38+
it("should render badge markup with correct content when used on an icon button", () => {
39+
const { container } = render(<Button name="item-info" badge={"badge content"} text={"Cation label"} />);
40+
const badge = container.querySelector(`.${eccgui}-badge`);
41+
expect(badge).not.toBeNull();
42+
expect(badge).toHaveTextContent("badge content");
43+
});
44+
it("should render badge markup with correct content when batch displays a 0 (zero) number on an icon button", () => {
45+
const { container } = render(<Button name="item-info" badge={0} text={"Cation label"} />);
46+
const badge = container.querySelector(`.${eccgui}-badge`);
47+
expect(badge).not.toBeNull();
48+
expect(badge).toHaveTextContent("0");
3549
});
3650
});

src/components/Button/Button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const Button = ({
113113
rightIcon={typeof rightIcon === "string" ? <Icon name={rightIcon} /> : rightIcon}
114114
>
115115
{children}
116-
{badge && (
116+
{typeof badge !== "undefined" && (
117117
<Badge
118118
children={badge}
119119
{...constructBadgeProperties({

0 commit comments

Comments
 (0)