Skip to content

Commit 457aa21

Browse files
authored
Merge pull request #1280 from dacadeorg/test/badges-component-test
test: test rewardBadges
2 parents 573215a + a60f00b commit 457aa21

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import "@testing-library/jest-dom";
2+
import RewardBadge, { RewardBadgeProps } from "@/components/badges/RewardBadge";
3+
import { screen } from "@testing-library/react";
4+
import { renderWithRedux } from "@__mocks__/renderWithRedux";
5+
6+
const mockRewardBadges: RewardBadgeProps = {
7+
type: "transparent",
8+
reward: {
9+
token: "BTC",
10+
amount: 2,
11+
},
12+
displayAmount: true,
13+
};
14+
jest.mock("../../../src/utilities", () => ({
15+
shortenNumber: (num: number) => `shortened-${num}`,
16+
}));
17+
18+
describe("RewardBadges", () => {
19+
it("should render rewardBadges", () => {
20+
renderWithRedux(<RewardBadge {...mockRewardBadges} />);
21+
const rewardBadge = screen.getByTestId("RewardBadge");
22+
expect(rewardBadge).toBeInTheDocument();
23+
});
24+
it("should render rewardBadges with coin when the token is provide", () => {
25+
renderWithRedux(<RewardBadge reward={{ token: "BTC" }} />);
26+
const rewardBadge = screen.getByTestId("RewardBadge");
27+
const coin = screen.getByTestId("coin");
28+
expect(rewardBadge).toBeInTheDocument();
29+
expect(coin).toBeInTheDocument();
30+
});
31+
32+
it("should render rewardBadges with amount and token", () => {
33+
renderWithRedux(<RewardBadge reward={{ token: "BTC", amount: 1000 }} displayAmount={true} />);
34+
const text = screen.getByText("shortened-1000 BTC");
35+
expect(text).toBeInTheDocument();
36+
});
37+
38+
it('should display "0" when the reward amount is 0 and token is empty', () => {
39+
renderWithRedux(<RewardBadge reward={{ token: "", amount: 0 }} />);
40+
const rewardBadge = screen.getByTestId("RewardBadge");
41+
expect(rewardBadge).toBeInTheDocument();
42+
expect(rewardBadge).toHaveTextContent("0");
43+
});
44+
});

src/components/badges/RewardBadge.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ interface Reward {
2727
* @typedef {RewardBadgeProps}
2828
*/
2929

30-
interface RewardBadgeProps {
30+
export interface RewardBadgeProps {
3131
type?: "transparent" | "gray" | "light-gray";
3232
reward?: Reward;
3333
displayAmount?: boolean;
34+
rewardBadgeTestId?:string;
3435
}
3536

36-
export default function RewardBadge({ reward = {}, type = "transparent", displayAmount = true }: RewardBadgeProps): ReactElement {
37+
export default function RewardBadge({ reward = {}, type = "transparent", displayAmount = true,rewardBadgeTestId = "RewardBadge" }: RewardBadgeProps): ReactElement {
3738
const { token, amount } = reward;
3839

3940
const badgeClassnames = classNames("font-semibold leading-none text-center inline-flex items-center justify-between rounded-full text-xs p-0.5 h-5 space-x-2", {
@@ -43,7 +44,7 @@ export default function RewardBadge({ reward = {}, type = "transparent", display
4344
});
4445
if (!reward) return <></>;
4546
return (
46-
<span className={badgeClassnames}>
47+
<span data-testid= {rewardBadgeTestId} className={badgeClassnames}>
4748
{token && <Coin token={token} size="small" />}
4849
{amount && (
4950
<div className="font-medium pl-0 pr-2">

src/components/ui/Coin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SUIIcon from "@/icons/tokens/SUI.svg";
1818
* @interface CoinProps
1919
* @typedef {CoinProps}
2020
*/
21-
interface CoinProps {
21+
export interface CoinProps {
2222
bgColor?: string;
2323
textColor?: string;
2424
size?: "medium" | "medium-mini" | "normal" | "small";

0 commit comments

Comments
 (0)