Skip to content

Establish test helper function pattern across all controllers #922

Open
@BelfordZ

Description

@BelfordZ
    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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions