Skip to content

Commit

Permalink
fix: test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Sep 21, 2024
1 parent afcd39e commit daf98f8
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 67 deletions.
57 changes: 44 additions & 13 deletions src/background/services/accounts/AccountsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.getAddresses).toHaveBeenNthCalledWith(
1,
0,
walletId
walletId,
networkService
);
expect(secretsService.getAddresses).toHaveBeenNthCalledWith(
2,
1,
walletId
walletId,
networkService
);
expect(secretsService.getImportedAddresses).toBeCalledTimes(3);

Expand Down Expand Up @@ -359,6 +361,7 @@ describe('background/services/accounts/AccountsService', () => {
});

it('correctly updates addresses for selected imported account', async () => {
const isMainnet = true;
jest
.mocked(secretsService.getImportedAddresses)
.mockImplementation((id) => {
Expand All @@ -375,7 +378,8 @@ describe('background/services/accounts/AccountsService', () => {
await accountsService.refreshAddressesForAccount('fb-acc');

expect(secretsService.getImportedAddresses).toHaveBeenCalledWith(
'fb-acc'
'fb-acc',
isMainnet
);
expect(secretsService.getAddresses).toHaveBeenCalledTimes(0);
expect(accountsService.getAccounts().imported['fb-acc']).toEqual({
Expand Down Expand Up @@ -494,7 +498,12 @@ describe('background/services/accounts/AccountsService', () => {
walletId,
});
expect(secretsService.addAddress).toBeCalledTimes(1);
expect(secretsService.addAddress).toBeCalledWith(0, WALLET_ID);
expect(secretsService.addAddress).toBeCalledWith({
index: 0,
walletId: WALLET_ID,
networkService,
ledgerService,
});

const accounts = accountsService.getAccounts();
expect(accounts).toStrictEqual({
Expand Down Expand Up @@ -541,7 +550,12 @@ describe('background/services/accounts/AccountsService', () => {

await accountsService.addPrimaryAccount({ walletId: WALLET_ID });
expect(secretsService.addAddress).toBeCalledTimes(1);
expect(secretsService.addAddress).toBeCalledWith(2, WALLET_ID);
expect(secretsService.addAddress).toBeCalledWith({
index: 2,
walletId: WALLET_ID,
networkService,
ledgerService,
});
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
expect(permissionsService.addWhitelistDomains).toBeCalledWith(
'0x000000000'
Expand Down Expand Up @@ -577,7 +591,12 @@ describe('background/services/accounts/AccountsService', () => {
walletId: WALLET_ID,
});
expect(secretsService.addAddress).toBeCalledTimes(1);
expect(secretsService.addAddress).toBeCalledWith(2, WALLET_ID);
expect(secretsService.addAddress).toBeCalledWith({
index: 2,
walletId: WALLET_ID,
networkService,
ledgerService,
});
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
expect(permissionsService.addWhitelistDomains).toBeCalledWith(
'0x000000000'
Expand Down Expand Up @@ -639,6 +658,7 @@ describe('background/services/accounts/AccountsService', () => {
const commitMock = jest.fn();

it('adds account to the imported list correctly', async () => {
const isMainnet = true;
const options: ImportData = {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
Expand All @@ -662,7 +682,10 @@ describe('background/services/accounts/AccountsService', () => {
options,
});
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(options);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
isMainnet
);
expect(commitMock).toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
expect(permissionsService.addWhitelistDomains).toBeCalledWith(
Expand Down Expand Up @@ -695,6 +718,7 @@ describe('background/services/accounts/AccountsService', () => {
});

it('sets default name when no name is given', async () => {
const isMainnet = true;
const options: ImportData = {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
Expand All @@ -717,7 +741,10 @@ describe('background/services/accounts/AccountsService', () => {

await accountsService.addImportedAccount({ options });
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(options);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
isMainnet
);
expect(commitMock).toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
expect(permissionsService.addWhitelistDomains).toBeCalledWith(
Expand Down Expand Up @@ -791,6 +818,7 @@ describe('background/services/accounts/AccountsService', () => {
});

it('returns the existing account id on duplicated accounts imports', async () => {
const isMainnet = true;
const options: ImportData = {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
Expand All @@ -815,7 +843,10 @@ describe('background/services/accounts/AccountsService', () => {
'0x1'
);
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(options);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
isMainnet
);
expect(commitMock).not.toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -1009,10 +1040,10 @@ describe('background/services/accounts/AccountsService', () => {
expect(result).toStrictEqual(expectedAccounts);
expect(eventListener).toHaveBeenCalledTimes(1);
expect(eventListener).toHaveBeenCalledWith(expectedAccounts);
expect(secretsService.deleteImportedWallets).toHaveBeenCalledWith([
'0x1',
'0x2',
]);
expect(secretsService.deleteImportedWallets).toHaveBeenCalledWith(
['0x1', '0x2'],
walletConnectService
);
});

it('changes the active account if deleted', async () => {
Expand Down
14 changes: 4 additions & 10 deletions src/background/services/fireblocks/FireblocksService.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sha256 } from 'ethers';
import { PeerType } from 'fireblocks-sdk';
import { AccountType, FireblocksAccount } from '../accounts/models';
import { Account, AccountType, FireblocksAccount } from '../accounts/models';
import { SecretType } from '../secrets/models';
import { SecretsService } from '../secrets/SecretsService';
import { FireblocksSecretsService } from './FireblocksSecretsService';
Expand Down Expand Up @@ -60,15 +60,9 @@ const mockResponsesByPath =
};

describe('src/background/services/fireblocks/FireblocksService', () => {
const accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);
const accountsService: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;
const secretsService = jest.mocked(new SecretsService({} as any));
const secretsProvider = new FireblocksSecretsService(
secretsService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { LedgerTransport } from '../LedgerTransport';
import { MigrateMissingPublicKeysFromLedgerHandler } from './migrateMissingPublicKeysFromLedger';
import { buildRpcCall } from '@src/tests/test-utils';
import { AccountsService } from '../../accounts/AccountsService';
import { Account } from '../../accounts/models';

jest.mock('../../secrets/SecretsService');
jest.mock('@avalabs/core-wallets-sdk');
Expand All @@ -20,15 +21,9 @@ describe('src/background/services/ledger/handlers/migrateMissingPublicKeysFromLe
id: '123',
method: ExtensionRequest.LEDGER_MIGRATE_MISSING_PUBKEYS,
} as any;
const accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);
const accountsService: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;

const secretsService = jest.mocked(new SecretsService({} as any));
const ledgerService = {} as any;
Expand Down
3 changes: 2 additions & 1 deletion src/background/services/secrets/SecretsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { WalletConnectService } from '../walletConnect/WalletConnectService';

jest.mock('../storage/StorageService');
jest.mock('../network/NetworkService');
jest.mock('../walletConnect/WalletConnectService');
jest.mock('@avalabs/core-wallets-sdk');
jest.mock('tsyringe', () => {
const tsyringe = jest.requireActual('tsyringe');
Expand Down Expand Up @@ -477,7 +478,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
});
});

it('attaches the account object to the result', async () => {
it.only('attaches the account object to the result', async () => {
mockMnemonicWallet();
const result = await secretsService.getActiveAccountSecrets(
activeAccountData
Expand Down
1 change: 0 additions & 1 deletion src/background/services/secrets/SecretsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ export class SecretsService {
const walletKeys = await this.storageService.load<WalletSecretInStorage>(
WALLET_STORAGE_KEY
);
console.log('walletKeys: ', walletKeys);

if (!walletKeys && strict) {
throw new Error('Wallet is not initialized');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { SecretsService } from '../../secrets/SecretsService';
import { SecretType } from '../../secrets/models';
import { buildRpcCall } from '@src/tests/test-utils';
import { AccountsService } from '../../accounts/AccountsService';
import { Account } from '../../accounts/models';

describe('src/background/services/seedless/handlers/updateSignerToken', () => {
let secretsService;
let accountsService;

const accountsService: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;

beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -18,15 +22,6 @@ describe('src/background/services/seedless/handlers/updateSignerToken', () => {
}),
updateSecrets: jest.fn().mockResolvedValue('walletId'),
} as any);
accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);
});

it('returns error when token is missing', async () => {
Expand Down
14 changes: 4 additions & 10 deletions src/background/services/wallet/WalletService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ describe('background/services/wallet/WalletService.ts', () => {
let walletConnectService: WalletConnectService;
let fireblocksService: FireblocksService;
let secretsService: jest.Mocked<SecretsService>;
let accountsService: AccountsService;
const accountsService: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;

const privateKeyMock =
'4ae3e293d0161fa90bfbf51028ceb1e51fe70bc6167afe4e0fe0927d86555503';
Expand Down Expand Up @@ -243,15 +245,7 @@ describe('background/services/wallet/WalletService.ts', () => {
networkService = new NetworkService({} as any, {} as any);
ledgerService = new LedgerService();
keystoneService = new KeystoneService();
accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);

walletConnectService = new WalletConnectService(
new WalletConnectStorage({} as any)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SecretType } from '../../secrets/models';
import { SecretsService } from '../../secrets/SecretsService';
import { GetUnencryptedMnemonicHandler } from './getUnencryptedMnemonic';
import { AccountsService } from '../../accounts/AccountsService';
import { Account } from '../../accounts/models';

describe('src/background/services/wallet/handlers/getUnencryptedMnemonic.ts', () => {
const lockService: jest.Mocked<LockService> = {
Expand All @@ -12,15 +13,10 @@ describe('src/background/services/wallet/handlers/getUnencryptedMnemonic.ts', ()
const secretsService: jest.Mocked<SecretsService> = {
getActiveAccountSecrets: jest.fn(),
} as any;
const accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);

const accountsService: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;

const buildHandler = () =>
new GetUnencryptedMnemonicHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@avalabs/core-wallets-sdk';
import { ExtensionRequest } from '@src/background/connections/extensionConnection/models';
import { networks } from 'bitcoinjs-lib';
import { AccountType } from '../../accounts/models';
import { Account, AccountType } from '../../accounts/models';
import { AccountWithSecrets, SecretType } from '../../secrets/models';
import { SecretsService } from '../../secrets/SecretsService';
import { StoreBtcWalletPolicyDetails } from './storeBtcWalletPolicyDetails';
Expand All @@ -29,7 +29,9 @@ describe('src/background/services/wallet/handlers/storeBtcWalletPolicyDetails.ts
isMainnet: () => false,
} as any;

const accountsServiceMock = jest.mocked({} as unknown as AccountsService);
const accountsServiceMock: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;

beforeEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -155,7 +157,8 @@ describe('src/background/services/wallet/handlers/storeBtcWalletPolicyDetails.ts
'masterFingerprint',
'hmacHex',
'name',
'wallet-id'
'wallet-id',
{}
);

expect(result).toStrictEqual({
Expand Down Expand Up @@ -199,7 +202,8 @@ describe('src/background/services/wallet/handlers/storeBtcWalletPolicyDetails.ts
'masterFingerprint',
'hmacHex',
'name',
'wallet-id'
'wallet-id',
{}
);

expect(result).toStrictEqual({
Expand Down

0 comments on commit daf98f8

Please sign in to comment.