Giter VIP home page Giter VIP logo

Comments (7)

tofuness avatar tofuness commented on May 14, 2024

Assuming you're running on the latest version.

import { expect } from 'chai';
// ...
const store = mockStore({todos: []});
store.dispatch(actions.fetchTodos());
expect(store.getActions()).to.eql(expectedActions);

from redux-mock-store.

davidpelayo avatar davidpelayo commented on May 14, 2024

It also happened to me (with the old version), but it was actually my issue.

If you see, I was expecting to fetch an immutable structure (due we use a middleware which convert responses to immutable data), like follows:

function fetch() {
  return dispatch => {
    dispatch(basicActionCreator(types.fetch.start));
    return api.get(urls.myResourceExample)
      .then((response) => {
        dispatch(dataActionCreator(types.fetch.finish, response.get('myKeyExample')));
      }, (response) => {
        dispatch(errorActionCreator(types.fetch.fail, response));
      });
  };
}

However, while expecting actions, the response example I provided wasn't an immutable Map:

const response = [{ id: 0, name: 'Example 1' }];

beforeEach(() => {
  spyOn(api, 'get').andReturn(Promise.resolve(response));
});

afterEach(() => {
  expect.restoreSpies();
});

it(`should create action ${types.fetch.finish}`, (done) => {
  const expectedActions = [
    { type: types.fetch.start },
    { type: types.fetch.finish, data: response }
  ];

  const store = mockStore({}, expectedActions, done);
  store.dispatch(actions.fetch());
});

Producing the following error:

ERROR: 'Unhandled promise rejection', TypeError{line: 27, stack: '
run

flush'}

While resolving the promise, there is an unhandled exception trying to access to a key (because the action actually expects to have a Map as a response), producing the error above.

The solution was simple, just expecting the proper structure fixed my test:

const response = fromJS({ myExample: [{ id: 0, name: 'Example 1' }]});

beforeEach(() => {
  spyOn(api, 'get').andReturn(Promise.resolve(response));
});

afterEach(() => {
  expect.restoreSpies();
});

it(`should create action ${types.fetch.finish}`, (done) => {
  const expectedActions = [
    { type: types.fetch.start },
    { type: types.fetch.finish, data: response.get('myExample') }
  ];

  const store = mockStore({}, expectedActions, done);
  store.dispatch(actions.fetch());
});

UPDATE: Using the old version

from redux-mock-store.

Onionz avatar Onionz commented on May 14, 2024

@tofuness I used redux-mock-store 0.0.6 and nock 7.4.0 for testing the async action creators which is recommended in redux website. So you recommend to use the newest version?

from redux-mock-store.

arnaudbenard avatar arnaudbenard commented on May 14, 2024

I have updated the latest version last night. I need to make a PR on redux website for the new version. I would recommend migrating to it because it is framework agnostic :)

from redux-mock-store.

davidpelayo avatar davidpelayo commented on May 14, 2024

Checked and working with newest version. Thanks!

I think this issue can then be considered closed, right?

from redux-mock-store.

Onionz avatar Onionz commented on May 14, 2024

I will start the new version. Problem solved :D. Thanks for helping.

from redux-mock-store.

arnaudbenard avatar arnaudbenard commented on May 14, 2024

That's awesome 👍

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.