Skip to content

Commit

Permalink
chore: cleanup + test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Oct 31, 2024
1 parent 8e3253a commit 4e3de58
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 73 deletions.
12 changes: 4 additions & 8 deletions src/background/services/accounts/AccountsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ 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 @@ -379,7 +378,7 @@ describe('background/services/accounts/AccountsService', () => {

expect(secretsService.getImportedAddresses).toHaveBeenCalledWith(
'fb-acc',
isMainnet
networkService
);
expect(secretsService.getAddresses).toHaveBeenCalledTimes(0);
expect(accountsService.getAccounts().imported['fb-acc']).toEqual({
Expand Down Expand Up @@ -658,7 +657,6 @@ 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 @@ -684,7 +682,7 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
isMainnet
networkService
);
expect(commitMock).toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
Expand Down Expand Up @@ -718,7 +716,6 @@ 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 @@ -743,7 +740,7 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
isMainnet
networkService
);
expect(commitMock).toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
Expand Down Expand Up @@ -818,7 +815,6 @@ 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 @@ -845,7 +841,7 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
isMainnet
networkService
);
expect(commitMock).not.toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).not.toHaveBeenCalled();
Expand Down
6 changes: 2 additions & 4 deletions src/background/services/networkFee/NetworkFeeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('src/background/services/networkFee/NetworkFeeService', () => {
low: { maxFeePerGas: 3n },
medium: { maxFeePerGas: 4n },
high: { maxFeePerGas: 5n },
displayDecimals: 9,
};
const getNetworkFee = jest.fn().mockResolvedValue(networkFees);

Expand All @@ -84,10 +85,7 @@ describe('src/background/services/networkFee/NetworkFeeService', () => {

expect(moduleManager.loadModuleByNetwork).toHaveBeenCalledWith(evm);

expect(fees).toEqual({
displayDecimals: 9,
...networkFees,
});
expect(fees).toEqual(networkFees);
});
});
});
24 changes: 14 additions & 10 deletions src/background/services/secrets/SecretsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,6 @@ describe('src/background/services/secrets/SecretsService.ts', () => {

describe('addImportedWallet', () => {
const pubKeyBuffer = Buffer.from('0x111', 'hex');
const isMainnet = false;

beforeEach(() => {
(networkService.isMainnet as jest.Mock).mockReturnValue(false);
Expand All @@ -1532,7 +1531,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
},
isMainnet
networkService
);

expect(result).toStrictEqual({
Expand Down Expand Up @@ -1571,7 +1570,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
},
isMainnet
networkService
)
).rejects.toThrow('Error while calculating addresses');
});
Expand All @@ -1591,7 +1590,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
},
isMainnet
networkService
)
).rejects.toThrow('Error while calculating addresses');
});
Expand All @@ -1611,7 +1610,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
importType: ImportType.PRIVATE_KEY,
data: 'privateKey',
},
isMainnet
networkService
)
).rejects.toThrow('Error while calculating addresses');
});
Expand All @@ -1621,7 +1620,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
const pubKeyBuffer = Buffer.from('0x111', 'hex');

beforeEach(() => {
(networkService.isMainnet as jest.Mock).mockReturnValue(false);
(networkService.isMainnet as jest.Mock).mockReturnValue(true);
(getPublicKeyFromPrivateKey as jest.Mock).mockReturnValue(pubKeyBuffer);
(getEvmAddressFromPubKey as jest.Mock).mockReturnValue('0x1');
(getBtcAddressFromPubKey as jest.Mock).mockReturnValue('0x2');
Expand All @@ -1634,7 +1633,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
);

await expect(
secretsService.getImportedAddresses('id', true)
secretsService.getImportedAddresses('id', networkService)
).rejects.toThrow('No secrets found for imported account');
});

Expand All @@ -1645,11 +1644,12 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
);

await expect(
secretsService.getImportedAddresses('id', true)
secretsService.getImportedAddresses('id', networkService)
).rejects.toThrow('Unsupported import type');
});

it('throws if addresses are missing', async () => {
(networkService.isMainnet as jest.Mock).mockReturnValue(false);
secretsService.getImportedAccountSecrets = jest.fn();
(secretsService.getImportedAccountSecrets as jest.Mock).mockResolvedValue(
{ secretType: SecretType.PrivateKey, secret: 'secret' }
Expand All @@ -1658,17 +1658,21 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
(getBtcAddressFromPubKey as jest.Mock).mockReturnValueOnce('');

await expect(
secretsService.getImportedAddresses('id', false)
secretsService.getImportedAddresses('id', networkService)
).rejects.toThrow('Missing address');
});

it('returns the addresses for PRIVATE_KEY correctly', async () => {
(networkService.isMainnet as jest.Mock).mockReturnValue(false);
secretsService.getImportedAccountSecrets = jest.fn();
(secretsService.getImportedAccountSecrets as jest.Mock).mockResolvedValue(
{ secretType: SecretType.PrivateKey, secret: 'secret' }
);

const result = await secretsService.getImportedAddresses('id', false);
const result = await secretsService.getImportedAddresses(
'id',
networkService
);

expect(result).toStrictEqual({
addressBTC: '0x2',
Expand Down
4 changes: 2 additions & 2 deletions src/background/services/seedless/SeedlessWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ describe('src/background/services/seedless/SeedlessWallet', () => {
mockPsbt();
jest
.mocked(getProviderForNetwork)
.mockReturnValue(new BitcoinProvider());
.mockResolvedValue(new BitcoinProvider());
wallet = new SeedlessWallet({
networkService,
sessionStorage,
Expand Down Expand Up @@ -1000,7 +1000,7 @@ describe('src/background/services/seedless/SeedlessWallet', () => {
jest
.spyOn(bitcoinProvider, 'getNetwork')
.mockReturnValue(networks.bitcoin);
jest.mocked(getProviderForNetwork).mockReturnValue(bitcoinProvider);
jest.mocked(getProviderForNetwork).mockResolvedValue(bitcoinProvider);
wallet = new SeedlessWallet({
networkService,
sessionStorage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ describe('src/background/services/wallet/handlers/avalanche_sendTransaction.ts',
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down Expand Up @@ -415,6 +416,7 @@ describe('src/background/services/wallet/handlers/avalanche_sendTransaction.ts',
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import getProvidedUtxos from '../utils/getProvidedUtxos';
import { openApprovalWindow } from '@src/background/runtime/openApprovalWindow';
import { buildRpcCall } from '@src/tests/test-utils';
import { HEADERS } from '../../glacier/glacierConfig';
import { AVALANCHE_XP_TEST_NETWORK } from '@avalabs/core-chains-sdk';

jest.mock('@avalabs/avalanchejs');
jest.mock('@avalabs/core-wallets-sdk');
Expand Down Expand Up @@ -93,6 +94,9 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
(Avalanche.getVmByChainAlias as jest.Mock).mockReturnValue(AVM);
(utils.hexToBuffer as jest.Mock).mockReturnValue(txBytes);
networkServiceMock.getAvalanceProviderXP.mockReturnValue(providerMock);
networkServiceMock.getAvalancheNetworkXP.mockReturnValueOnce(
AVALANCHE_XP_TEST_NETWORK
);
(utils.unpackWithManager as jest.Mock).mockReturnValue(txMock);
(utils.addressesFromBytes as jest.Mock).mockReturnValue([
signerAddressMock,
Expand Down Expand Up @@ -225,6 +229,7 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down Expand Up @@ -273,6 +278,7 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down Expand Up @@ -328,6 +334,7 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down Expand Up @@ -385,6 +392,7 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down Expand Up @@ -527,14 +535,15 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
});
});
});

describe('(partially) signex tx', () => {
describe('(partially) signed tx', () => {
const checkExpected = (req, result, signaturesMock) => {
expect(result).toEqual({
...req,
Expand Down Expand Up @@ -716,6 +725,7 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
transactionHex: request.params.transactionHex,
chainAlias: request.params.chainAlias,
isTestnet: true,
isDevnet: false,
url: process.env.GLACIER_URL,
token: process.env.GLACIER_API_KEY,
headers: HEADERS,
Expand Down Expand Up @@ -780,9 +790,6 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
(Avalanche.signedTxToHex as jest.Mock).mockReturnValueOnce(
signedTransactionHex
);
networkServiceMock.getAvalancheNetworkXP.mockReturnValueOnce(
providerMock
);

const handler = new AvalancheSignTransactionHandler(
walletServiceMock as any,
Expand Down Expand Up @@ -837,10 +844,6 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
(Avalanche.signedTxToHex as jest.Mock).mockReturnValueOnce(
signedTransactionHex
);
networkServiceMock.getAvalancheNetworkXP.mockReturnValueOnce(
providerMock
);

const handler = new AvalancheSignTransactionHandler(
walletServiceMock as any,
networkServiceMock as any,
Expand Down Expand Up @@ -874,9 +877,6 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
(Avalanche.signedTxToHex as jest.Mock).mockReturnValueOnce(
signedTransactionHex
);
networkServiceMock.getAvalancheNetworkXP.mockReturnValueOnce(
providerMock
);

const handler = new AvalancheSignTransactionHandler(
walletServiceMock as any,
Expand Down Expand Up @@ -913,7 +913,7 @@ describe('src/background/services/wallet/handlers/avalanche_signTransaction', ()
{
tx: mockedTx,
},
providerMock,
AVALANCHE_XP_TEST_NETWORK,
frontendTabId
);
expect(signedTxMock.getCredentials).toHaveBeenCalled();
Expand Down
8 changes: 4 additions & 4 deletions src/contexts/BridgeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('contexts/BridgeProvider', () => {
symbol: 'AVAX',
},
},
avalancheProvider: {
avaxProviderC: {
waitForTransaction: jest.fn(),
},
ethereumProvider: {
Expand Down Expand Up @@ -249,7 +249,7 @@ describe('contexts/BridgeProvider', () => {
amount,
asset: currentAssetData,
account: accountsContext.accounts.active.addressC,
avalancheProvider: networkContext.avalancheProvider,
avalancheProvider: networkContext.avaxProviderC,
ethereumProvider: networkContext.ethereumProvider,
config: bridgeSDKContext.bridgeConfig.config,
onStatusChange: expect.any(Function),
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('contexts/BridgeProvider', () => {
jest.mocked(useNetworkContext).mockReturnValue({
...networkContext,
ethereumProvider: undefined,
avalancheProviderC: undefined,
avaxProviderC: undefined,
});
});

Expand Down Expand Up @@ -337,7 +337,7 @@ describe('contexts/BridgeProvider', () => {
asset,
{
ethereum: networkContext.ethereumProvider,
avalanche: networkContext.avalancheProvider,
avalanche: networkContext.avaxProviderC,
},
bridgeSDKContext.bridgeConfig.config,
bridgeSDKContext.currentBlockchain
Expand Down
4 changes: 2 additions & 2 deletions src/contexts/SwapProvider/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('contexts/SwapProvider', () => {
symbol: 'AVAX',
},
},
avalancheProvider: {
avaxProviderC: {
waitForTransaction: jest.fn(),
},
} as any;
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('contexts/SwapProvider', () => {
} as SwapParams);

beforeEach(() => {
networkContext.avalancheProvider.waitForTransaction.mockResolvedValue({
networkContext.avaxProviderC.waitForTransaction.mockResolvedValue({
status: 1,
});
});
Expand Down
Loading

0 comments on commit 4e3de58

Please sign in to comment.