Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
Updated usage to be in connect method
Fixed file names
  • Loading branch information
Alex Risch authored and Alex Risch committed May 30, 2024
1 parent c9607d2 commit ea02aa2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/services/coinbaseWallet.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 14 additions & 8 deletions src/services/randomWallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {mmkvStorage} from './mmkvStorage';
import {RandomWallet} from './RandomWallet';
import {RandomWallet} from './randomWallet';

// Mock mmkvStorage
jest.mock('./mmkvStorage', () => ({
Expand All @@ -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);
Expand All @@ -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',
);
Expand All @@ -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',
Expand Down
15 changes: 9 additions & 6 deletions src/services/randomWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ 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;
this.address = account.address;
mmkvStorage.saveAddress(this.address);
}

async connect() {
//
}

async disconnect() {
//
}
Expand All @@ -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;
Expand Down

0 comments on commit ea02aa2

Please sign in to comment.