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

chore: adding tests for core dapp-kit #81

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/lint-build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ jobs:
- name: Lint
run: yarn run lint

- name: Test
run: yarn run test

- name: Build
run: yarn run build
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prepare": "husky install",
"purge": "npx turbo@latest run purge && rm -rf node_modules",
"reinstall": "yarn clean && yarn purge && yarn && yarn run build:deps",
"test": "turbo run test"
"test": "turbo run test --filter='@vechainfoundation/*'"
},
"husky": {
"hooks": {
Expand Down
8 changes: 7 additions & 1 deletion packages/dapp-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"clean": "rm -rf dist .turbo",
"lint": "tsc --noEmit && eslint src --ext .js,.jsx,.ts,.tsx",
"purge": "yarn clean && rm -rf node_modules",
"test": "vitest run --coverage",
"watch": "tsup src/index.ts --format cjs,esm --watch --dts"
},
"dependencies": {
Expand All @@ -25,9 +26,14 @@
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@vechain/repo-config": "https://github.com/vechainfoundation/repo-config#v0.0.1",
"@vitest/coverage-v8": "^0.34.6",
"@walletconnect/types": "2.10.2",
"eslint": "*",
"happy-dom": "^12.10.3",
"lokijs": "^1.5.12",
"tsup": "*",
"typescript": "*"
"typescript": "*",
"vite": "^4.5.0",
"vitest": "^0.34.6"
}
}
10 changes: 5 additions & 5 deletions packages/dapp-kit/src/connex.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { createNoVendor } from '@vechain/connex/esm/driver';
import { createNoVendor, LazyDriver } from '@vechain/connex/esm/driver';
import { newThor } from '@vechain/connex-framework/dist/thor';
import type { DriverNoVendor } from '@vechain/connex-driver';
import { newVendor } from '@vechain/connex-framework';
import type { ConnexOptions } from './types';
import { normalizeGenesisBlock } from './genesis';
import { FullDriver } from './full-driver';
import { WalletManager } from './wallet-manager';

class MultiWalletConnex {
Expand All @@ -23,10 +22,11 @@ class MultiWalletConnex {
);

const walletManager = new WalletManager(options);
const fullDriver = new FullDriver(thorOnlyDriver, walletManager);
const lazyDriver = new LazyDriver(Promise.resolve(walletManager));
lazyDriver.setNoVendor(thorOnlyDriver);

const thor = newThor(fullDriver);
const vendor = newVendor(fullDriver);
const thor = newThor(lazyDriver);
const vendor = newVendor(lazyDriver);

this.thor = thor;
this.vendor = vendor;
Expand Down
78 changes: 0 additions & 78 deletions packages/dapp-kit/src/full-driver.ts

This file was deleted.

9 changes: 4 additions & 5 deletions packages/dapp-kit/src/wallet-connect/signer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference types="@vechain/connex-types" />
import type { SessionTypes } from '@walletconnect/types';
import type { ProposalTypes } from '@walletconnect/types/dist/types/sign-client/proposal';
import type { EngineTypes } from '@walletconnect/types/dist/types/sign-client/engine';
Expand Down Expand Up @@ -152,12 +151,12 @@ export const newWcSigner = ({
vechain: namespace,
};

const { uri, approval } = await signClient.connect({
const res = await signClient.connect({
requiredNamespaces,
});

if (uri) {
await web3Modal.openModal({ uri });
if (res.uri) {
await web3Modal.openModal({ uri: res.uri });
}

return await new Promise((resolve, reject) => {
Expand All @@ -168,7 +167,7 @@ export const newWcSigner = ({
}
});

approval()
res.approval()
.then((newSession) => {
session = newSession;
endSubscription();
Expand Down
88 changes: 88 additions & 0 deletions packages/dapp-kit/test/create-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, expect, it, vi } from 'vitest';
import type { Connex1 } from '@vechain/connex/esm/signer';
import { createWallet } from '../src/create-wallet';
import type { ConnexOptions, WalletConnectOptions, WalletSource } from '../src';

type ICreateWallet = ConnexOptions & {
source: WalletSource;
onDisconnected: () => void;
};
const createOptions = (
source: WalletSource,
wcOptions?: WalletConnectOptions,
): ICreateWallet => {
return {
nodeUrl: 'https://testnet.veblocks.net/',
source,
walletConnectOptions: wcOptions,
genesis: 'main',
customWcModal: undefined,
onDisconnected: () => {},
};
};

vi.mock('@walletconnect/modal');

describe('createWallet', () => {
describe('sync', () => {
it('is NOT in sync browser', () => {
window.connex = undefined;

expect(() => {
createWallet(createOptions('sync'));
}).toThrowError('User is not in a Sync wallet');
});

it('is in sync2 browser', () => {
window.connex = {} as Connex1;

const wallet = createWallet(createOptions('sync2'));

expect(wallet).toBeDefined();
});
});

describe('veworld-extension', () => {
it('is not installed', () => {
window.vechain = undefined;

expect(() => {
createWallet(createOptions('veworld-extension'));
}).toThrowError('VeWorld Extension is not installed');
});

it('is installed', () => {
window.vechain = {
newConnexSigner: () => ({} as Connex.Signer),
};

const wallet = createWallet(createOptions('veworld-extension'));

expect(wallet).toBeDefined();
});
});

describe('wallet-connect', () => {
it('no options provided', () => {
expect(() => {
createWallet(createOptions('wallet-connect'));
}).toThrowError('WalletConnect options are not provided');
});

it('options provided', () => {
const wallet = createWallet(
createOptions('wallet-connect', {
projectId: '123',
metadata: {
name: 'test',
description: 'test',
url: 'test',
icons: ['test'],
},
}),
);

expect(wallet).toBeDefined();
});
});
});
56 changes: 56 additions & 0 deletions packages/dapp-kit/test/genesis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from 'vitest';
import { genesisBlocks } from '@vechain/connex/esm/config';
import { normalizeGenesisBlock, normalizeGenesisId } from '../src/genesis';

const customBlock = {
number: 0,
id: '0x00000000c05a20fbca2bf6ae3affba6af4a74b800b585bf7a4988aba7aea69f6',
size: 170,
parentID:
'0xffffffff00000000000000000000000000000000000000000000000000000000',
timestamp: 1526400000,
gasLimit: 10000000,
beneficiary: '0x0000000000000000000000000000000000000000',
gasUsed: 0,
totalScore: 0,
txsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
txsFeatures: 0,
stateRoot:
'0x93de0ffb1f33bc0af053abc2a87c4af44594f5dcb1cb879dd823686a15d68550',
receiptsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
signer: '0x0000000000000000000000000000000000000000',
isTrunk: true,
transactions: [],
};

describe('genesis', () => {
describe('normalizeGenesisId', () => {
it('normalizes `main`', () => {
expect(normalizeGenesisId('main')).toBe(genesisBlocks.main.id);
});

it('normalizes `test`', () => {
expect(normalizeGenesisId('test')).toBe(genesisBlocks.test.id);
});

it('normalizes a block', () => {
expect(normalizeGenesisId(customBlock)).toBe(customBlock.id);
});
});

describe('normalizeGenesisBlock', () => {
it('normalizes `main`', () => {
expect(normalizeGenesisBlock('main')).toBe(genesisBlocks.main);
});

it('normalizes `test`', () => {
expect(normalizeGenesisBlock('test')).toBe(genesisBlocks.test);
});

it('normalizes a block', () => {
expect(normalizeGenesisBlock(customBlock)).toBe(customBlock);
});
});
});
19 changes: 19 additions & 0 deletions packages/dapp-kit/test/helpers/connex-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { vi } from 'vitest';
import type { WalletConnectOptions } from '../../src';
import { MultiWalletConnex } from '../../src';

export const createUnitTestConnex = (
walletConnectOptions?: WalletConnectOptions,
): MultiWalletConnex => {
return new MultiWalletConnex({
nodeUrl: 'https://mainnet.vechain.org/',
walletConnectOptions,
customWcModal: {
openModal: vi.fn(),
closeModal: vi.fn(),
subscribeModal: () => {
return vi.fn();
},
},
});
};
Loading
Loading