1

I'm trying to use proxyquire to unit test my Redux reducers. I need to replace the functionality of one function in my test but keep the original functionality of the other, which is possible according to proxyquire's docs.

formsReducer.test.js:

import { expect } from 'chai';
import * as types from '../constants/actionTypes';
import testData from '../data/TestData';
import proxyquire from 'proxyquire';
describe('Forms Reducer', () => {
    describe('types.UPDATE_PRODUCT', () => {
        it('should get new form blueprints when the product changes', () => {
            //arrange
            const initialState = {
                blueprints: [ testData.ipsBlueprint ],
                instances:  [ testData.basicFormInstance ]
            };
            //use proxyquire to stub call to formsHelper.getFormsByProductId
            const formsReducerProxy = proxyquire.noCallThru().load('./formsReducer', {
              '../utils/FormsHelper': {
                getFormsByProductId: () => { return initialState.blueprints; }
              }
            }).default;
            const action = {
                type: types.UPDATE_PRODUCT,
                stateOfResidence: testData.alabamaObject,
                product: testData.basicProduct
            };
            //act
            const newState = formsReducerProxy(initialState, action);
            //assert
            expect(newState.blueprints).to.be.an('array');
            expect(newState.blueprints).to.equal(initialState.blueprints);
        }); 
    });
});

formsReducer.js:

import * as types from '../constants/actionTypes';
import objectAssign from 'object-assign';
import initialState from './initialState';
import formsHelper from '../utils/FormsHelper';
export default function formsReducer(state = initialState.forms, action) {
  switch (action.type) {
    case types.UPDATE_PRODUCT: {
        let formBlueprints = formsHelper.getFormsByProductId(action.product.id);
        formBlueprints = formsHelper.addOrRemoveMnDisclosure(formBlueprints, action.stateOfResidence.id);
        return objectAssign({}, state, {blueprints: formBlueprints, instances: []});
    }
}

I need to replace the functionality of formsHelper.getFormsByProductId() but keep the original functionality of formsHelper.addOrRemoveMnDisclosure() - as you can see in the proxyquire block I'm only replacing the getFormsByProductId() function. However, when I do this get the following error: TypeError: _FormsHelper2.default.addOrRemoveMnDisclosure is not a function. Looks to be a problem either with babel or with my export default for FormHelper.

The export for the FormsHelper looks like this:

export default class FormsHelper { ...methods and whatnot }.

How can I fix this problem?

kibowki
  • 4,206
  • 16
  • 48
  • 74

0 Answers0