Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Parameterized mocking with different return values based on a parameter. #811

Open
stephenkjohnston opened this issue Apr 22, 2020 · 0 comments

Comments

@stephenkjohnston
Copy link

Hello, I'm working on trying to find a solution to mocking with parameters and having said mock return a different value based on the parameter. I've come up with the below solution but it doesn't quite feel right despite working.

Any advice on accomplishing this in a cleaner way would be much appreciated.

jest.mock(
  'hooks/useResourceAttribute',
  () => (domain: string, type: string, id: string, attribute: string) => {
    let value: string = '';
    if (type === 'blah') {
      value = 'meh';
    }

    if (type === 'blah2') {
      value = 'meh2';
    }

    return {
      value,
      fetch: jest.fn(),
    };
  }
);
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant