Skip to content

Commit

Permalink
Merge branch 'develop' into reapply-withkeyring-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito authored Sep 16, 2024
2 parents c483c52 + aeb91c9 commit d5005d0
Show file tree
Hide file tree
Showing 38 changed files with 391 additions and 327 deletions.
6 changes: 6 additions & 0 deletions app/scripts/lib/setupSentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ function setSentryClient() {
*/
globalThis.nw = {};

/**
* Sentry checks session tracking support by looking for global history object and functions inside it.
* Scuttling sets this property to undefined which breaks Sentry logic and crashes background.
*/
globalThis.history ??= {};

log('Updating client', {
environment,
dsn,
Expand Down
14 changes: 6 additions & 8 deletions development/build/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,16 @@ const scuttlingConfigBase = {
extra: '',
stateHooks: '',
nw: '',
// Sentry Auto Session Tracking
document: '',
history: '',
isNaN: '',
parseInt: '',
},
};

const mv3ScuttlingConfig = { ...scuttlingConfigBase };

const standardScuttlingConfig = {
...scuttlingConfigBase,
'scripts/sentry-install.js': {
...scuttlingConfigBase['scripts/sentry-install.js'],
document: '',
},
};
const standardScuttlingConfig = { ...scuttlingConfigBase };

const noopWriteStream = through.obj((_file, _fileEncoding, callback) =>
callback(),
Expand Down
2 changes: 2 additions & 0 deletions shared/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export const CHAIN_IDS = {
SEI: '0x531',
BERACHAIN: '0x138d5',
METACHAIN_ONE: '0x1b6e6',
ARBITRUM_SEPOLIA: '0x66eee',
NEAR: '0x18d',
NEAR_TESTNET: '0x18e',
} as const;
Expand Down Expand Up @@ -1034,4 +1035,5 @@ export const TEST_NETWORK_IDS = [
CHAIN_IDS.SEPOLIA,
CHAIN_IDS.LINEA_GOERLI,
CHAIN_IDS.LINEA_SEPOLIA,
CHAIN_IDS.ARBITRUM_SEPOLIA,
];
12 changes: 12 additions & 0 deletions test/e2e/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ class FixtureBuilder {
return this;
}

withConversionRateDisabled() {
return this.withPreferencesController({
useCurrencyRateCheck: false,
});
}

withConversionRateEnabled() {
return this.withPreferencesController({
useCurrencyRateCheck: true,
});
}

withGasFeeController(data) {
merge(this.fixture.data.GasFeeController, data);
return this;
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/helpers/mock-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { strict as assert } from 'assert';
import { Driver } from '../webdriver/driver';
import { MockedEndpoint } from '../mock-e2e';

const TIMEOUT_DEFAULT = 10 * 1000; // 10 Seconds

export async function expectMockRequest(
driver: Driver,
mockedEndpoint: MockedEndpoint,
{ timeout }: { timeout?: number } = {},
) {
await driver.wait(async () => {
const isPending = await mockedEndpoint.isPending();
return isPending === false;
}, timeout ?? TIMEOUT_DEFAULT);
}

export async function expectNoMockRequest(
driver: Driver,
mockedEndpoint: MockedEndpoint,
{ timeout }: { timeout?: number } = {},
) {
await driver.delay(timeout ?? TIMEOUT_DEFAULT);

const isPending = await mockedEndpoint.isPending();

assert.ok(isPending, 'Expected no requests');
}
72 changes: 72 additions & 0 deletions test/e2e/tests/metrics/sessions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { MockttpServer } from 'mockttp';
import FixtureBuilder from '../../fixture-builder';
import {
defaultGanacheOptions,
unlockWallet,
withFixtures,
} from '../../helpers';
import {
expectMockRequest,
expectNoMockRequest,
} from '../../helpers/mock-server';

async function mockSentrySession(mockServer: MockttpServer) {
return [
await mockServer
.forPost(/sentry/u)
.withBodyIncluding('"type":"session"')
.withBodyIncluding('"status":"exited"')
.thenCallback(() => {
return {
statusCode: 200,
json: {},
};
}),
];
}

describe('Sessions', function () {
it('sends session in UI if metrics enabled', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withMetaMetricsController({
participateInMetaMetrics: true,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
testSpecificMock: mockSentrySession,
manifestFlags: {
doNotForceSentryForThisTest: true,
},
},
async ({ driver, mockedEndpoint }) => {
await unlockWallet(driver);
await expectMockRequest(driver, mockedEndpoint[0], { timeout: 3000 });
},
);
});

it('does not send session in UI if metrics disabled @no-mmi', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder()
.withMetaMetricsController({
participateInMetaMetrics: false,
})
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
testSpecificMock: mockSentrySession,
manifestFlags: {
doNotForceSentryForThisTest: true,
},
},
async ({ driver, mockedEndpoint }) => {
await unlockWallet(driver);
await expectNoMockRequest(driver, mockedEndpoint[0], { timeout: 3000 });
},
);
});
});
16 changes: 4 additions & 12 deletions test/e2e/tests/settings/account-token-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Settings', function () {
it('Should match the value of token list item and account list item for eth conversion', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder().build(),
fixtures: new FixtureBuilder().withConversionRateDisabled().build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.fullTitle(),
},
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('Settings', function () {
it('Should match the value of token list item and account list item for fiat conversion', async function () {
await withFixtures(
{
fixtures: new FixtureBuilder().build(),
fixtures: new FixtureBuilder().withConversionRateEnabled().build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.fullTitle(),
},
Expand All @@ -57,16 +57,6 @@ describe('Settings', function () {
tag: 'div',
});
await driver.clickElement({ text: 'Fiat', tag: 'label' });
// We now need to enable "Show fiat on testnet" if we are using testnets (and since our custom
// network during test is using a testnet chain ID, it will be considered as a test network)
await driver.clickElement({
text: 'Advanced',
tag: 'div',
});
await driver.clickElement('.show-fiat-on-testnets-toggle');
// Looks like when enabling the "Show fiat on testnet" it takes some time to re-update the
// overview screen, so just wait a bit here:
await driver.delay(1000);

await driver.clickElement(
'.settings-page__header__title-container__close-button',
Expand All @@ -78,7 +68,9 @@ describe('Settings', function () {
const tokenListAmount = await driver.findElement(
'.eth-overview__primary-container',
);
await driver.delay(1000);
assert.equal(await tokenListAmount.getText(), '$42,500.00\nUSD');

await driver.clickElement('[data-testid="account-menu-icon"]');
const accountTokenValue = await driver.waitForSelector(
'.multichain-account-list-item .multichain-account-list-item__asset',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const MALFORMED_TRANSACTION_MOCK = {

export const MALFORMED_TRANSACTION_REQUEST_MOCK: MockRequestResponse = {
request: {
id: '21',
jsonrpc: '2.0',
method: 'infura_simulateTransactions',
params: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export async function mockRequest(
) {
await server
.forPost(TX_SENTINEL_URL)
.withJsonBody(request)
.withJsonBodyIncluding(request)
.thenJson(200, response);
}

Expand Down
30 changes: 27 additions & 3 deletions test/e2e/tests/swaps/swaps-notifications.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const { withFixtures, unlockWallet } = require('../../helpers');
const { SWAP_TEST_ETH_USDC_TRADES_MOCK } = require('../../../data/mock-data');
const {
withFixturesOptions,
buildQuote,
reviewQuote,
checkNotification,
} = require('./shared');

async function mockSwapsTransactionQuote(mockServer) {
return [
await mockServer
.forGet('https://swap.api.cx.metamask.io/networks/1/trades')
.thenCallback(() => ({
statusCode: 200,
json: SWAP_TEST_ETH_USDC_TRADES_MOCK,
})),
];
}

describe('Swaps - notifications @no-mmi', function () {
async function mockTradesApiPriceSlippageError(mockServer) {
await mockServer
Expand Down Expand Up @@ -95,25 +107,37 @@ describe('Swaps - notifications @no-mmi', function () {
);
});
it('tests a notification for not enough balance', async function () {
const lowBalanceGanacheOptions = {
accounts: [
{
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: 0,
},
],
};

await withFixtures(
{
...withFixturesOptions,
ganacheOptions: lowBalanceGanacheOptions,
testSpecificMock: mockSwapsTransactionQuote,
title: this.test.fullTitle(),
},
async ({ driver }) => {
await unlockWallet(driver);
await buildQuote(driver, {
amount: 50,
amount: 0.001,
swapTo: 'USDC',
});
await checkNotification(driver, {
title: 'Insufficient balance',
text: 'You need 43.4467 more TESTETH to complete this swap',
text: 'You need 0.001 more TESTETH to complete this swap',
});
await reviewQuote(driver, {
swapFrom: 'TESTETH',
swapTo: 'USDC',
amount: 50,
amount: 0.001,
skipCounter: true,
});
await driver.waitForSelector({
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/tokens/custom-token-send-transfer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Transfer custom tokens @no-mmi', function () {
'.currency-display-component__text',
);
assert.notEqual(
await estimatedGasFee[0].getText(),
await estimatedGasFee[1].getText(),
'0',
'Estimated gas fee should not be 0',
);
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/tests/transaction/send-edit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Editing Confirm Transaction', function () {
{
fixtures: new FixtureBuilder()
.withTransactionControllerTypeOneTransaction()
.withConversionRateDisabled()
.build(),
ganacheOptions: defaultGanacheOptions,
title: this.test.fullTitle(),
Expand Down Expand Up @@ -87,6 +88,7 @@ describe('Editing Confirm Transaction', function () {
{
fixtures: new FixtureBuilder()
.withTransactionControllerTypeTwoTransaction()
.withConversionRateDisabled()
.build(),
ganacheOptions: generateGanacheOptions({ hardfork: 'london' }),
title: this.test.fullTitle(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import { screen, act, waitFor } from '@testing-library/react';
import { renderWithProvider } from '../../../../../test/jest';
import configureStore from '../../../../store/store';
import configureStore, { MetaMaskReduxState } from '../../../../store/store';
import mockState from '../../../../../test/data/mock-state.json';
import { CHAIN_IDS } from '../../../../../shared/constants/network';
import { useIsOriginalNativeTokenSymbol } from '../../../../hooks/useIsOriginalNativeTokenSymbol';
import { getTokenSymbol } from '../../../../store/actions';
import { getSelectedInternalAccountFromMockState } from '../../../../../test/jest/mocks';
import { mockNetworkState } from '../../../../../test/stub/networks';
import AssetList from './asset-list';
import AssetList from '.';

// Specific to just the ETH FIAT conversion
const CONVERSION_RATE = 1597.32;
Expand Down Expand Up @@ -67,8 +67,9 @@ jest.mock('../../../../store/actions', () => {
};
});

const mockSelectedInternalAccount =
getSelectedInternalAccountFromMockState(mockState);
const mockSelectedInternalAccount = getSelectedInternalAccountFromMockState(
mockState as unknown as MetaMaskReduxState,
);

const render = (balance = ETH_BALANCE, chainId = CHAIN_IDS.MAINNET) => {
const state = {
Expand Down Expand Up @@ -102,15 +103,15 @@ const render = (balance = ETH_BALANCE, chainId = CHAIN_IDS.MAINNET) => {
};
const store = configureStore(state);
return renderWithProvider(
<AssetList onClickAsset={() => undefined} />,
<AssetList onClickAsset={() => undefined} showTokensLinks />,
store,
);
};

describe('AssetList', () => {
useIsOriginalNativeTokenSymbol.mockReturnValue(true);
(useIsOriginalNativeTokenSymbol as jest.Mock).mockReturnValue(true);

getTokenSymbol.mockImplementation(async (address) => {
(getTokenSymbol as jest.Mock).mockImplementation(async (address) => {
if (address === USDC_CONTRACT) {
return 'USDC';
}
Expand Down
Loading

0 comments on commit d5005d0

Please sign in to comment.