1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Fix flakey tests in metamask controller tests (#19312)

* Refactor metamask controller tests to isolate mv3 specific setup
This commit is contained in:
Nicholas Ellul 2023-06-14 15:38:40 -04:00 committed by GitHub
parent 875bad125f
commit 46a2604df0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,7 +31,9 @@ const browserPolyfillMock = {
getPlatformInfo: async () => 'mac',
},
storage: {
session: {},
session: {
set: () => undefined,
},
},
};
@ -186,19 +188,13 @@ const firstTimeState = {
},
};
const noop = () => undefined;
describe('MetaMaskController', function () {
let metamaskController;
const sandbox = sinon.createSandbox();
const noop = () => undefined;
browserPolyfillMock.storage.session.set = sandbox.spy();
before(async function () {
globalThis.isFirstTimeProfileLoaded = true;
await ganacheServer.start();
sinon.spy(MetaMaskController.prototype, 'resetStates');
sinon.spy(MetaMaskControllerMV3.prototype, 'resetStates');
});
beforeEach(function () {
@ -232,6 +228,24 @@ describe('MetaMaskController', function () {
sendMessage: sandbox.stub().rejects(),
});
browserPolyfillMock.storage.session.set = sandbox.spy();
});
afterEach(function () {
nock.cleanAll();
sandbox.restore();
});
after(async function () {
await ganacheServer.quit();
});
describe('MetaMaskController Behaviour', function () {
let metamaskController;
beforeEach(function () {
sandbox.spy(MetaMaskController.prototype, 'resetStates');
metamaskController = new MetaMaskController({
showUserConfirmation: noop,
encryptor: {
@ -265,81 +279,11 @@ describe('MetaMaskController', function () {
);
});
afterEach(function () {
nock.cleanAll();
sandbox.restore();
});
after(async function () {
await ganacheServer.quit();
});
describe('should reset states on first time profile load', function () {
it('in mv2, it should reset state without attempting to call browser storage', function () {
assert.equal(metamaskController.resetStates.callCount, 1);
assert.equal(browserPolyfillMock.storage.session.set.callCount, 0);
});
it('in mv3, it should reset state', function () {
MetaMaskControllerMV3.prototype.resetStates.resetHistory();
const metamaskControllerMV3 = new MetaMaskControllerMV3({
showUserConfirmation: noop,
encryptor: {
encrypt(_, object) {
this.object = object;
return Promise.resolve('mock-encrypted');
},
decrypt() {
return Promise.resolve(this.object);
},
},
initState: cloneDeep(firstTimeState),
initLangCode: 'en_US',
platform: {
showTransactionNotification: () => undefined,
getVersion: () => 'foo',
},
browser: browserPolyfillMock,
infuraProjectId: 'foo',
isFirstMetaMaskControllerSetup: true,
});
assert.equal(metamaskControllerMV3.resetStates.callCount, 1);
assert.equal(browserPolyfillMock.storage.session.set.callCount, 1);
assert.deepEqual(
browserPolyfillMock.storage.session.set.getCall(0).args[0],
{
isFirstMetaMaskControllerSetup: false,
},
);
});
it('in mv3, it should not reset states if isFirstMetaMaskControllerSetup is false', function () {
MetaMaskControllerMV3.prototype.resetStates.resetHistory();
browserPolyfillMock.storage.session.set.resetHistory();
const metamaskControllerMV3 = new MetaMaskControllerMV3({
showUserConfirmation: noop,
encryptor: {
encrypt(_, object) {
this.object = object;
return Promise.resolve('mock-encrypted');
},
decrypt() {
return Promise.resolve(this.object);
},
},
initState: cloneDeep(firstTimeState),
initLangCode: 'en_US',
platform: {
showTransactionNotification: () => undefined,
getVersion: () => 'foo',
},
browser: browserPolyfillMock,
infuraProjectId: 'foo',
isFirstMetaMaskControllerSetup: false,
});
assert.equal(metamaskControllerMV3.resetStates.callCount, 0);
assert.equal(browserPolyfillMock.storage.session.set.callCount, 0);
});
});
describe('#importAccountWithStrategy', function () {
@ -507,7 +451,10 @@ describe('MetaMaskController', function () {
);
delete secondVaultIdentities[TEST_ADDRESS_ALT].lastSelected;
assert.deepEqual(secondVaultIdentities, {
[TEST_ADDRESS_ALT]: { address: TEST_ADDRESS_ALT, name: DEFAULT_LABEL },
[TEST_ADDRESS_ALT]: {
address: TEST_ADDRESS_ALT,
name: DEFAULT_LABEL,
},
});
});
@ -682,7 +629,8 @@ describe('MetaMaskController', function () {
.stub(metamaskController.keyringController, 'getKeyringsByType')
.returns([mockHDKeyring]);
const recoveredMnemonic = metamaskController.getPrimaryKeyringMnemonic();
const recoveredMnemonic =
metamaskController.getPrimaryKeyringMnemonic();
assert.equal(recoveredMnemonic, uint8ArrayMnemonic);
});
@ -769,7 +717,10 @@ describe('MetaMaskController', function () {
getAccountsStub.onCall(2).returns(Promise.resolve(['0x3']));
getAccountsStub.onCall(3).returns(Promise.resolve(['0x4']));
sinon.spy(metamaskController.preferencesController, 'setAddresses');
sinon.spy(metamaskController.preferencesController, 'setSelectedAddress');
sinon.spy(
metamaskController.preferencesController,
'setSelectedAddress',
);
sinon.spy(metamaskController.preferencesController, 'setAccountLabel');
await metamaskController
.connectHardware(HardwareDeviceNames.trezor, 0, `m/44'/1'/0'/0`)
@ -807,12 +758,15 @@ describe('MetaMaskController', function () {
});
it('should call preferencesController.setAddresses', async function () {
assert(metamaskController.preferencesController.setAddresses.calledOnce);
assert(
metamaskController.preferencesController.setAddresses.calledOnce,
);
});
it('should call preferencesController.setSelectedAddress', async function () {
assert(
metamaskController.preferencesController.setSelectedAddress.calledOnce,
metamaskController.preferencesController.setSelectedAddress
.calledOnce,
);
});
@ -868,7 +822,9 @@ describe('MetaMaskController', function () {
'getNetworkId',
);
selectedAddressStub.returns('0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc');
selectedAddressStub.returns(
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
);
getNetworkIdStub.returns(42);
metamaskController.txController.txStateManager._addTransactionsToState([
@ -1254,7 +1210,8 @@ describe('MetaMaskController', function () {
});
metamaskController.provider = provider;
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
const tokenDetails =
await metamaskController.getTokenStandardAndDetails(
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'0xf0d172594caedee459b89ad44c94098e474571b6',
);
@ -1304,7 +1261,8 @@ describe('MetaMaskController', function () {
});
metamaskController.provider = provider;
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
const tokenDetails =
await metamaskController.getTokenStandardAndDetails(
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'0xf0d172594caedee459b89ad44c94098e474571b6',
);
@ -1340,7 +1298,8 @@ describe('MetaMaskController', function () {
});
metamaskController.provider = provider;
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
const tokenDetails =
await metamaskController.getTokenStandardAndDetails(
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'0xf0d172594caedee459b89ad44c94098e474571b6',
);
@ -1401,7 +1360,8 @@ describe('MetaMaskController', function () {
return tokenData;
});
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
const tokenDetails =
await metamaskController.getTokenStandardAndDetails(
'0xNotInTokenList',
'0xf0d172594caedee459b89ad44c94098e474571b6',
);
@ -1461,7 +1421,8 @@ describe('MetaMaskController', function () {
return tokenData;
});
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
const tokenDetails =
await metamaskController.getTokenStandardAndDetails(
'0xAAA75474e89094c44da98b954eedeac495271d0f',
'0xf0d172594caedee459b89ad44c94098e474571b6',
);
@ -1521,7 +1482,8 @@ describe('MetaMaskController', function () {
return tokenData;
});
const tokenDetails = await metamaskController.getTokenStandardAndDetails(
const tokenDetails =
await metamaskController.getTokenStandardAndDetails(
'0xAAA75474e89094c44da98b954eedeac495271d0f',
'0xf0d172594caedee459b89ad44c94098e474571b6',
);
@ -1635,4 +1597,77 @@ describe('MetaMaskController', function () {
});
});
});
});
describe('MV3 Specific behaviour', function () {
before(async function () {
globalThis.isFirstTimeProfileLoaded = true;
});
beforeEach(async function () {
sandbox.spy(MetaMaskControllerMV3.prototype, 'resetStates');
});
it('it should reset state', function () {
browserPolyfillMock.storage.session.set.resetHistory();
const metamaskControllerMV3 = new MetaMaskControllerMV3({
showUserConfirmation: noop,
encryptor: {
encrypt(_, object) {
this.object = object;
return Promise.resolve('mock-encrypted');
},
decrypt() {
return Promise.resolve(this.object);
},
},
initState: cloneDeep(firstTimeState),
initLangCode: 'en_US',
platform: {
showTransactionNotification: () => undefined,
getVersion: () => 'foo',
},
browser: browserPolyfillMock,
infuraProjectId: 'foo',
isFirstMetaMaskControllerSetup: true,
});
assert.equal(metamaskControllerMV3.resetStates.callCount, 1);
assert.equal(browserPolyfillMock.storage.session.set.callCount, 1);
assert.deepEqual(
browserPolyfillMock.storage.session.set.getCall(0).args[0],
{
isFirstMetaMaskControllerSetup: false,
},
);
});
it('in mv3, it should not reset states if isFirstMetaMaskControllerSetup is false', function () {
browserPolyfillMock.storage.session.set.resetHistory();
const metamaskControllerMV3 = new MetaMaskControllerMV3({
showUserConfirmation: noop,
encryptor: {
encrypt(_, object) {
this.object = object;
return Promise.resolve('mock-encrypted');
},
decrypt() {
return Promise.resolve(this.object);
},
},
initState: cloneDeep(firstTimeState),
initLangCode: 'en_US',
platform: {
showTransactionNotification: () => undefined,
getVersion: () => 'foo',
},
browser: browserPolyfillMock,
infuraProjectId: 'foo',
isFirstMetaMaskControllerSetup: false,
});
assert.equal(metamaskControllerMV3.resetStates.callCount, 0);
assert.equal(browserPolyfillMock.storage.session.set.callCount, 0);
});
});
});