Skip to content

Commit

Permalink
fix: fix approvals service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Oct 2, 2024
1 parent 15697b6 commit 82488eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
23 changes: 15 additions & 8 deletions apps/extension/src/background/approvals/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { chains } from "@namada/chains";
import { WrapperTxMsgValue } from "@namada/types";
import { ChainsService } from "background/chains";
import { KeyRingService } from "background/keyring";
Expand Down Expand Up @@ -60,10 +61,11 @@ describe("approvals service", () => {
jest.clearAllMocks();
txStore = new KVStoreMock<PendingTx>("PendingTx");
dataStore = new KVStoreMock<string>("DataStore");
keyRingService = createMockInstance(KeyRingService as any);
keyRingService = createMockInstance(KeyRingService);
const vaultService: jest.Mocked<VaultService> = createMockInstance(
VaultService as any
);
chainService = createMockInstance(ChainsService);
const broadcaster: jest.Mocked<ExtensionBroadcaster> =
createMockInstance(ExtensionBroadcaster);
localStorage = new LocalStorage(new KVStoreMock("LocalStorage"));
Expand Down Expand Up @@ -264,7 +266,7 @@ describe("approvals service", () => {
describe("approveConnection", () => {
it("should approve connection if it's not already approved", async () => {
const interfaceOrigin = "origin";
const chainId = "chainId";
const chainId = chains.namada.chainId;
const tabId = 1;

jest.spyOn(service, "isConnectionApproved").mockResolvedValue(false);
Expand All @@ -281,10 +283,11 @@ describe("approvals service", () => {

expect(service["launchApprovalPopup"]).toHaveBeenCalledWith(
"/approve-connection",
{ interfaceOrigin }
{ interfaceOrigin, chainId }
);
expect(service.isConnectionApproved).toHaveBeenCalledWith(
interfaceOrigin
interfaceOrigin,
chainId
);
await expect(promise).resolves.toBeDefined();
});
Expand Down Expand Up @@ -366,7 +369,7 @@ describe("approvals service", () => {
describe("approveDisconnection", () => {
it("should approve disconnection if there is a connection already approved", async () => {
const interfaceOrigin = "origin";
const chainId = "";
const chainId = chains.namada.chainId;
const tabId = 1;

jest.spyOn(service, "isConnectionApproved").mockResolvedValue(true);
Expand All @@ -386,14 +389,15 @@ describe("approvals service", () => {
{ interfaceOrigin }
);
expect(service.isConnectionApproved).toHaveBeenCalledWith(
interfaceOrigin
interfaceOrigin,
chainId
);
await expect(promise).resolves.toBeDefined();
});

it("should not approve disconnection if it is NOT already approved", async () => {
const interfaceOrigin = "origin";
const chainId = "";
const chainId = "chainId";
jest.spyOn(service, "isConnectionApproved").mockResolvedValue(false);

await expect(
Expand Down Expand Up @@ -559,7 +563,8 @@ describe("approvals service", () => {
describe("isConnectionApproved", () => {
it("should return true if origin is approved", async () => {
const origin = "origin";
const chainId = "chainId";
const chainId = chains.namada.chainId;
jest.spyOn(chainService, "getChain").mockResolvedValue(chains.namada);
jest
.spyOn(localStorage, "getApprovedOrigins")
.mockResolvedValue([origin]);
Expand All @@ -572,6 +577,7 @@ describe("approvals service", () => {
it("should return false if origin is not approved", async () => {
const origin = "origin";
const chainId = "chainId";
jest.spyOn(chainService, "getChain").mockResolvedValue(chains.namada);
jest.spyOn(localStorage, "getApprovedOrigins").mockResolvedValue([]);

await expect(service.isConnectionApproved(origin, chainId)).resolves.toBe(
Expand All @@ -582,6 +588,7 @@ describe("approvals service", () => {
it("should return false if there are no origins in store", async () => {
const origin = "origin";
const chainId = "chainId";
jest.spyOn(chainService, "getChain").mockResolvedValue(chains.namada);
jest
.spyOn(localStorage, "getApprovedOrigins")
.mockResolvedValue(undefined);
Expand Down
4 changes: 3 additions & 1 deletion apps/namadillo/src/App/Transfer/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ export const Example = (): JSX.Element => {
sourceWallet={selectedWallet ? wallets[selectedWallet] : undefined}
onChangeWallet={async (wallet: WalletProvider) => {
try {
await integrations[wallet.id].connect();
setWallet(wallet.id);
if (!chainId) {
setChainId(cosmos.chain_id);
}
if (chainId) {
await integrations[wallet.id].connect(chainId);
}
} catch (err) {
console.error(err);
}
Expand Down
6 changes: 5 additions & 1 deletion apps/namadillo/src/App/Transfer/SelectedWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { integrations } from "@namada/integrations";
import { shortenAddress } from "@namada/utils";
import { chainParametersAtom } from "atoms/chain";
import clsx from "clsx";
import { useAtomValue } from "jotai";
import { useEffect, useState } from "react";
import { WalletProvider } from "types";

Expand All @@ -16,12 +18,14 @@ export const SelectedWallet = ({
isShielded,
}: SelectedWalletProps): JSX.Element => {
const [walletAddress, setWalletAddress] = useState("");
const { data: chain } = useAtomValue(chainParametersAtom);

const loadAccounts = async (): Promise<void> => {
try {
const integration = integrations[wallet.id];
integration.detect();
await integration.connect();
// TODO: This likely isn't correct:
await integration.connect(chain!.chainId);
const accounts = await integration.accounts();

if (accounts && accounts.length > 0) {
Expand Down

0 comments on commit 82488eb

Please sign in to comment.