-
Notifications
You must be signed in to change notification settings - Fork 7
feat: wallet card test #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
feat: wallet card test #1275
Changes from 8 commits
5860bc5
2330c48
baf824a
e503a4a
0d1e3ad
05d2f1b
b1128d0
ccc0b40
3836af5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import "@testing-library/jest-dom"; | ||
import { screen, fireEvent } from "@testing-library/react"; | ||
import { Wallet } from "@/types/wallet"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import CashoutAddress from "@/components/cards/wallet/CashoutAddress"; | ||
import { mockUser } from "@__mocks__/fixtures/user"; | ||
import { mockWallet } from "@__mocks__/fixtures/wallet"; | ||
import { KYCSTATUS, openVerificationModal } from "@/store/feature/kyc.slice"; | ||
import { useDispatch } from "@/hooks/useTypedDispatch"; | ||
|
||
jest.mock("@/hooks/useTypedDispatch.ts", () => ({ | ||
useDispatch: jest.fn(), | ||
})); | ||
|
||
const walletMock: Wallet = { | ||
...mockWallet, | ||
title: "Test Wallet", | ||
token: "BTC", | ||
balance: 100, | ||
description: "Test Wallet Description", | ||
payouts: [], | ||
}; | ||
|
||
jest.mock("@/store/feature/kyc.slice", () => { | ||
const actual = jest.requireActual("@/store/feature/kyc.slice"); | ||
return { | ||
...actual, | ||
openVerificationModal: jest.fn(), | ||
}; | ||
}); | ||
|
||
const mockUserStore = { | ||
user: { | ||
data: mockUser, | ||
userBalance: null, | ||
balance: null, | ||
walletAddresses: null, | ||
token: null, | ||
referrals: null, | ||
fetchingUserLoading: false, | ||
filteredUsers: null, | ||
}, | ||
}; | ||
|
||
const verifiedUserStore = { | ||
user: { | ||
data: { | ||
...mockUser, | ||
kycStatus: KYCSTATUS.VERIFIED, | ||
}, | ||
userBalance: null, | ||
balance: null, | ||
walletAddresses: null, | ||
token: null, | ||
referrals: null, | ||
fetchingUserLoading: false, | ||
filteredUsers: null, | ||
}, | ||
}; | ||
const address = walletMock.address ? walletMock.address.match(/.{1,4}/g) : null; | ||
|
||
describe("CashoutAddress component", () => { | ||
let setShowEditModal: () => void; | ||
let setShowPayoutModal: () => void; | ||
const mockDispatch = jest.fn(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already mocked I guess |
||
|
||
beforeEach(() => { | ||
setShowEditModal = jest.fn(); | ||
setShowPayoutModal = jest.fn(); | ||
(useDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You already mocked useDispatch; why again redoing it before each test? |
||
}); | ||
|
||
test("renders CashoutAddress component correctly with given props when cashable is true and there's address", () => { | ||
renderWithRedux(<CashoutAddress wallet={walletMock} setShowEditModal={setShowEditModal} disabled={false} setShowPayoutModal={setShowPayoutModal} />, mockUserStore); | ||
expect(screen.getByTestId("cashoutAddressId")).toBeInTheDocument(); | ||
address?.forEach((text) => { | ||
expect(screen.getByText(text)).toBeInTheDocument(); | ||
}); | ||
expect(screen.getByText("profile.wallets.address-change")).toBeInTheDocument(); | ||
}); | ||
|
||
it("Renders wallet description when there is no address", () => { | ||
renderWithRedux( | ||
<CashoutAddress wallet={{ ...walletMock, address: "" }} setShowEditModal={setShowEditModal} disabled={false} setShowPayoutModal={setShowPayoutModal} />, | ||
mockUserStore | ||
); | ||
expect(screen.getByText(walletMock.description)).toBeInTheDocument(); | ||
expect(screen.getByText("profile.wallets.address-set")).toBeInTheDocument(); | ||
}); | ||
|
||
it("triggers cashout for verified user", () => { | ||
renderWithRedux(<CashoutAddress wallet={walletMock} setShowEditModal={setShowEditModal} disabled={false} setShowPayoutModal={setShowPayoutModal} />, verifiedUserStore); | ||
fireEvent.click(screen.getByText("profile.wallets.cash-out")); | ||
expect(setShowPayoutModal).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
it("Should trigger KYC verification for non-verified user", () => { | ||
renderWithRedux(<CashoutAddress wallet={walletMock} setShowEditModal={setShowEditModal} disabled={false} setShowPayoutModal={setShowPayoutModal} />, mockUserStore); | ||
fireEvent.click(screen.getByText("profile.wallets.cash-out")); | ||
expect(openVerificationModal).toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should trigger edit address modal", () => { | ||
renderWithRedux(<CashoutAddress wallet={walletMock} setShowEditModal={setShowEditModal} disabled={false} setShowPayoutModal={setShowPayoutModal} />, verifiedUserStore); | ||
fireEvent.click(screen.getByText("profile.wallets.address-change")); | ||
expect(setShowEditModal).toHaveBeenCalledWith(true); | ||
expect(mockDispatch).toHaveBeenCalled(); | ||
}); | ||
|
||
}); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code in here is not formatted |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Overview from "@/components/cards/wallet/Overview"; | ||
import { mockWallet } from "@__mocks__/fixtures/wallet"; | ||
import "@testing-library/jest-dom"; | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
const wallet = { | ||
...mockWallet, | ||
title: "some title", | ||
}; | ||
|
||
describe("Overview component", () => { | ||
it("Should render Overview component and all the required elements with props value", () => { | ||
render(<Overview wallet={wallet} />); | ||
expect(screen.getByTestId("overviewId")).toBeInTheDocument(); | ||
expect(screen.getByText(wallet.title)).toBeInTheDocument(); | ||
expect(screen.getByText("profile.wallets.balance")).toBeInTheDocument(); | ||
expect(screen.getByTestId("currencyId")).toBeInTheDocument(); | ||
expect(screen.getByTestId("coin")).toBeInTheDocument(); | ||
expect(screen.getByTestId("tag")).toBeInTheDocument(); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import "@testing-library/jest-dom"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { Wallet } from "@/types/wallet"; | ||
import WalletHint from "@/components/cards/wallet/WalletHint"; | ||
import { mockWallet } from "@__mocks__/fixtures/wallet"; | ||
|
||
const wallet: Wallet = { | ||
...mockWallet, | ||
payouts: [ | ||
{ amount: 100, token: "ETH" }, | ||
{ amount: 200, token: "BTC" }, | ||
], | ||
}; | ||
|
||
describe("WalletHint component", () => { | ||
it("should render WalletHint component with the correct number of Hint components", () => { | ||
render(<WalletHint wallet={wallet} />); | ||
const textElements = screen.getAllByText("profile.wallet.payout.text"); | ||
expect(textElements).toHaveLength(wallet.payouts.length); | ||
}); | ||
|
||
it("renders the correct number of Hint components", () => { | ||
render(<WalletHint wallet={wallet} />); | ||
const hints = screen.getAllByText("profile.wallet.payout.text"); | ||
expect(hints).toHaveLength(wallet.payouts.length); | ||
Comment on lines
+19
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It good already that you are checking the length; it might also be better if you can check the amount and the token is well rendered in the component |
||
}); | ||
|
||
it("renders Currency components with correct values", () => { | ||
render(<WalletHint wallet={wallet} />); | ||
const currencyElements = screen.getAllByTestId("currencyId"); | ||
expect(currencyElements).toHaveLength(wallet.payouts.length); | ||
wallet.payouts.forEach((payout, index) => { | ||
const element = currencyElements[index]; | ||
expect(element).toHaveTextContent(new RegExp(`${payout.token}`)); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import CardsWallet from "@/components/cards/wallet"; | ||
import "@testing-library/jest-dom"; | ||
import { mockWallet } from "@__mocks__/fixtures/wallet"; | ||
import { screen } from "@testing-library/react"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
|
||
// use the actual component when it is done tested | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I beleive we don't need this comment here. |
||
jest.mock("@/components/sections/profile/modals/EditAddress", () => { | ||
return <div>Edit address component</div>; | ||
}); | ||
|
||
jest.mock("@/hooks/useTypedDispatch.ts", () => ({ | ||
useDispatch: jest.fn(), | ||
})); | ||
|
||
describe("Wallet card component", () => { | ||
it("renders the wallet component with all the children", () => { | ||
renderWithRedux(<CardsWallet wallet={mockWallet} disabled={false} />); | ||
expect(screen.getByTestId("cardWalletId")).toBeInTheDocument(); | ||
expect(screen.getByTestId("overviewId")).toBeInTheDocument(); | ||
expect(screen.getByTestId("cashoutAddressId")).toBeInTheDocument(); | ||
expect(screen.queryByText("profile.wallet.payout.text")).not.toBeNull(); | ||
}); | ||
}); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a fixtured instead: