Skip to content

Commit

Permalink
update unit test mocks to not modify child state by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
bergeron committed Sep 18, 2024
1 parent d75e942 commit e1db1c3
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions app/scripts/migrations/127.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ describe(`migration #${version}`, () => {
},
};

const expectedState = defaultPostMigrationState();
const expectedNetwork =
expectedState.networkConfigurationsByChainId[customNetwork.chainId];
const defaultStateToExpect = defaultPostMigrationState();
const expectedNetwork = {
...defaultStateToExpect.networkConfigurationsByChainId[
customNetwork.chainId
],
};

// The custom network's rpc url should be added to the existing network
expectedNetwork.rpcEndpoints.push({
Expand All @@ -68,6 +71,14 @@ describe(`migration #${version}`, () => {
customNetwork.rpcPrefs.blockExplorerUrl,
);

const expectedState = {
...defaultStateToExpect,
networkConfigurationsByChainId: {
...defaultStateToExpect.networkConfigurationsByChainId,
[customNetwork.chainId]: expectedNetwork,
},
};

const newState = await migrate(oldState);
expect(newState.data.NetworkController).toStrictEqual(expectedState);
});
Expand Down Expand Up @@ -144,12 +155,14 @@ describe(`migration #${version}`, () => {
},
};

const expectedState = defaultPostMigrationState();
const expectedNetwork =
expectedState.networkConfigurationsByChainId[customNetwork.chainId];
const defaultStateToExpect = defaultPostMigrationState();
const expectedNetwork = {
...defaultStateToExpect.networkConfigurationsByChainId[
customNetwork.chainId
],
};

// The custom network should remain selected, and become the default RPC url
expectedState.selectedNetworkClientId = customNetwork.id;
// The custom network should become the default RPC url
expectedNetwork.defaultRpcEndpointIndex =
expectedNetwork.rpcEndpoints.push({
networkClientId: customNetwork.id,
Expand All @@ -165,6 +178,16 @@ describe(`migration #${version}`, () => {
customNetwork.rpcPrefs.blockExplorerUrl,
) - 1;

const expectedState = {
...defaultStateToExpect,
// The custom network should remain selected
selectedNetworkClientId: customNetwork.id,
networkConfigurationsByChainId: {
...defaultStateToExpect.networkConfigurationsByChainId,
[customNetwork.chainId]: expectedNetwork,
},
};

const newState = await migrate(oldState);
expect(newState.data.NetworkController).toStrictEqual(expectedState);
});
Expand Down Expand Up @@ -207,13 +230,12 @@ describe(`migration #${version}`, () => {
},
};

// Selected network shouldn't change
const expectedState = defaultPostMigrationState();
expectedState.selectedNetworkClientId =
oldState.data.NetworkController.selectedNetworkClientId;

const expectedNetwork =
expectedState.networkConfigurationsByChainId[customNetwork.chainId];
const defaultStateToExpect = defaultPostMigrationState();
const expectedNetwork = {
...defaultStateToExpect.networkConfigurationsByChainId[
customNetwork.chainId
],
};

// The custom network's rpc url should be added to the
// existing network, and become the default RPC url
Expand All @@ -232,6 +254,17 @@ describe(`migration #${version}`, () => {
customNetwork.rpcPrefs.blockExplorerUrl,
) - 1;

const expectedState = {
...defaultStateToExpect,
// Selected network shouldn't change
selectedNetworkClientId:
oldState.data.NetworkController.selectedNetworkClientId,
networkConfigurationsByChainId: {
...defaultStateToExpect.networkConfigurationsByChainId,
[customNetwork.chainId]: expectedNetwork,
},
};

const newState = await migrate(oldState);
expect(newState.data.NetworkController).toStrictEqual(expectedState);
});
Expand Down

0 comments on commit e1db1c3

Please sign in to comment.