Skip to content

Commit a60f00b

Browse files
committed
test: add more test cases
1 parent 5daa7b9 commit a60f00b

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
import "@testing-library/jest-dom";
2-
import RewardBadge from "@/components/badges/RewardBadge";
3-
import { render, screen } from "@testing-library/react";
4-
import Coin from "@/components/ui/Coin";
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+
}));
517

618
describe("RewardBadges", () => {
719
it("should render rewardBadges", () => {
8-
render(<RewardBadge />);
20+
renderWithRedux(<RewardBadge {...mockRewardBadges} />);
921
const rewardBadge = screen.getByTestId("RewardBadge");
1022
expect(rewardBadge).toBeInTheDocument();
1123
});
12-
13-
it("should render rewardBadges with coins", () => {
14-
render(<RewardBadge />);
15-
render(<Coin />);
16-
const rewardBadge = screen.queryByTestId("RewardBadge");
24+
it("should render rewardBadges with coin when the token is provide", () => {
25+
renderWithRedux(<RewardBadge reward={{ token: "BTC" }} />);
26+
const rewardBadge = screen.getByTestId("RewardBadge");
1727
const coin = screen.getByTestId("coin");
1828
expect(rewardBadge).toBeInTheDocument();
1929
expect(coin).toBeInTheDocument();
2030
});
31+
2132
it("should render rewardBadges with amount and token", () => {
22-
render(<RewardBadge reward={{ token: "BTC", amount: 1000 }} displayAmount={true} />);
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 }} />);
2340
const rewardBadge = screen.getByTestId("RewardBadge");
24-
const coin = screen.getByTestId("coin");
25-
const amount = screen.getByTestId("amount");
2641
expect(rewardBadge).toBeInTheDocument();
27-
expect(coin).toBeInTheDocument();
28-
expect(amount).toBeInTheDocument();
42+
expect(rewardBadge).toHaveTextContent("0");
2943
});
3044
});

src/components/badges/RewardBadge.tsx

Lines changed: 5 additions & 4 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,10 +44,10 @@ export default function RewardBadge({ reward = {}, type = "transparent", display
4344
});
4445
if (!reward) return <></>;
4546
return (
46-
<span data-testid="RewardBadge" className={badgeClassnames}>
47+
<span data-testid= {rewardBadgeTestId} className={badgeClassnames}>
4748
{token && <Coin token={token} size="small" />}
4849
{amount && (
49-
<div data-testid="amount" className="font-medium pl-0 pr-2">
50+
<div className="font-medium pl-0 pr-2">
5051
{displayAmount && shortenNumber(amount)} {token}
5152
</div>
5253
)}

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)