From ea02aa26dac502e63cad901cae51baceaad64f9e Mon Sep 17 00:00:00 2001 From: Alex Risch Date: Thu, 30 May 2024 14:32:13 -0600 Subject: [PATCH] fixed tests Updated usage to be in connect method Fixed file names --- src/services/coinbaseWallet.test.ts | 2 +- src/services/randomWallet.test.ts | 22 ++++++++++++++-------- src/services/randomWallet.ts | 15 +++++++++------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/services/coinbaseWallet.test.ts b/src/services/coinbaseWallet.test.ts index d150997..f755a3b 100644 --- a/src/services/coinbaseWallet.test.ts +++ b/src/services/coinbaseWallet.test.ts @@ -1,6 +1,6 @@ import {configure, isConnected} from '@coinbase/wallet-mobile-sdk'; import {Alert} from 'react-native'; -import {CoinbaseWallet} from './CoinbaseWallet'; +import {CoinbaseWallet} from './coinbaseWallet'; import {mmkvStorage} from './mmkvStorage'; // Mock mmkvStorage diff --git a/src/services/randomWallet.test.ts b/src/services/randomWallet.test.ts index 7627462..008b28c 100644 --- a/src/services/randomWallet.test.ts +++ b/src/services/randomWallet.test.ts @@ -1,5 +1,5 @@ import {mmkvStorage} from './mmkvStorage'; -import {RandomWallet} from './RandomWallet'; +import {RandomWallet} from './randomWallet'; // Mock mmkvStorage jest.mock('./mmkvStorage', () => ({ @@ -19,9 +19,10 @@ describe('RandomWallet', () => { jest.clearAllMocks(); }); - test('should generate a private key and set account and address', () => { + test('should generate a private key and set account and address', async () => { + await randomWallet.connect(); expect(randomWallet.account).toBeDefined(); - expect(randomWallet.address).toBe(randomWallet.account.address); + expect(randomWallet.address).toBe(randomWallet.account?.address); // @ts-ignore-next-line mmkvStorage.saveAddress.mockReturnValue(); expect(mmkvStorage.saveAddress).toHaveBeenCalledWith(randomWallet.address); @@ -33,17 +34,21 @@ describe('RandomWallet', () => { }); test('isConnected should return true when address is set', async () => { + await randomWallet.connect(); const result = await randomWallet.isConnected(); expect(result).toBe(true); }); test('getAddress should return the address if set', async () => { const address = await randomWallet.getAddress(); - expect(address).toBe(randomWallet.account.address); + expect(address).toBe(randomWallet.account?.address); }); test('getAddress should throw an error if address is not set', async () => { - randomWallet.address = undefined; + randomWallet.account = { + // @ts-ignore-next-line + address: undefined, + }; await expect(randomWallet.getAddress()).rejects.toThrow( 'Failed to get address', ); @@ -52,19 +57,20 @@ describe('RandomWallet', () => { test('signMessage should return a signed message', async () => { const message = 'Test message'; const signedMessage = 'signed_message'; // Mocked signed message - + await randomWallet.connect(); + // @ts-ignore-next-line randomWallet.account.signMessage = jest .fn() .mockResolvedValue(signedMessage); const result = await randomWallet.signMessage(message); expect(result).toBe(signedMessage); + // @ts-ignore-next-line expect(randomWallet.account.signMessage).toHaveBeenCalledWith({message}); }); test('signMessage should throw an error if account does not have signMessage function', async () => { - // @ts-expect-error - randomWallet.account.signMessage = undefined; + randomWallet.account = undefined; await expect(randomWallet.signMessage('Test message')).rejects.toThrow( 'Failed to sign message', diff --git a/src/services/randomWallet.ts b/src/services/randomWallet.ts index acf3d79..2356714 100644 --- a/src/services/randomWallet.ts +++ b/src/services/randomWallet.ts @@ -9,10 +9,14 @@ import {mmkvStorage} from './mmkvStorage'; export class RandomWallet extends WalletConnection { address?: string; - account: PrivateKeyAccount; + account?: PrivateKeyAccount; constructor() { super(); + } + + async connect() { + // const privateKey = generatePrivateKey(); const account = privateKeyToAccount(privateKey); this.account = account; @@ -20,10 +24,6 @@ export class RandomWallet extends WalletConnection { mmkvStorage.saveAddress(this.address); } - async connect() { - // - } - async disconnect() { // } @@ -37,7 +37,10 @@ export class RandomWallet extends WalletConnection { } async getAddress() { - if (!this.address) { + if (!this.account) { + await this.connect(); + } + if (!this.account?.address) { throw new Error('Failed to get address'); } return this.account.address;