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

How to return thrown object in test #11

Open
DaKaZ opened this issue Nov 13, 2018 · 1 comment
Open

How to return thrown object in test #11

DaKaZ opened this issue Nov 13, 2018 · 1 comment

Comments

@DaKaZ
Copy link

DaKaZ commented Nov 13, 2018

In my saga I have code that looks like this:

  try {
// removed for brevity
  } catch (e) {
    const errorMsg = e && e.response ? `Error ${e.response.status} - Unable to create Timesheet` : 'An unknown error occurred';
    const errorObj = e && e.response && e.response.data && e.response.data.result || store.timesheet.model
    yield put(timesheet.actions.setTimesheets(store.timesheet.collection, errorObj, errorMsg));
  }

I have this in my saga test:

  it('should call api', (result) => {
    expect(result).toEqual(call(api, mockState.auth, '/timesheets'));
    return new Error('Something went wrong')
  });

which works great for the generic "unknown error occurred" but I need to test throwing an object like:

    throw({
      response: {
        data: { result: mockTimesheetWithError },
        status: 406
      }
    });

But I can't figure out actually throw the object back to the object. Any ideas?

@inzKornel
Copy link

I faced similar problem and do it like:

class MockError extends Error {
  constructor(...params) {
    super(...params);

    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, MockError);
    }

    this.response = {
      data: {
        error: 3001
      }
    };
  }
}
it('should have called genres api first', result => {
    expect(something).toEqual(something);
    return new MockError();
});

it('then should call add notification action', result => {
    expect(result).toEqual(put(someAction(new MockError())));
});
// and it works :)
const someAction = error => console.log(error.response.data)

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

No branches or pull requests

2 participants