1

I have an arrow function in my React-Redux application that dispatches just an action without a payload, which will wipe (Reset) a part of my Redux store. There's no HTTP (or Promises) involved.

export const resetSearch = () => dispatch => {
  dispatch({
    type: RESET_SEARCH
  });
};

I want to test that it returns the correct Action type:

import * as actions from '../actions/actionCreators';
import * as types from '../actions/actionTypes';

describe('actions', () => {
  it('should create an action to reset part of the store', () => {
    const expectedAction = {
      type: types.RESET_SEARCH
    };
    expect(actions.resetSearch()).toEqual(expectedAction);
  });
});

The test is not passing because I need to return an Object but, instead, I send this anonimous arrow function. Here is the Jest output

Expected value to equal: {"type": "RESET_SEARCH"}

Received: [Function anonymous]

How should the test be? All help will be apreaciated!

Thanks

1
  • Call the returned function with an implementation of dispatch that returns the dispatch's parameter? I mean, you're not returning an object, you're returning a function. I'm not sure why, but that's why it doesn't work. Commented Jun 10, 2018 at 18:17

1 Answer 1

3

Can you plese try below code snippet, It should do:

const expectedAction = {
  type: types.RESET_SEARCH
};

let retnFunc = actions.resetSearch();
retnFunc((receivedAction)=>{
    expect(receivedAction).toEqual(expectedAction);  
});

Here resetSearch returns a function which gets called with the action object so just imitated the same.

Let me know if you need any more help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.