Comments (7)
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.
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.
@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.
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.
Checked and working with newest version. Thanks!
I think this issue can then be considered closed, right?
from redux-mock-store.
I will start the new version. Problem solved :D. Thanks for helping.
from redux-mock-store.
That's awesome 👍
from redux-mock-store.
Related Issues (20)
- [NPM] - ES Modules are not released on 1.5.3 HOT 3
- This repo still alive? HOT 13
- Actions dispatched after using Promise.all don't save to store HOT 2
- Configuring store for all React tests HOT 1
- Cannot read property 'enqueueForceUpdate' of undefined
- Docs link is not working the NPM repo HOT 3
- Testing nested actions HOT 2
- Test with extra dispatched action in middleware
- update url's in package.json to point to latest location HOT 2
- What is in 1.5.4? HOT 1
- Issue with Store Helpers HOT 1
- The Documentation is wrong for CommonJS HOT 1
- does not work with nested async thunks? HOT 4
- Version 1.5.4? HOT 5
- [Redux-toolkit] [createEntityAdapter] Cannot read property 'ids' of undefined HOT 1
- getActions method doesn't log correct actions
- Usage with createAsyncThunk - 'await' has no effect on the type of this expression. HOT 1
- clearActions method isn't working as expected
- ESM problem with default export
- Add note that this is no longer recommended or maintained?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-mock-store.