Skip to content

Commit

Permalink
test: disconnect BeaconWallet in tests, change jest setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ac10n committed Jul 23, 2024
1 parent 41612b1 commit 518caec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/taquito-beacon-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"node": ">=18"
},
"scripts": {
"test": "jest --coverage --forceExit",
"test": "jest --coverage --forceExit --detectOpenHandles --openHandlesTimeout=5000",
"test:watch": "jest --coverage --watch",
"test:prod": "npm run lint && npm run test -- --no-cache",
"lint": "eslint --ext .js,.ts .",
Expand Down
26 changes: 23 additions & 3 deletions packages/taquito-beacon-wallet/test/taquito-beacon-wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ global.window = { addEventListener: jest.fn() } as any;

describe('Beacon Wallet tests', () => {
it('Verify that BeaconWallet is instantiable', () => {
expect(new BeaconWallet({ name: 'testWallet' })).toBeInstanceOf(BeaconWallet);
const wallet = new BeaconWallet({ name: 'testWallet' });
expect(wallet).toBeInstanceOf(BeaconWallet);
wallet.disconnect();
});

it('Verify BeaconWallet not initialized error', () => {
Expand All @@ -24,52 +26,61 @@ describe('Beacon Wallet tests', () => {
});

it('Verify that permissions must be called before getPKH', async () => {
const wallet = new BeaconWallet({ name: 'testWallet' });
try {
const wallet = new BeaconWallet({ name: 'testWallet' });
await wallet.getPKH();
} catch (error: any) {
expect(error.message).toContain('BeaconWallet needs to be initialized');
} finally {
wallet.disconnect();
}
});

it(`Verify that a Beacon Wallet has a beacon ID`, async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
expect(typeof (await wallet.client.beaconId)).toEqual('string');
expect(await wallet.client.beaconId).toBeDefined;
wallet.disconnect();
});

it(`Verify that an error is thrown if BeaconWallet is initialized with an empty object`, async () => {
const wallet = new BeaconWallet({} as any);
try {
const wallet = new BeaconWallet({} as any);
expect(wallet).toBeDefined();
} catch (e) {
console.log('hello there', e);
expect((e as any).message).toEqual('Name not set');
} finally {
wallet.disconnect();
}
});

it(`Verify formatParameters for fees`, async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const formattedParam = await wallet.formatParameters({ fee: 10 });
expect(formattedParam.fee).toEqual('10');
wallet.disconnect();
});

it(`Verify formatParameters for storageLimit`, async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const formattedParam = await wallet.formatParameters({ storageLimit: 2000 });
expect(formattedParam.storageLimit).toEqual('2000');
wallet.disconnect();
});

it(`Verify formatParameters for gasLimit`, async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const formattedParam = await wallet.formatParameters({ gasLimit: 40 });
expect(formattedParam.gasLimit).toEqual('40');
wallet.disconnect();
});

it(`Verify removeDefaultParameters for fees`, async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const formattedParam = await wallet.removeDefaultParams({ fee: 10 }, { fee: 30 });
expect(formattedParam.fee).toEqual(30);
wallet.disconnect();
});

it(`Verify removeDefaultParameters for storageLimit`, async () => {
Expand All @@ -79,53 +90,62 @@ describe('Beacon Wallet tests', () => {
{ storageLimit: 165 }
);
expect(formattedParam.storageLimit).toEqual(165);
wallet.disconnect();
});

it(`Verify removeDefaultParameters for gas limit`, async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const formattedParam = await wallet.removeDefaultParams({ gasLimit: 40 }, { gasLimit: 80 });
expect(formattedParam.gasLimit).toEqual(80);
wallet.disconnect();
});

it('Verify getSigningType returns correct signing type for undefined', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const signingType = wallet['getSigningType'](undefined);
expect(signingType).toBe(SigningType.RAW);
wallet.disconnect();
});

it('Verify getSigningType returns correct signing type for an empty array', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const signingType = wallet['getSigningType'](new Uint8Array([]));
expect(signingType).toBe(SigningType.RAW);
wallet.disconnect();
});

it('Verify getSigningType returns correct signing type for 3', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const signingType = wallet['getSigningType'](new Uint8Array([3]));
expect(signingType).toBe(SigningType.OPERATION);
wallet.disconnect();
});

it('Verify getSigningType returns correct signing type for 5', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
const signingType = wallet['getSigningType'](new Uint8Array([5]));
expect(signingType).toBe(SigningType.MICHELINE);
wallet.disconnect();
});

it('Verify getSigningType throws for invalid inputs', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
expect(() => wallet['getSigningType'](new Uint8Array([5, 3]))).toThrow();
expect(() => wallet['getSigningType'](new Uint8Array([7]))).toThrow();
wallet.disconnect();
});

it('Verify sign throws for Micheline', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
expect(
async () => await wallet.sign('48656C6C6F20576F726C64', new Uint8Array([5]))
).rejects.toThrow();
wallet.disconnect();
});

it('Verify sign throws for Raw', async () => {
const wallet = new BeaconWallet({ name: 'Test', storage: new LocalStorage() });
expect(async () => await wallet.sign('48656C6C6F20576F726C64')).rejects.toThrow();
wallet.disconnect();
});
});

0 comments on commit 518caec

Please sign in to comment.