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 NftDetectionController tests #3742

Merged
merged 2 commits into from
Jan 8, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Jan 8, 2024

Explanation

The NftDetectionController tests have been refactored to use mocks rather than full AssetsContractController, PreferencesController, and NftController references. The withController pattern has been introduced as well, to simplify the controller setup and ensure all polling has stopped after each test.

A few tests were found to be testing functionality that was internal to the NftController, so they have been removed. Coverage for the NftDetectionController remains unchanged.

References

Relates to #3708

Changelog

@metamask/assets-controllers

Added

  • Add getDefaultNftState function to the NftController

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 14:15
@Gudahtt Gudahtt requested a review from a team as a code owner January 8, 2024 14:15
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Just a couple of comments, but otherwise looks good.

});
mockNfts.reset();

await controller.start();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? It seems that the controller self-starts when useNftDetection is enabled.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm. Good point. The way polling is setup right now doesn't really make sense. Not only does it auto-start, it also fails to start at all if the current network isn't Ethereum Mainnet (even if you later switch to Ethereum Mainnet).

For now I'll remove these unnecessary steps, but I'll follow up later with a PR that properly separates the disabled state from the useNftDetection setting, which will make this API make sense again.

Addressed in ac26f42

Comment on lines +619 to +620
provider: jest.fn(),
blockTracker: jest.fn(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, these aren't functions. Could we create a FakeProvider and then use to construct a block tracker? If that's too much trouble, that's fine, but just a thought.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, though I'm inclined to leave this alone for now because this mock is pre-existing. This PR just moves it into a helper function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, no problem.

The `NftDetectionController` tests have been refactored to use mocks
rather than full `AssetsContractController`, `PreferencesController`,
and `NftController` references. The `withController` pattern has been
introduced as well, to simplify the controller setup and ensure all
polling has stopped after each test.

A few tests were found to be testing functionality that was internal
to the `NftController`, so they have been removed. Coverage for the
`NftDetectionController` remains unchanged.

Relates to #3708
@Gudahtt Gudahtt force-pushed the refactor-nft-detection-controller-tests branch from 9ccc441 to 9de452d Compare January 8, 2024 18:20
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

LGTM!

@Gudahtt Gudahtt merged commit 894c9a8 into main Jan 8, 2024
136 checks passed
@Gudahtt Gudahtt deleted the refactor-nft-detection-controller-tests branch January 8, 2024 19:04
@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