Skip to content

Commit

Permalink
chore: basic tests for the reactive controller
Browse files Browse the repository at this point in the history
  • Loading branch information
wainola committed Oct 5, 2023
1 parent 5149995 commit 8697e81
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/wallet-manager/src/test/WalletManagerController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { LitElement } from 'lit';
import { ExternalProvider } from '@ethersproject/providers';
import { ethers } from 'ethers'
import { describe, it, beforeEach, vi, expect } from 'vitest';
import { WalletManagerController } from '..';

class WidgetTestFixture extends LitElement {
constructor() {
super();
}
}

customElements.define('widget-test-fixture', WidgetTestFixture);

describe('WalletManagerController', () => {
let walletController: WalletManagerController;
const walletTextFixture = new WidgetTestFixture();

beforeEach(() => {
window.ethereum = {
request: () => Promise.resolve(true),
on: vi.fn()
} as ExternalProvider & { on: () => void };

walletController = new WalletManagerController(walletTextFixture);
});

it('should instantiate', () => {
expect(walletController).toBeInstanceOf(WalletManagerController);
});
it('should initialize evm wallet from window', () => {
walletController.initFromWindow();
expect(walletController.evmWallet).toBeDefined();
});
it('should initialize evm wallet from web3 provider', () => {
const provider = new ethers.providers.Web3Provider(window.ethereum);
walletController.initFromWeb3Provider(provider);
expect(walletController.evmWallet).toBeDefined();
});
});
9 changes: 9 additions & 0 deletions packages/wallet-manager/src/wallets/Evm/Evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ class EvmWallet extends events.EventEmitter implements IEvmWallet {
}
}

/**
* @name addChain
* @param chainId
* @param chainName
* @param rpcUrl
* @param nativeCurrency - { name, symbol, decimals }
* @returns void
* @description Adds a new chain to the wallet
*/
public async addChain({
chainId,
chainName,
Expand Down
29 changes: 27 additions & 2 deletions packages/wallet-manager/src/wallets/Substrate/Substrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,46 @@ class SubstrateWallet implements ISusbtrateWallet {
this.apiPromise = apiPromise;
}

static async connectFromWssProvider(wssProvider: string) {
/**
* @name connectFromWssProvider
* @param wssProvider
* @returns SubstrateWallet
* @description Initializes the SubstrateWallet from a wssProvider
*/
static async connectFromWssProvider(
wssProvider: string
): Promise<SubstrateWallet> {
const wsProvider = await SubstrateWallet.conntectToApi(wssProvider);
const apiPromise = await ApiPromise.create({ provider: wsProvider });
return new SubstrateWallet(apiPromise);
}

static connectFromApiPromise(apiPromise: ApiPromise) {
/**
* @name connectFromApiPromise
* @param apiPromise
* @returns SubstrateWallet
* @description Initializes the SubstrateWallet from an ApiPromise
*/
static connectFromApiPromise(apiPromise: ApiPromise): SubstrateWallet {
return new SubstrateWallet(apiPromise);
}

/**
* @name conntectToApi
* @param wssProvider
* @returns WsProvider
* @description Initializes the WsProvider
*/
static async conntectToApi(wssProvider: string): Promise<WsProvider> {
const wsProvider = new WsProvider(wssProvider);
return wsProvider;
}

/**
* @name connect
* @returns void
* @description Connects to the Substrate Wallet
*/
public async connect() {
const injectors = await web3Enable('Polkadot Wallet');
// using polkadot-js extension
Expand Down

0 comments on commit 8697e81

Please sign in to comment.