lundi 31 octobre 2016

Jest - testing redux actions

I'm trying to test my app's redux actions using Jest. I've looked at the testing site @ redux.js.org and other forums posts but I can't see why I'm getting the error I am, I think I'm not getting how the mocking works.

My action is in index.js:

export const setResponse = (res) => {
    return {
        type: 'RESPONSE_RECEIVED',
        payload: res
    }
};

My test is actions.test.js:

import * as actions from '../src/client/scripts/actions/index'

describe('actions', () => {
  it('should create an action to receive a response', () => {
    const payload = {response: 'solr_json'};
    const expectedAction = {
      type: 'RESPONSE_RECEIVED',
      payload
    }
    expect(actions.setResponse(payload)).toEqual(expectedAction)
  })
});

I got the error 'Expected undefined to equal Object...' soI thought actions.setResponse didn't exist, so I printed out actions.setResponse and it looks like this:

{ [Function: setResponse]
  _isMockFunction: true,
  getMockImplementation: [Function],
  mock: { calls: [], instances: [] },
  mockClear: [Function],
  mockReturnValueOnce: [Function],
  mockReturnValue: [Function],
  mockImplementationOnce: [Function],
  mockImpl: [Function],
  mockImplementation: [Function],
  mockReturnThis: [Function] }

So it seems the function is there, how do I actually call the function inside the Jest test? Even if I change

export const setResponse = (res) =>

to

export function setResponse(res)

(so it's the same format of the redux.js.org tutorial) I get the same error. For reference this is the jest part of my package.json:

"jest": {
    "scriptPreprocessor": "node_modules/babel-jest",
    "testFileExtensions": [
      ".test.js"
    ],
    "testPathDirs": [
      "__tests__"
    ],
    "moduleFileExtensions": [
      "js"
    ],
    "unmockedModulePathPatterns": [
      "node_modules/react",
      "node_modules/react-addons-test-utils",
      "node_modules/react-dom"
    ]
  }

Thanks for any help

Aucun commentaire:

Enregistrer un commentaire