-
Notifications
You must be signed in to change notification settings - Fork 12
refactor(swap): model confirm modal state as discriminated union #859
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
Open
notJoon
wants to merge
1
commit into
develop
Choose a base branch
from
refactor/swap-confirm-modal-state-union
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
packages/web/src/components/swap/confirm-swap-modal/ConfirmSwapModal.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { Provider as JotaiProvider, createStore } from "jotai"; | ||
| import { render, screen } from "@testing-library/react"; | ||
|
|
||
| import { SwapRateAction } from "@hooks/swap/data/use-swap-handler"; | ||
| import { SwapSummaryInfo } from "@models/swap/swap-summary-info"; | ||
| import { SwapTokenInfo } from "@models/swap/swap-token-info"; | ||
| import GnoswapThemeProvider from "@providers/gnoswap-theme-provider/GnoswapThemeProvider"; | ||
| import { SwapState } from "@states/index"; | ||
| import { SwapConfirmModalState } from "@states/swap"; | ||
| import ConfirmSwapModal from "./ConfirmSwapModal"; | ||
|
|
||
| jest.mock("react-i18next", () => ({ | ||
| useTranslation: () => ({ | ||
| t: (key: string) => key, | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock("@adena-wallet/sdk", () => ({ | ||
| makeMsgCallMessage: jest.fn(), | ||
| makeMsgSendMessage: jest.fn(), | ||
| TransactionBuilder: jest.fn(), | ||
| })); | ||
|
|
||
| const swapSummaryInfo: SwapSummaryInfo = { | ||
| tokenA: { | ||
| type: "GRC20", | ||
| chainId: "dev.gnoswap", | ||
| createdAt: "2023-12-08T03:57:43Z", | ||
| name: "Foo", | ||
| path: "gno.land/r/foo", | ||
| decimals: 4, | ||
| symbol: "FOO", | ||
| logoURI: "https://raw.githubusercontent.com/onbloc/gno-token-resource/main/grc20/images/gno_land_r_foo.svg", | ||
| priceID: "gno.land/r/foo", | ||
| address: "", | ||
| }, | ||
| tokenB: { | ||
| type: "GRC20", | ||
| chainId: "dev.gnoswap", | ||
| createdAt: "2023-12-08T03:57:43Z", | ||
| name: "Bar", | ||
| path: "gno.land/r/bar", | ||
| decimals: 4, | ||
| symbol: "BAR", | ||
| logoURI: "https://raw.githubusercontent.com/onbloc/gno-token-resource/main/grc20/images/gno_land_r_bar.svg", | ||
| priceID: "gno.land/r/bar", | ||
| address: "", | ||
| }, | ||
| swapDirection: "EXACT_IN", | ||
| swapRate: 1.14, | ||
| swapRateUSD: 1.14, | ||
| priceImpact: 0.3, | ||
| guaranteedAmount: { | ||
| amount: 45124, | ||
| currency: "BAR", | ||
| }, | ||
| gasFee: { | ||
| amount: 0.000001, | ||
| currency: "GNOT", | ||
| }, | ||
| gasFeeUSD: 0.1, | ||
| swapRate1USD: 0, | ||
| swapRateAction: SwapRateAction.ATOB, | ||
| protocolFee: "0.30%", | ||
| routerFee: 0.15, | ||
| gasEstimateSuccess: true, | ||
| }; | ||
|
|
||
| const swapTokenInfo: SwapTokenInfo = { | ||
| tokenA: { | ||
| chainId: "dev", | ||
| createdAt: "2023-10-17T05:58:00+09:00", | ||
| name: "Foo", | ||
| address: "g1evezrh92xaucffmtgsaa3rvmz5s8kedffsg469", | ||
| path: "gno.land/r/foo", | ||
| decimals: 4, | ||
| symbol: "FOO", | ||
| logoURI: "https://raw.githubusercontent.com/onbloc/gno-token-resource/main/grc20/images/gno_land_r_foo.svg", | ||
| type: "GRC20", | ||
| priceID: "gno.land/r/foo", | ||
| }, | ||
| tokenAAmount: "10", | ||
| tokenABalance: "100", | ||
| tokenAUSD: 10, | ||
| tokenAUSDStr: "$10.00", | ||
| tokenB: { | ||
| chainId: "dev", | ||
| createdAt: "2023-10-17T05:58:00+09:00", | ||
| name: "Bar", | ||
| address: "g1evezrh92xaucffmtgsaa3rvmz5s8kedffsg470", | ||
| path: "gno.land/r/bar", | ||
| decimals: 4, | ||
| symbol: "BAR", | ||
| logoURI: "https://raw.githubusercontent.com/onbloc/gno-token-resource/main/grc20/images/gno_land_r_bar.svg", | ||
| type: "GRC20", | ||
| priceID: "gno.land/r/bar", | ||
| }, | ||
| tokenBAmount: "11.4", | ||
| tokenBBalance: "200", | ||
| tokenBUSD: 11.4, | ||
| tokenBUSDStr: "$11.40", | ||
| direction: "EXACT_IN", | ||
| slippage: 0.5, | ||
| tokenAPriceGrade: "NONE", | ||
| tokenBPriceGrade: "NONE", | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| submitted: false, | ||
| swapResult: null, | ||
| title: "Confirm swap", | ||
| isWrapOrUnwrap: false, | ||
| priceImpactStatus: "HIGH" as const, | ||
| isLoading: false, | ||
| connectedWallet: true, | ||
| setSwapRateAction: jest.fn(), | ||
| swap: jest.fn(), | ||
| close: jest.fn(), | ||
| }; | ||
|
|
||
| describe("ConfirmSwapModal", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| function renderModal(state: SwapConfirmModalState) { | ||
| const store = createStore(); | ||
| store.set(SwapState.swapConfirmModalState, state); | ||
|
|
||
| return render( | ||
| <JotaiProvider store={store}> | ||
| <GnoswapThemeProvider> | ||
| <ConfirmSwapModal {...defaultProps} /> | ||
| </GnoswapThemeProvider> | ||
| </JotaiProvider>, | ||
| ); | ||
| } | ||
|
|
||
| it("does not render when confirm modal state is idle", () => { | ||
| const { container } = renderModal({ status: "idle" }); | ||
|
|
||
| expect(container.firstChild).toBeNull(); | ||
| expect(screen.queryByText("Confirm swap")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders swap data when confirm modal state is ready", () => { | ||
| renderModal({ | ||
| status: "ready", | ||
| swapTokenInfo, | ||
| swapSummaryInfo, | ||
| isRefetching: false, | ||
| estimatedAmount: null, | ||
| tokenAmountLimit: 45124, | ||
| }); | ||
|
|
||
| expect(screen.getAllByText("Confirm swap")).toHaveLength(2); | ||
| expect(screen.getByText("10")).toBeInTheDocument(); | ||
| expect(screen.getByText("11.4")).toBeInTheDocument(); | ||
| expect(screen.getByText("$10.00")).toBeInTheDocument(); | ||
| expect(screen.getByText("$11.40")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: gnoswap-labs/gnoswap-interface
Length of output: 1871
Update test assertion —
container.firstChildwill not benullwithGnoswapThemeProvider.The idle-case assertion
expect(container.firstChild).toBeNull()will fail becauseGnoswapThemeProviderrenders a<Global>component that produces DOM nodes, even whenConfirmSwapModalcorrectly returnsnull. Since wrapping components inGnoswapThemeProvideris required for tests, replace this assertion withexpect(screen.queryByText("Confirm swap")).not.toBeInTheDocument(), which already provides the correct functional coverage and aligns with the provider's DOM output.