Skip to content

Commit

Permalink
Merge pull request #2316 from trilitech/fix-buy-tez-url-on-web
Browse files Browse the repository at this point in the history
[WEB] Fix buy TEZ url
  • Loading branch information
asiia-trilitech authored Jan 13, 2025
2 parents fe19226 + 3599e02 commit 87edd3a
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 11 deletions.
16 changes: 15 additions & 1 deletion apps/web/src/components/AccountCard/AccountBalance.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
addTestAccount,
assetsActions,
makeStore,
networksActions,
} from "@umami/state";
import { GHOSTNET, MAINNET } from "@umami/tezos";

import { AccountBalance } from "./AccountBalance";
import { act, render, screen, userEvent, waitFor, within } from "../../testUtils";
Expand All @@ -20,7 +22,9 @@ beforeEach(() => {
});

describe("<AccountBalance />", () => {
it("renders a buy tez link", () => {
it("renders a buy tez link for mainnet", () => {
store.dispatch(networksActions.setCurrent(MAINNET));

render(<AccountBalance />, { store });

const link = screen.getByRole("link", { name: "Buy" });
Expand All @@ -31,6 +35,16 @@ describe("<AccountBalance />", () => {
);
});

it("renders a buy tez link for ghostnet", () => {
store.dispatch(networksActions.setCurrent(GHOSTNET));

render(<AccountBalance />, { store });

const link = screen.getByRole("link", { name: "Buy" });
expect(link).toBeVisible();
expect(link).toHaveAttribute("href", "https://faucet.ghostnet.teztnets.com/");
});

it("renders a receive button", () => {
render(<AccountBalance />, { store });

Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/components/AccountCard/AccountBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Box, Flex, Link, Text } from "@chakra-ui/react";
import { useDynamicModalContext } from "@umami/components";
import { useCurrentAccount, useGetAccountBalance, useGetDollarBalance } from "@umami/state";
import {
useBuyTezUrl,
useCurrentAccount,
useGetAccountBalance,
useGetDollarBalance,
} from "@umami/state";
import { TEZ, prettyTezAmount } from "@umami/tezos";

import { SendTezButton } from "./SendTezButton";
Expand All @@ -19,7 +24,7 @@ export const AccountBalance = () => {
const usdBalance = useGetDollarBalance()(address);
const isVerified = useIsAccountVerified();

const buyTezUrl = `https://widget.wert.io/default/widget/?commodity=XTZ&address=${address}&network=tezos&commodity_id=xtz.simple.tezos`;
const buyTezUrl = useBuyTezUrl(address);

const getUsdBalance = () => {
if (balance === undefined) {
Expand Down
30 changes: 28 additions & 2 deletions apps/web/src/views/Activity/Activity.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
makeStore,
networksActions,
} from "@umami/state";
import { MAINNET } from "@umami/tezos";
import { GHOSTNET, MAINNET } from "@umami/tezos";
import {
type TzktCombinedOperation,
getCombinedOperations,
Expand Down Expand Up @@ -45,7 +45,7 @@ describe("<Activity />", () => {
jest.mocked(getRelatedTokenTransfers).mockResolvedValue([]);
});

it("displays an empty state ", async () => {
it("displays an empty state", async () => {
render(<Activity />, { store });

await waitFor(() => {
Expand All @@ -57,6 +57,32 @@ describe("<Activity />", () => {
).toBeVisible();
expect(screen.queryByTestId("view-all-operations-button")).not.toBeInTheDocument();
});

it("has correct mainnet Buy Tez link", async () => {
store.dispatch(networksActions.setCurrent(MAINNET));
render(<Activity />, { store });

await waitFor(() => {
expect(screen.getByTestId("empty-state-message")).toBeVisible();
});
const link = screen.getByRole("link", { name: "Buy Tez Now" });
expect(link).toHaveAttribute(
"href",
`https://widget.wert.io/default/widget/?commodity=XTZ&address=${account.address.pkh}&network=tezos&commodity_id=xtz.simple.tezos`
);
});

it("has correct ghostnet Buy Tez link", async () => {
store.dispatch(networksActions.setCurrent(GHOSTNET));
render(<Activity />, { store });

await waitFor(() => {
expect(screen.getByTestId("empty-state-message")).toBeVisible();
});
const link = screen.getByRole("link", { name: "Buy Tez Now" });
expect(link).toBeVisible();
expect(link).toHaveAttribute("href", "https://faucet.ghostnet.teztnets.com/");
});
});

describe("with operations", () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/views/Activity/Activity.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Center, Divider, Flex, Image, Spinner } from "@chakra-ui/react";
import { type Account } from "@umami/core";
import { useGetOperations } from "@umami/data-polling";
import { useCurrentAccount } from "@umami/state";
import { useBuyTezUrl, useCurrentAccount } from "@umami/state";

import loadingDots from "../../assets/loading-dots.gif";
import { EmptyMessage } from "../../components/EmptyMessage";
Expand All @@ -21,7 +21,7 @@ export const Activity = () => {
isVerified
);

const buyTezUrl = `https://widget.wert.io/default/widget/?commodity=XTZ&address=${currentAccount?.address.pkh}&network=tezos&commodity_id=xtz.simple.tezos`;
const buyTezUrl = useBuyTezUrl(currentAccount?.address.pkh);

const isEmpty = operations.length === 0 && !isLoading;

Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/views/Earn/Earn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { ViewOverlay } from "../../components/ViewOverlay/ViewOverlay";
export const Earn = () => {
const isVerified = useIsAccountVerified();

const buyTezUrl = "https://stake.tezos.com/";
const stakeTezosUrl = "https://stake.tezos.com/";

return (
<>
<Flex zIndex={1} flexGrow={1} width="full">
{isVerified ? (
<EmptyMessage
cta="Start Earning"
ctaUrl={buyTezUrl}
ctaUrl={stakeTezosUrl}
subtitle={"Maximize your tez with staking.com.\nSecure, efficient, and simple."}
title="Boost your rewards"
/>
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/views/Tokens/Token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const Token = ({ token }: TokenProps) => {
_notLast={{
borderBottom: `1px solid ${color("100")}`,
}}
data-testid="token-card"
paddingY={{ base: "18px", md: "30px" }}
>
<Flex alignItems="center" flex="1" gridArea="token">
Expand Down
72 changes: 72 additions & 0 deletions apps/web/src/views/Tokens/Tokens.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { mockImplicitAccount, mockMnemonicAccount } from "@umami/core";
import {
type UmamiStore,
accountsActions,
addTestAccount,
makeStore,
networksActions,
} from "@umami/state";
import { GHOSTNET, MAINNET } from "@umami/tezos";

import { Tokens } from "./Tokens";
import { render, screen, waitFor } from "../../testUtils";

let store: UmamiStore;
const account = mockImplicitAccount(0);

beforeEach(() => {
store = makeStore();
addTestAccount(store, account);
store.dispatch(accountsActions.setCurrent(account.address.pkh));
});

describe("<Tokens />", () => {
beforeEach(() => {
addTestAccount(store, mockMnemonicAccount(1));
addTestAccount(store, mockMnemonicAccount(2));
store.dispatch(networksActions.setCurrent(MAINNET));
});

describe("without tokens", () => {
it("displays an empty state", async () => {
render(<Tokens />, { store });

await waitFor(() => {
expect(screen.getByTestId("empty-state-message")).toBeVisible();
});

expect(screen.getByText("Get Started with Tokens")).toBeVisible();
expect(
screen.getByText("You need Tez to take part in any activity. Buy some to get started.")
).toBeVisible();
expect(screen.getByText("Buy Tez Now")).toBeVisible();
expect(screen.queryByTestId("token-card")).not.toBeInTheDocument();
});

it("has correct mainnet Buy Tez link", async () => {
store.dispatch(networksActions.setCurrent(MAINNET));
render(<Tokens />, { store });

await waitFor(() => {
expect(screen.getByTestId("empty-state-message")).toBeVisible();
});
const link = screen.getByRole("link", { name: "Buy Tez Now" });
expect(link).toHaveAttribute(
"href",
`https://widget.wert.io/default/widget/?commodity=XTZ&address=${account.address.pkh}&network=tezos&commodity_id=xtz.simple.tezos`
);
});

it("has correct ghostnet Buy Tez link", async () => {
store.dispatch(networksActions.setCurrent(GHOSTNET));
render(<Tokens />, { store });

await waitFor(() => {
expect(screen.getByTestId("empty-state-message")).toBeVisible();
});
const link = screen.getByRole("link", { name: "Buy Tez Now" });
expect(link).toBeVisible();
expect(link).toHaveAttribute("href", "https://faucet.ghostnet.teztnets.com/");
});
});
});
4 changes: 2 additions & 2 deletions apps/web/src/views/Tokens/Tokens.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex, VStack } from "@chakra-ui/react";
import { fullId } from "@umami/core";
import { useCurrentAccount, useGetAccountAllTokens } from "@umami/state";
import { useBuyTezUrl, useCurrentAccount, useGetAccountAllTokens } from "@umami/state";

import { Token } from "./Token";
import { EmptyMessage } from "../../components/EmptyMessage";
Expand All @@ -13,7 +13,7 @@ export const Tokens = () => {
const currentAccount = useCurrentAccount()!;
const availableTokens = useGetAccountAllTokens()(currentAccount.address.pkh);

const buyTezUrl = `https://widget.wert.io/default/widget/?commodity=XTZ&address=${currentAccount.address.pkh}&network=tezos&commodity_id=xtz.simple.tezos`;
const buyTezUrl = useBuyTezUrl(currentAccount.address.pkh);

return (
<>
Expand Down
23 changes: 23 additions & 0 deletions packages/state/src/hooks/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GHOSTNET, MAINNET, mockImplicitAddress } from "@umami/tezos";

import {
useAvailableNetworks,
useBuyTezUrl,
useFindNetwork,
useSelectNetwork,
useSelectedNetwork,
Expand Down Expand Up @@ -35,6 +36,28 @@ describe("networkHooks", () => {
});
});

describe("useBuyTezUrl", () => {
it("for mainnet - returns customized mainnet url", () => {
store.dispatch(networksActions.setCurrent(MAINNET));

const {
result: { current },
} = renderHook(() => useBuyTezUrl("pkh123"), { store });
expect(current).toEqual(
`${MAINNET.buyTezUrl}/default/widget/?commodity=XTZ&address=pkh123&network=tezos&commodity_id=xtz.simple.tezos`
);
});

it("for others - returns url from network setting", () => {
store.dispatch(networksActions.setCurrent(GHOSTNET));

const {
result: { current },
} = renderHook(() => useBuyTezUrl("pkh123"), { store });
expect(current).toEqual(GHOSTNET.buyTezUrl);
});
});

describe("useAvailableNetworks", () => {
it("returns default networks by default", () => {
const {
Expand Down
12 changes: 12 additions & 0 deletions packages/state/src/hooks/network.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { MAINNET } from "@umami/tezos";
import { useDispatch } from "react-redux";

import { useAppSelector } from "./useAppSelector";
import { assetsActions, networksActions } from "../slices";

export const useSelectedNetwork = () => useAppSelector(s => s.networks.current);

export const useBuyTezUrl = (pkh?: string) => {
const network = useSelectedNetwork();
let buyTezUrl = network.buyTezUrl;

if (buyTezUrl && network === MAINNET) {
buyTezUrl += `/default/widget/?commodity=XTZ&address=${pkh}&network=tezos&commodity_id=xtz.simple.tezos`;
}

return buyTezUrl;
};

export const useAvailableNetworks = () => useAppSelector(s => s.networks.available);

export const useFindNetwork = () => {
Expand Down

1 comment on commit 87edd3a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Title Lines Statements Branches Functions
apps/desktop Coverage: 83%
83.81% (1787/2132) 79.58% (850/1068) 78.27% (454/580)
apps/web Coverage: 83%
83.81% (1787/2132) 79.58% (850/1068) 78.27% (454/580)
packages/components Coverage: 97%
97.51% (196/201) 95.91% (94/98) 88.13% (52/59)
packages/core Coverage: 81%
82.47% (207/251) 72.72% (88/121) 81.35% (48/59)
packages/crypto Coverage: 100%
100% (43/43) 90.9% (10/11) 100% (7/7)
packages/data-polling Coverage: 97%
95.27% (141/148) 87.5% (21/24) 92.85% (39/42)
packages/multisig Coverage: 98%
98.47% (129/131) 85.71% (18/21) 100% (36/36)
packages/social-auth Coverage: 100%
100% (21/21) 100% (11/11) 100% (3/3)
packages/state Coverage: 85%
84.48% (828/980) 81.35% (192/236) 77.83% (302/388)
packages/tezos Coverage: 89%
88.72% (118/133) 94.59% (35/37) 86.84% (33/38)
packages/tzkt Coverage: 89%
87.32% (62/71) 87.5% (14/16) 80.48% (33/41)

Please sign in to comment.