Skip to content

Commit

Permalink
test(react): add test for updating badge slot on toggle button
Browse files Browse the repository at this point in the history
add test for conditionally rendering badge in badge slot of toggle button

test #1880
  • Loading branch information
lz405 authored and GCHQ-Developer-299 committed Nov 27, 2024
1 parent a1db00f commit 97dd259
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "../utils/constants";
import { setThresholdBasedOnEnv } from "../../../cypress/utils/helpers";
import {
ConditionalBadge,
Dark,
IconPlacementRight,
IconPlacementTop,
Expand Down Expand Up @@ -94,6 +95,14 @@ describe("IcToggleButton end-to-end tests", () => {
cy.clickOnButton(IC_TOGGLE_BUTTON_SELECTOR);
cy.get(WIN_CONSOLE_SPY).should(NOT_BE_CALLED_ONCE);
});

it("should render elements passed into slots after initial render", () => {
mount(<ConditionalBadge />);
cy.checkHydrated(IC_TOGGLE_BUTTON_SELECTOR);
cy.get("ic-badge").should("not.exist");
cy.get("button").click();
cy.get("ic-badge").should("be.visible");
});
});

describe("IcToggleButton visual regression and a11y tests", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactElement } from "react";
import { IcToggleButton } from "../../components";
import React, { ReactElement, useState } from "react";
import { IcBadge, IcToggleButton } from "../../components";
import { SlottedSVG } from "../../react-component-lib/slottedSVG";

const ReusableSlottedIcon = (): ReactElement => (
Expand Down Expand Up @@ -165,3 +165,29 @@ export const Light = (): ReactElement => {
</div>
);
};

export const ConditionalBadge = (): ReactElement => {
const [count, setCount] = useState(0);
const incrementCount = () => setCount((previous) => previous + 1);

return (
<div
style={{
padding: "var(--ic-space-sm)",
}}
>
<IcToggleButton>
Button with badge
{count > 0 && (
<IcBadge
slot="badge"
variant="info"
textLabel={`${count}`}
maxNumber={99}
/>
)}
</IcToggleButton>
<button onClick={incrementCount}>Increment count</button>
</div>
);
};

0 comments on commit 97dd259

Please sign in to comment.