Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wainola committed Oct 2, 2023
1 parent e6caa8d commit 43666c3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/wallet-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { EvmWallet, Substrate } from './wallets';
export { EvmWallet, SubstrateWallet } from './wallets';
18 changes: 13 additions & 5 deletions packages/wallet-manager/src/test/EvmWallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { ethers } from 'ethers';
import { describe, it, expect, beforeEach } from 'vitest';
import { ExternalProvider } from '@ethersproject/providers';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { EvmWallet } from '../';

describe('EvmWallet', () => {
describe('EvmWallet + provider on window', () => {
beforeEach(() => {
window.ethereum = {
request: () => Promise.resolve(true)
};
request: () => Promise.resolve(true),
on: vi.fn()
} as ExternalProvider & { on: () => void };
});
it('should be able to create an instance of EvmWallet', () => {
it('should be able to create an instance of EvmWallet passing a web3Provider', () => {
const w3Provider = new ethers.providers.Web3Provider(window.ethereum);
const evmWallet = new EvmWallet(w3Provider);
const evmWallet = EvmWallet.initFromWeb3Provider(w3Provider);

expect(evmWallet).toBeInstanceOf(EvmWallet);
});

it('should be able to create an instance of EvmWallet without passing a web3Provider', () => {
const evmWallet = EvmWallet.initFromWindow();

expect(evmWallet).toBeInstanceOf(EvmWallet);
});
Expand Down
37 changes: 37 additions & 0 deletions packages/wallet-manager/src/test/Substrate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { SubstrateWallet } from '../wallets';

vi.mock('@polkadot/api', async () => {
const mod =
await vi.importActual<typeof import('@polkadot/api')>('@polkadot/api');
const WsProviderMock = vi.fn();
const ApiPromiseMock = {
create: async () => {}
};
return {
...mod,
WsProvider: WsProviderMock,
ApiPromise: ApiPromiseMock
};
});

describe('SubstrateWallet', () => {
afterEach(() => {
vi.clearAllMocks();
});

it('should be able to create an instance of SubstrateWallet passing an ApiPromise', async () => {
const wsProvider = new WsProvider('wss:someurl');
const apiPromise = await ApiPromise.create({ provider: wsProvider });

const substrateWallet = SubstrateWallet.connectFromApiPromise(apiPromise);
expect(substrateWallet).toBeInstanceOf(SubstrateWallet);
});

it('should be able to create an instance of SubstrateWallet passing a wssProvider', async () => {
const substrateWallet =
await SubstrateWallet.connectFromWssProvider('wss:someurl');
expect(substrateWallet).toBeInstanceOf(SubstrateWallet);
});
});
10 changes: 5 additions & 5 deletions packages/wallet-manager/src/wallets/Substrate/Substrate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { web3Accounts, web3Enable } from '@polkadot/extension-dapp';

class Substrate {
class SubstrateWallet {
substrateAccount?: string;
apiPromise?: ApiPromise;
wssProvider?: WsProvider;
Expand All @@ -11,13 +11,13 @@ class Substrate {
}

static async connectFromWssProvider(wssProvider: string) {
const wsProvider = await Substrate.conntectToApi(wssProvider);
const wsProvider = await SubstrateWallet.conntectToApi(wssProvider);
const apiPromise = await ApiPromise.create({ provider: wsProvider });
return new Substrate(apiPromise);
return new SubstrateWallet(apiPromise);
}

static connectFromApiPromise(apiPromise: ApiPromise) {
return new Substrate(apiPromise);
return new SubstrateWallet(apiPromise);
}

static async conntectToApi(wssProvider: string): Promise<WsProvider> {
Expand All @@ -41,4 +41,4 @@ class Substrate {
}
}

export { Substrate };
export { SubstrateWallet };
2 changes: 1 addition & 1 deletion packages/wallet-manager/src/wallets/Substrate/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Substrate } from './Substrate';
export { SubstrateWallet } from './Substrate';
2 changes: 1 addition & 1 deletion packages/wallet-manager/src/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { EvmWallet } from './Evm';
export { Substrate } from './Substrate';
export { SubstrateWallet } from './Substrate';
3 changes: 2 additions & 1 deletion packages/wallet-manager/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
environment: 'jsdom'
environment: 'jsdom',
exclude: ['**/node_modules/**', '**/dist/**', '**/build/**']
}
});

0 comments on commit 43666c3

Please sign in to comment.