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

Establish test helper function pattern across all controllers #922

Open
BelfordZ opened this issue Sep 27, 2022 · 0 comments
Open

Establish test helper function pattern across all controllers #922

BelfordZ opened this issue Sep 27, 2022 · 0 comments

Comments

@BelfordZ
Copy link
Contributor

    One pattern I've been following in other projects recently to handle steps that you'd normally put into an `afterEach` is to have your setup function take a function. I like to rename the function `withXXXX` to indicate that it doesn't return the controllers (or test data) itself. For instance you could have:
function withControllers(fn): Controllers {
  // ...instantiate controllers...

  try {
    fn({ messenger, network, preferences, assetsContract });
  } finally {
    messenger.clearEventSubscriptions('NetworkController:stateChange');
  }
}

The reason withControllers takes a function is because that would now represent the test. Here's how you'd update an existing test for instance:

  it('should update the ipfsGateWay config value when this value is changed in the preferences controller', () => {
    withControllers(({ assetsContract, messenger, preferences }) => {
      expect(assetsContract.config).toStrictEqual({
        chainId: SupportedTokenDetectionNetworks.mainnet,
        ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,
        provider: undefined,
      });
      preferences.setIpfsGateway('newIPFSGateWay');
      expect(assetsContract.config).toStrictEqual({
        ipfsGateway: 'newIPFSGateWay',
        chainId: SupportedTokenDetectionNetworks.mainnet,
        provider: undefined,
      });
    });
  });

I realize you've spent a lot of time on these tests already, so this isn't a request for this PR, but could give you ideas for a future PR.

Originally posted by @mcmire in #903 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants