Giter Site home page Giter Site logo

Comments (4)

bcolloran avatar bcolloran commented on June 11, 2024

Update: this definitely seems to be a tricky race condition kind of thing, because inserting a brief delay allows the tests to pass.

Here's an updated sandbox https://codesandbox.io/s/nifty-hofstadter-wnouo

And I'll put the updated passing code here as well for posterity:

import configureStore from "redux-mock-store";
import thunk from "redux-thunk";

const middlewares = [thunk]; // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares);

// You would import the action from your codebase in a real scenario
function success() {
  return {
    type: "FETCH_DATA_SUCCESS"
  };
}

function success_innerAsync() {
  return {
    type: "FETCH_DATA_SUCCESS--INNER_DISPATCH"
  };
}

function fetchData() {
  return async dispatch => {
    await fetch("/users.json"); // Some async action with promise
    dispatch(success());
    dispatch(fetchMoreData_innerAsync());
  };
}

function fetchMoreData_innerAsync() {
  return async dispatch => {
    await fetch("/more-data.json"); // Some async action with promise
    dispatch(success_innerAsync());
  };
}

async function wait(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}

it("should execute fetch data", async () => {
  const store = mockStore({});

  // Return the promise
  await store.dispatch(fetchData());
  await wait(1000);
  const actions = store.getActions();
  expect(actions).toEqual([success(), success_innerAsync()]);
});

from redux-mock-store.

landonalder avatar landonalder commented on June 11, 2024

I'm having this same issue, but the wait makes no difference for me. Redux mock store version is 1.5.4 and react-redux version is 7.1.3. Any additional async actions that are dispatched inside an action creator are never called

Edit: I actually figured out the reason that my async thunk that was dispatched inside the async action I was testing wasn't getting called because there was an exception inside it. Sadly, there's no indication of this except for when you call store.getActions(), I saw an error action that was dispatched with the error message.

from redux-mock-store.

garyee avatar garyee commented on June 11, 2024

@landonalder thanks!! I had the same error and after reading your comment about the error not showing. I search for and discovered the error. It is working now!

from redux-mock-store.

landonalder avatar landonalder commented on June 11, 2024

@garyee glad to hear this helped :)

from redux-mock-store.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.