Skip to content
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

fix: packages/core/src/manager.ts - addChains function execution logic error fix. #471

Open
wants to merge 9 commits into
base: testing/mocks
Choose a base branch
from
8 changes: 4 additions & 4 deletions packages/core/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ export class WalletManager extends StateBase {
});
newChainRecords.forEach((chainRecord) => {
const index = this.chainRecords.findIndex(
(chainRecord2) => chainRecord2.name !== chainRecord.name
(chainRecord2) => chainRecord2.name === chainRecord.name
);
if (index == -1) {
if (index === -1) {
this.chainRecords.push(chainRecord);
} else {
this.chainRecords[index] = chainRecord;
Expand Down Expand Up @@ -235,9 +235,9 @@ export class WalletManager extends StateBase {
}

const index = this.walletRepos.findIndex(
(repo2) => repo2.chainName !== repo.chainName
(repo2) => repo2.chainName === repo.chainName
);
if (index == -1) {
if (index === -1) {
this.walletRepos.push(repo);
} else {
this.walletRepos[index] = repo;
Expand Down
93 changes: 53 additions & 40 deletions packages/test/__tests__/chain-wallet-base.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@
import { ChainWalletBase, ChainRecord } from "@cosmos-kit/core";
import { chains } from "chain-registry";
import { ChainWalletBase, ChainRecord } from '@cosmos-kit/core';
import { chains } from 'chain-registry';
import { mockExtensionInfo as walletInfo } from '../src/mock-extension/extension/registry';
import { initActiveWallet } from '../src/utils';
import { getMockFromExtension } from '../src/mock-extension/extension/utils';
import { MockClient } from '../src/mock-extension/extension/client';

// Mock global window object
global.window = {
// @ts-ignore
localStorage: {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn()
}
// @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'
},
name: 'cosmoshub',
chain: chains.find((c) => c.chain_name === 'cosmoshub'),
clientOptions: {
preferredSignType: 'direct',
},
};

const chainWallet = new ChainWalletBase(walletInfo, chainRecord);
// 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);
}
}
// 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();
// 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...');
});
let chainWallet: ChainWalletBase;
beforeEach(async () => {
await initActiveWallet([chainRecord.chain]);

chainWallet = new ChainWalletBase(walletInfo, chainRecord);

chainWallet.initingClient();
const mockWallet = await getMockFromExtension();
chainWallet.initClientDone(new MockClient(mockWallet));

// 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 chainWallet.connect();

expect(
chainWallet.data.address.startsWith(chainRecord.chain.bech32_prefix)
).toBe(true);
expect(chainWallet.data.address).toBe(chainWallet.address);
});
});
28 changes: 20 additions & 8 deletions packages/test/__tests__/cosmos-kit.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import { getMockFromExtension } from '../src/mock-extension/extension/utils';
import { MockWallet } from '../src/mocker';
import { getChainInfoByChainId, initActiveWallet } from '../src/utils';

describe('Wallet functionality', () => {
const wallet = new MockWallet();

it('should handle key retrieval', async () => {
const key = await wallet.getKey('cosmos');
expect(key.bech32Address).toBe('cosmos1...');
const chain = getChainInfoByChainId('cosmoshub-4');

await initActiveWallet([chain]);

const key = await wallet.getKey(chain.chain_id);
expect(key.bech32Address.startsWith('cosmos')).toBe(true);
});

// Add more tests as needed
// Some `Wallet` functions has been tested in `wallet-manager.test.ts` using `ChainWalletContext`
});

describe('getMockFromExtension', () => {
it('returns the provided mock', async () => {
const mock = new MockWallet();
// @ts-ignore
const result = await getMockFromExtension({ mock });
expect(result).toEqual(mock);
});
// it('returns the provided mock', async () => {
// const mock = new MockWallet();
// // @ts-ignore
// const result = await getMockFromExtension({ mock });
// expect(result).toEqual(mock);
// });

it('instantiates MockWallet if no mock is provided', async () => {
const result = await getMockFromExtension();
expect(result).toBeInstanceOf(MockWallet);
});

it('should be only one MockWallet instance in an environment', async () => {
const result = await getMockFromExtension();
const mock = await getMockFromExtension();
expect(result).toBe(mock);
});
});
Loading