Skip to content

Commit

Permalink
feat: add btc balance mock call
Browse files Browse the repository at this point in the history
  • Loading branch information
montelaidev committed Jul 24, 2024
1 parent d4c2256 commit f7bef90
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 119 deletions.
11 changes: 7 additions & 4 deletions test/e2e/accounts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ export async function createBtcAccount(driver: Driver) {
text: messages.addNewBitcoinAccount.message,
tag: 'button',
});
await driver.clickElementAndWaitToDisappear({
text: 'Create',
tag: 'button',
});
await driver.clickElementAndWaitToDisappear(
{
text: 'Create',
tag: 'button',
},
5000,
);
}
3 changes: 3 additions & 0 deletions test/e2e/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ export const VERIFYING_PAYMASTER = '0xbdbDEc38ed168331b1F7004cc9e5392A2272C1D7';

/* Default ganache ETH balance in decimal when first login */
export const DEFAULT_GANACHE_ETH_BALANCE_DEC = '25';

/* Default BTC address created using test SRP */
export const DEFAULT_BTC_ACCOUNT = 'bc1qg6whd6pc0cguh6gpp3ewujm53hv32ta9hdp252';
25 changes: 4 additions & 21 deletions test/e2e/flask/btc/btc-account-overview.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import { strict as assert } from 'assert';
import { Suite } from 'mocha';
import {
withFixtures,
defaultGanacheOptions,
unlockWallet,
} from '../../helpers';
import { Driver } from '../../webdriver/driver';
import FixtureBuilder from '../../fixture-builder';
import { createBtcAccount } from '../../accounts/common';
import { withBtcAccountSnap } from './common-btc';

describe('BTC Account - Overview', function (this: Suite) {
it('has portfolio button enabled for BTC accounts', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await driver.findElement({
css: '[data-testid="account-menu-icon"]',
text: 'Bitcoin Account',
Expand Down
29 changes: 5 additions & 24 deletions test/e2e/flask/btc/btc-dapp-connection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
import { strict as assert } from 'assert';
import { Suite } from 'mocha';
import {
withFixtures,
defaultGanacheOptions,
unlockWallet,
openDapp,
WINDOW_TITLES,
} from '../../helpers';
import { Driver } from '../../webdriver/driver';
import FixtureBuilder from '../../fixture-builder';
import { createBtcAccount } from '../../accounts/common';
import { openDapp, WINDOW_TITLES } from '../../helpers';
import { withBtcAccountSnap } from './common-btc';

describe('BTC Account - Dapp Connection', function (this: Suite) {
it('cannot connect to dapps', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await openDapp(driver);
await driver.clickElement('#connectButton');
await driver.waitUntilXWindowHandles(3);
Expand Down
59 changes: 59 additions & 0 deletions test/e2e/flask/btc/common-btc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import FixtureBuilder from '../../fixture-builder';
import { withFixtures, unlockWallet } from '../../helpers';
import { Mockttp } from 'mockttp';
import { DEFAULT_BTC_ACCOUNT } from '../../constants';
import { Driver } from '../../webdriver/driver';
import { createBtcAccount } from '../../accounts/common';

const GENERATE_MOCK_BTC_BALANCE_CALL = (
address: string = DEFAULT_BTC_ACCOUNT,
): { data: { [address: string]: number } } => {
return {
data: {
[address]: 9999,
},
};
};

export async function mockBtcBalanceQuote(
mockServer: Mockttp,
address: string = DEFAULT_BTC_ACCOUNT,
) {
return [
await mockServer
.forGet(/https:\/\/api\.blockchair\.com\/bitcoin\/addresses\/balances/u)
.withQuery({
addresses: address,
})
.thenCallback(() => ({
statusCode: 200,
json: GENERATE_MOCK_BTC_BALANCE_CALL(address),
})),
];
}

export async function withBtcAccountSnap(
{
title,
bitcoinSupportEnabled,
}: { title?: string; bitcoinSupportEnabled?: boolean },
test: (driver: Driver) => Promise<void>,
) {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: bitcoinSupportEnabled ?? true,
})
.build(),
title,
dapp: true,
testSpecificMock: mockBtcBalanceQuote,
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await test(driver);
},
);
}
86 changes: 16 additions & 70 deletions test/e2e/flask/btc/create-btc-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,22 @@ import { strict as assert } from 'assert';
import { Suite } from 'mocha';
import messages from '../../../../app/_locales/en/messages.json';

import FixtureBuilder from '../../fixture-builder';
import {
WALLET_PASSWORD,
completeSRPRevealQuiz,
defaultGanacheOptions,
getSelectedAccountAddress,
openSRPRevealQuiz,
removeSelectedAccount,
tapAndHoldToRevealSRP,
unlockWallet,
withFixtures,
} from '../../helpers';
import { Driver } from '../../webdriver/driver';
import { createBtcAccount } from '../../accounts/common';
import { withBtcAccountSnap } from './common-btc';

describe('Create BTC Account', function (this: Suite) {
it('create BTC account from the menu', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await driver.findElement({
css: '[data-testid="account-menu-icon"]',
text: 'Bitcoin Account',
Expand All @@ -41,19 +27,9 @@ describe('Create BTC Account', function (this: Suite) {
});

it('cannot create multiple BTC accounts', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await driver.delay(500);
await driver.clickElement('[data-testid="account-menu-icon"]');
await driver.clickElement(
Expand All @@ -80,19 +56,9 @@ describe('Create BTC Account', function (this: Suite) {
});

it('can cancel the removal of BTC account', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await driver.findElement({
css: '[data-testid="account-menu-icon"]',
text: 'Bitcoin Account',
Expand Down Expand Up @@ -121,19 +87,9 @@ describe('Create BTC Account', function (this: Suite) {
});

it('can recreate BTC account after deleting it', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await driver.findElement({
css: '[data-testid="account-menu-icon"]',
text: 'Bitcoin Account',
Expand All @@ -156,19 +112,9 @@ describe('Create BTC Account', function (this: Suite) {
});

it('can recreate BTC account after restoring wallet with SRP', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withPreferencesControllerAndFeatureFlag({
bitcoinSupportEnabled: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await unlockWallet(driver);
await createBtcAccount(driver);
await withBtcAccountSnap(
{ title: this.test?.fullTitle() },
async (driver) => {
await driver.findElement({
css: '[data-testid="account-menu-icon"]',
text: 'Bitcoin Account',
Expand Down

0 comments on commit f7bef90

Please sign in to comment.