Skip to content

Instantly share code, notes, and snippets.

@yannvr
Last active May 28, 2024 10:53
Show Gist options
  • Save yannvr/445de8632788fa94ecd77eed121fe257 to your computer and use it in GitHub Desktop.
Save yannvr/445de8632788fa94ecd77eed121fe257 to your computer and use it in GitHub Desktop.
wallet observer tests
import { WalletCore } from "@aptos-labs/wallet-adapter-core";
import { MartianWallet } from "@martianwallet/aptos-wallet-adapter";
import { PetraWallet } from "petra-plugin-wallet-adapter";
import { connectWalletAction, disconnectWalletAction } from "@store/wallet/wallet.reducer";
import { getAptosWalletState } from "@store/wallet/wallet.utils";
import { aptosWalletCore, connectAptosWallet, injectStoreToAptosWalletConfig } from './wallet.config.aptos';
jest.mock("@aptos-labs/wallet-adapter-core");
jest.mock("@martianwallet/aptos-wallet-adapter");
jest.mock("petra-plugin-wallet-adapter");
jest.mock("@store/wallet/wallet.reducer");
jest.mock("@store/wallet/wallet.utils");
describe("Aptos Wallet Config", () => {
let storeMock;
beforeEach(() => {
storeMock = {
dispatch: jest.fn(),
};
injectStoreToAptosWalletConfig(storeMock);
});
it("should connect to wallet", async () => {
const walletName = "MartianWallet";
await connectAptosWallet(walletName);
expect(WalletCore.prototype.connect).toHaveBeenCalledWith(walletName);
expect(WalletCore.prototype.onAccountChange).toHaveBeenCalled();
expect(WalletCore.prototype.onNetworkChange).toHaveBeenCalled();
});
it("should update wallet state when connected", () => {
WalletCore.prototype.isConnected.mockReturnValue(true);
aptosWalletCore.emit("connect");
expect(getAptosWalletState).toHaveBeenCalled();
expect(storeMock.dispatch).toHaveBeenCalledWith(connectWalletAction(expect.any(Object)));
});
it("should dispatch disconnect action when disconnected", () => {
aptosWalletCore.emit("disconnect");
expect(storeMock.dispatch).toHaveBeenCalledWith(disconnectWalletAction("aptos"));
});
// Add more tests for other events like "readyStateChange", "networkChange", "accountChange"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment