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 use put.resolve with function #164

Open
dangh opened this issue Dec 27, 2017 · 0 comments
Open

How to use put.resolve with function #164

dangh opened this issue Dec 27, 2017 · 0 comments

Comments

@dangh
Copy link

dangh commented Dec 27, 2017

Hello,

Is there any way to test the put.resolve when I pass it a Promise instead of object?

Here is my code:

const registry = new WeakMap()

const defer = action => {
  return dispatch => new Promise((resolve, reject) => {
    // register the callbacks and use it to resolve/reject later
    registry.set(action, {resolve, reject})
    dispatch(action)
  })
}

function* handleSaga() {
  const action = yield take('MY_ACTION')
  fetch('MY_API').then(
    result => registry.get(action).resolve(result),
    reason => registry.get(action).reject(reason)
  )
}

function* rootSaga() {
  yield put.resolve(defer({type: 'MY_ACTION'}))
}

And the test:

expectSaga(rootSaga)
  .put.resolve(defer({type: 'MY_ACTION'}))
  .run()

But it's failed because the result of defer() is a Promise, not a plain object.

I know I can create a middleware to handle the promise and put a plain object instead but I want to keep it simple.

Thanks.

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

1 participant