Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TokensController tests #4247

Merged
merged 6 commits into from
May 6, 2024
Merged

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented May 2, 2024

Explanation

We want to change TokensController so that it relies on selectedNetworkClientId instead of providerConfig to know which chain is selected, and this will require updating the tests to match. However, the TokensController tests are written in such a way that makes updating them difficult.

This commit rewrites each test to use the withController pattern that we've adopted for other controller tests. It also removes usage of beforeEach so that each test is isolated.

Note that this commit isn't a complete refactor of the tests; there is more work we could do to make them easier to maintain, such as removing a reliance on ApprovalController and using describe to group tests by method. This step can be done later.

In addition, this commit also adds some NetworkController-related and BaseController-related test helpers which the TokensController tests use. These helpers aren't defined in the public part of their respective packages, but in the private area. The TokensController tests can import them directly because they are private themselves, and therefore don't need to adhere to the same importing rules that the (public) implementation files do.

Finally, this commit also exposes BuiltInNetworkClientId and CustomNetworkClientId. These are used in the aforementioned test helpers but are also useful on their own.

Tip

The diff for the tests will be easier to read if you hide whitespace.

References

This is a prerequisite to #4185.

Changelog

@metamask/network-controller

  • ADDED: Export BuiltInNetworkClientId and CustomNetworkClientId.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mcmire mcmire force-pushed the refactor-tokens-controller-tests branch 2 times, most recently from fb59737 to 0884667 Compare May 3, 2024 06:08
We want to change TokensController so that it relies on
`selectedNetworkClientId` instead of `providerConfig` to know which
chain is selected, and this will require updating the tests to match.
However, the TokensController tests are written in such a way that makes
updating them difficult.

This commit rewrites each test to use the `withController` pattern that
we've adopted for other controller tests. It also removes usage of
`beforeEach` so that each test is isolated.

Note that this commit isn't a complete refactor of the tests; there is
more work we could do to make them easier to maintain, such as removing
a reliance on ApprovalController and using `describe` to group tests by
method. This step can be done later.

In addition, this commit also adds some NetworkController-related and
BaseController-related test helpers which the TokensController tests
use. These helpers aren't defined in the public part of their respective
packages, but in the private area. The TokensController tests can import
them directly because they are private themselves, and therefore don't
need to adhere to the same importing rules that the (public)
implementation files do.

Finally, this commit also exposes `BuiltInNetworkClientId` and
`CustomNetworkClientId`. These are used in the aforementioned test
helpers but are also useful on their own.
@mcmire mcmire force-pushed the refactor-tokens-controller-tests branch from 0884667 to 26e38f9 Compare May 3, 2024 06:09
@mcmire mcmire marked this pull request as ready for review May 3, 2024 14:50
@mcmire mcmire requested a review from a team as a code owner May 3, 2024 14:50
* requests.
* @returns The fake network client.
*/
function buildFakeNetworkClient({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern of including test helpers specific to a controller inside the package for that controller is new. Previously we might have just put it within the test file that needed these helpers. But by putting them here, we define them once and then any package in the monorepo can use them.

};

const getNetworkClientById = buildMockGetNetworkClientById({
// Since we access these networks so often in these tests, register
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could put these defaults directly in buildMockGetNetworkClientById. We do end up needing these in other test files. Thoughts? Would that be too much magic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Seems fine to build them directly into buildMockGetNetworkClientById to me - I don't think it's too much magic.

Copy link
Contributor Author

@mcmire mcmire May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay — done in 9bc9d12 and 7adbbd3.

'TokenListController:stateChange',
],
});
tokensController = new TokensController({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, so many controllers. Really glad to see this get deleted!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I didn't want to go so far as to remove ApprovalController — that's still there. But, the TransactionController tests don't use a real ApprovalController, so we could copy the patterns we have there, or extract the helper functions so that we can reuse them here.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fantastic! Just a couple of questions

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mcmire mcmire merged commit f31ff0f into main May 6, 2024
139 checks passed
@mcmire mcmire deleted the refactor-tokens-controller-tests branch May 6, 2024 15:06
@Gudahtt Gudahtt mentioned this pull request May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants