Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Apr 25, 2024
1 parent 333577c commit a80163d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/test/__tests__/chain-wallet-base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ChainWalletBase, ChainRecord } from "@cosmos-kit/core";
import { chains } from "chain-registry";
import { mockExtensionInfo as walletInfo } from '../src/mock-extension/extension/registry';

// Mock global window object
global.window = {
// @ts-ignore
localStorage: {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn()
}
};


const chainRecord: ChainRecord = {
name: 'cosmoshub',
chain: chains.find(c => c.chain_name === 'cosmoshub'),
clientOptions: {
preferredSignType: 'direct'
},
};

const chainWallet = new ChainWalletBase(walletInfo, chainRecord);

async function connectAndFetchAccount() {
try {
await chainWallet.update({ connect: true });
console.log('Connected and account data fetched:', chainWallet.data);
} catch (error) {
console.error('Failed to connect or fetch account data:', error);
}
}

connectAndFetchAccount();

describe('ChainWalletBase', () => {
let chainWallet;
beforeEach(() => {
chainWallet = new ChainWalletBase(walletInfo, chainRecord);
// Mocking necessary methods and properties
// jest.spyOn(chainWallet, 'connectChains').mockResolvedValue(undefined);
jest.spyOn(chainWallet.client, 'getSimpleAccount').mockResolvedValue({
namespace: 'cosmos',
chainId: 'cosmoshub-4',
address: 'cosmos1...'
});
});

it('should update and fetch account data', async () => {
await expect(chainWallet.update({ connect: true })).resolves.not.toThrow();
expect(chainWallet.data.address).toBe('cosmos1...');
});
});
60 changes: 60 additions & 0 deletions packages/test/__tests__/wallet-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { WalletManager, Logger, ChainRecord, Session } from '@cosmos-kit/core';
import { chains, assets } from 'chain-registry';
import { MockExtensionWallet } from '../src/mock-extension/extension';
import { mockExtensionInfo as walletInfo } from '../src/mock-extension/extension/registry';

const logger = new Logger(); function logoutUser() {
console.log('Session expired. Logging out user.');
// Code to log out user
}

// Session duration set for 30 minutes
const userSession = new Session({
duration: 30 * 60 * 1000, // 30 minutes in milliseconds
callback: logoutUser
});

// Start the session when the user logs in
userSession.update();

const mainWalletBase = new MockExtensionWallet(walletInfo)

describe('WalletManager', () => {
let walletManager: WalletManager;

beforeEach(() => {
walletManager = new WalletManager(
chains,
[mainWalletBase],
logger,
true, // throwErrors
true, // subscribeConnectEvents
false // disableIframe
);
});

it('should initialize with provided configurations', () => {
expect(walletManager.throwErrors).toBe(true);
expect(walletManager.subscribeConnectEvents).toBe(true);
expect(walletManager.disableIframe).toBe(false);
expect(walletManager.chainRecords).toHaveLength(2); // Assuming `convertChain` is mocked
});

it('should handle onMounted lifecycle correctly', async () => {
// Mock environment parser
jest.mock('Bowser', () => ({
getParser: () => ({
getBrowserName: jest.fn().mockReturnValue('chrome'),
getPlatform: jest.fn().mockReturnValue({ type: 'desktop' }),
getOSName: jest.fn().mockReturnValue('windows')
})
}));

await walletManager.onMounted();

expect(walletManager.walletRepos).toHaveLength(2); // Depends on internal logic
expect(logger.debug).toHaveBeenCalled(); // Check if debug logs are called
});

// Add more tests as needed for each method
});

0 comments on commit a80163d

Please sign in to comment.