Comments (9)
What specifically do you want to test in it? Control flow? Would you mock network request or let them happen?
from redux-thunk.
The network request would get mocked. And I suppose I'm looking to test control flow; when the action is called, does it a: return the expected function and b: does that function do what is expected of it.
I'm wondering now if that's an acceptable test or not; it seems like it could be brittle.
from redux-thunk.
I ended up (crudely) coming up with the following solution: https://gist.github.com/kwhitaker/f0d8624af9061f3bd8a9
The setTimeout
calls are ugly, but they work.
from redux-thunk.
You don't need setTimeout
, use store.subscribe()
.
reduxjs/redux#546 (comment)
from redux-thunk.
@kwhitaker How did you mock network requests in this case? Did you dispatch an action yourself, simulating async action dispatch which has the response object?
from redux-thunk.
@salmanm I used Jasmine AJAX to handle that:
let request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toBe(`${BSPACE_URL}/connectors/builds`);
request.respondWith({
status: 200,
contentType: 'application/json',
responseText: JSON.stringify(testBuilds)
});
from redux-thunk.
I am testing my react-component and check that it calls dispatch in an appropriate place.
To check that my action creator returned the proper function, I use named function and check its name:
//action creator
export function fetchData(query) {
return function fetchDataAction(dispatch) {/*some loading logic here*/}
}
//dispatch
onLoadClick() {
this.props.dispatch(fetchData(this.props.query));
}
//test
const dispatch = jasmine.createSpy();
const component = renderIntoDocument(<Component dispatch={dispatch} />);
Simulate.click(findRenderedDOMComponentWithClass(component, 'load-button'));
expect(dispatch).toHaveBeenCalledWith(jasmine.objectContaining({name: 'fetchDataAction'}))
function.name
is used here as a kind of type
property for plain action objects. Am I doing this right? Can this be considered as a common practice?
from redux-thunk.
@just-boris
Ideally, I would test some changes in the state or spy some underlying library to check if the call has been made using the correct query
parameter.
For example, I had one lib containing all util functions which take care of ajax calls, I import
ed that and stubbed callAPI
function.
Testing against function name doesn't look very useful. Because the test would pass as long as the function name is same even if the the function behaviour has changed, which should ideally fail the test.
from redux-thunk.
@salmanm
I want to keep my test on components clear from side-effects. Of course, I have tests with store and action create logic, but they are separated from tests with React components.
I did it because it is easier to test isolated store and its action handling logic, including ajax calls.
My point was in that it is nice to have mocked dispatch
for React-components and just check that a component calls dispatches in the proper order.
UPD: there is an example of my tests:
from redux-thunk.
Related Issues (20)
- Why `redux` module extension is not part of index.d.ts file in package? HOT 1
- Passing `dispatch` instead of calling it.
- Empty payload in PENDING state HOT 1
- Why not next? HOT 1
- types wrong by infer in TS HOT 1
- No type hints when using dispatch HOT 4
- import 'redux-thunk/extend-redux' doesn't work HOT 12
- When running server getting expected errors HOT 2
- ThunkAction is not assignable to parameter of type 'AnyAction'. HOT 46
- Current implemention doesn't allow Jest or any other mock tool to spy on dispatch or getState HOT 2
- TS do not show the error if payload is Partial<> HOT 1
- Can I abort redux thunk dispatches? Without Redux toolkit or aborting the fetch request itself. HOT 1
- Allow wrapped thunk actions for extensibility HOT 2
- When type: "module" is used in root package.json, redux-thunk cannot be applied without crashing HOT 8
- Warning in React-thunk while installing HOT 1
- Query About Redux Thunk HOT 1
- `thunk` as a named export? HOT 3
- redux thunk cannot be use as middleware using Typescript 5.0 or above HOT 1
- during integrating thunk into react-redux project, getting middleware is not a function error. HOT 1
- No default export for redux-thunk HOT 1
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-thunk.