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(assets-controllers): Simplify TokenBalancesController tests #3743

Merged
merged 1 commit into from
Jan 8, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Jan 8, 2024

Explanation

The TokenBalancesController tests have been refactored to use mocks instead of NetworkController and PreferencesController instances. This should make the tests easier to read, and it decouples the tests further from the other controllers.

References

Relates to #3708

Changelog

@metamask/assets-controllers

Added

  • Add getDefaultTokensState function to the TokensController

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

@Gudahtt Gudahtt marked this pull request as ready for review January 8, 2024 15:21
@Gudahtt Gudahtt requested a review from a team as a code owner January 8, 2024 15:21
@jiexi
Copy link
Contributor

jiexi commented Jan 8, 2024

If we're going to use this pattern in more of the tests, is it worth creating some sort of helper that returns fns that keeps track of the listener and a way to trigger them?

const mockStateChangeListener<T>() => {
  const listeners: ((state: T) => void)[] = [];
  
  return {
    onStateChange: (listener) => {
          listeners.push(listener);
    },
     triggerStateChange: () => {
         for (const listener of listeners) {
          listener(state);
        }
     }
  }
}

Maybe? Not sure if it's complicated enough to warrant the helper though

The `TokenBalancesController` tests have been refactored to use mocks
instead of `NetworkController` and `PreferencesController` instances.
This should make the tests easier to read, and it decouples the tests
further from the other controllers.

Relates to #3708
@Gudahtt Gudahtt force-pushed the refactor-token-balances-controller-tests branch from 262b25e to 000b301 Compare January 8, 2024 18:13
@Gudahtt
Copy link
Member Author

Gudahtt commented Jan 8, 2024

Good suggestion @jiexi . That sounds like it could be useful, but we may see this pattern used less often as we complete the BaseControllerV2 migrations so maybe the need for it will be eliminated fairly soon.

The BaseControllerV2 controllers will use the messenger rather than on[event] constructor parameters.

@Gudahtt Gudahtt merged commit 5762e8a into main Jan 8, 2024
136 checks passed
@Gudahtt Gudahtt deleted the refactor-token-balances-controller-tests branch January 8, 2024 18:20
@Gudahtt Gudahtt mentioned this pull request Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants