GithubHelp home page GithubHelp logo

Comments (9)

gaearon avatar gaearon commented on September 7, 2024

What specifically do you want to test in it? Control flow? Would you mock network request or let them happen?

from redux-thunk.

kwhitaker avatar kwhitaker commented on September 7, 2024

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.

kwhitaker avatar kwhitaker commented on September 7, 2024

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.

gaearon avatar gaearon commented on September 7, 2024

You don't need setTimeout, use store.subscribe().
reduxjs/redux#546 (comment)

from redux-thunk.

salmanm avatar salmanm commented on September 7, 2024

@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.

kwhitaker avatar kwhitaker commented on September 7, 2024

@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.

just-boris avatar just-boris commented on September 7, 2024

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.

salmanm avatar salmanm commented on September 7, 2024

@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 imported 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.

just-boris avatar just-boris commented on September 7, 2024

@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)

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.