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

test(ramp): add useFiatCurrencies tests #9582

Merged
merged 6 commits into from
Jul 25, 2024
Merged

Conversation

wachunei
Copy link
Member

@wachunei wachunei commented May 9, 2024

Description

This PR adds tests for the useFiatCurrencies hook in Ramp folder.

Related issues

Fixes: N/A

Manual testing steps

N/A

Screenshots/Recordings

N/A

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@wachunei wachunei added No QA Needed Apply this label when your PR does not need any QA effort. needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) team-ramp issues related to Ramp features Run Smoke E2E Triggers smoke e2e on Bitrise labels May 9, 2024
Copy link
Contributor

github-actions bot commented May 9, 2024

https://bitrise.io/ Bitrise

✅✅✅ pr_smoke_e2e_pipeline passed on Bitrise! ✅✅✅

Commit hash: f71809a
Build link: https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/c24d6922-9a38-4189-8059-0af984eb4002

Note

  • You can kick off another pr_smoke_e2e_pipeline on Bitrise by removing and re-applying the Run Smoke E2E label on the pull request

@wachunei wachunei marked this pull request as ready for review May 9, 2024 03:34
@wachunei wachunei requested review from a team as code owners May 9, 2024 03:34
@wachunei wachunei added Run Smoke E2E Triggers smoke e2e on Bitrise and removed Run Smoke E2E Triggers smoke e2e on Bitrise labels Jun 19, 2024
Copy link
Contributor

github-actions bot commented Jun 19, 2024

https://bitrise.io/ Bitrise

✅✅✅ pr_smoke_e2e_pipeline passed on Bitrise! ✅✅✅

Commit hash: 48b3fbf
Build link: https://app.bitrise.io/app/be69d4368ee7e86d/pipelines/fdf67ea7-e9cc-4ad9-be0a-9fa94aec6d3e

Note

  • You can kick off another pr_smoke_e2e_pipeline on Bitrise by removing and re-applying the Run Smoke E2E label on the pull request

Copy link

@georgeweiler
Copy link
Contributor

awesome! I have a small suggestion for cleaning up this file, but it's a nit. I see some re-used data passed to useSdkMethod. We could extract it out as default values to make each test a bit more readable:

I haven't run this code, but something like this might be helpful:

const mockUseSDKMethod = (defaultFiatCurrency, fiatCurrencies, isFetchingDefault = false, isFetchingFiat = false, errorDefault = null, errorFiat = null) => {
  (useSDKMethod as jest.Mock)
    .mockReturnValueOnce([
      {
        data: defaultFiatCurrency, // HERE - we can pass just the data and abstract away the boilerplate
        error: errorDefault,
        isFetching: isFetchingDefault,
      },
      jest.fn(),
    ])
    .mockReturnValueOnce([
      {
        data: fiatCurrencies,
        error: errorFiat,
        isFetching: isFetchingFiat,
      },
      jest.fn(),
    ]);
};

and then the test could look like this:

it('should call useSDKMethod with correct parameters when buying', () => {
    mockUseSDKMethod([], []); 
    renderHookWithProvider(() => useFiatCurrencies());

    expect(useSDKMethod).toHaveBeenCalledWith(
      'getDefaultFiatCurrency',
      'test-region-id',
      'test-payment-method-id',
    );

    expect(useSDKMethod).toHaveBeenCalledWith(
      'getFiatCurrencies',
      'test-region-id',
      'test-payment-method-id',
    );
  });

another example:

  it('does not select the default fiat currency if current selection is available', () => {
    mockUseRampSDKValues.selectedFiatCurrencyId = 'test-fiat-currency-id-2';
    
    mockUseSDKMethod({ id: 'default-fiat-currency-id' }, [
      { id: 'test-fiat-currency-id-1' },
      { id: 'test-fiat-currency-id-2' },
      { id: 'default-fiat-currency-id' },
    ]);
    
    renderHookWithProvider(() => useFiatCurrencies());

    expect(
      mockUseRampSDKValues.setSelectedFiatCurrencyId,
    ).not.toHaveBeenCalled();
  });

Copy link
Contributor

@georgeweiler georgeweiler left a comment

Choose a reason for hiding this comment

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

Approved with a nit to consider.

@wachunei
Copy link
Member Author

I see the direction of your comment but I think another layer of indirection would be mostl helpful for default-ish cases. In this case I vary the combinations for the two sets of { data, error, loading } a lot.

In the abstracted method there are already 6 arguments and there's still missing two more for the jest.fn()s that are mocked sometimes

So in most cases it won't look like this mockUseSDKMethod([], []); but mockUseSDKMethod( /* an array of 8 args*/), which might get confusing to understand what is what because of the default arguments requirement to be present to keep their position, unless we want to add code to identify the type to correctly position the argument, which is now worse than the plain mocking it is now.

@wachunei wachunei removed the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Jul 25, 2024
Copy link

@wachunei wachunei merged commit d8e48b4 into main Jul 25, 2024
35 checks passed
@wachunei wachunei deleted the test/ramp-use-fiat-currencies branch July 25, 2024 16:27
@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2024
@metamaskbot metamaskbot added the release-7.29.0 Issue or pull request that will be included in release 7.29.0 label Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
No QA Needed Apply this label when your PR does not need any QA effort. release-7.29.0 Issue or pull request that will be included in release 7.29.0 Run Smoke E2E Triggers smoke e2e on Bitrise team-ramp issues related to Ramp features
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants