Test if a call was made with a certain constant

Viewed 56

I have the following function which I like to test.

Is there a way I could test that the path was called with which token value?

export function load({ store: { dispatch }, ownProps }) {
      const tkn = external.sample1 || external.sample2;
      return {
        content: () => dispatch(resources({
          resource: 'fetch',
          id: {
            path: `/ref/${tkn}`,
          },
        })),
      };
    }

Example test that the path was either

/ref/sample1

or

/ref/sample2

This is the current test which passes but it doesn't test the tkn value.

jest.mock('sample-rest', () => ({
  resources: jest.fn(() => ({ data: 'some data' })),
  collection: jest.fn(() => ({ data: 'some other data' })),
}));

it('load should return the content', () => {
    const { content } = load({ store: mockStore, ownProps: defaultProps });
    expect(content()).toMatchSnapshot();
});

Snapshot

exports[`Component tests load should return the content 1`] = `
Object {
  "data": "some data",
}
`;

Update:

Attempted test after advice from answer.

const mockResource = jest.fn(() => ({ data: 'some data' }));
const mockCollection = jest.fn(() => ({ data: 'some collection' }));

jest.mock('sample-rest', () => ({
  resources: () => mockResource,
  collection: () => mockCollection,
}));

Test case is same as answer below.
0 Answers
Related