lundi 19 septembre 2016

proxyquire not stubbing method call

I'm trying to use proxyquire to replace a method call within a module I'm testing, but it is calling the method as is despite the fact I have the stub set up. What am I doing wrong?

formsReducer.test.js:

describe('Forms Reducer', () => {
    describe('types.UPDATE_PRODUCT', () => {
        it('should get new form blueprints when the product changes', () => {
            //arrange
            const initialState = {
                blueprints: [
                    {
                        id: 1,
                        categoryId: 1,
                        identifier: null,
                        name: "Investment Policy Statement",
                        sortGroup: 1,
                        wetSignRequired: false
                    }
                ]
            };
            const testBlueprints = [{ id: 999, categoryId: 1, name: "Form Blueprint Loaded From Product ID 1", sortGroup: 1, wetSignRequired: false }];
            //use proxyquire to stub call to formsHelper.getFormsByProductId 
            let formsReducer = proxyquire.noCallThru().load('./formsReducer', {
              formsHelper: {
                getFormsByProductId: id => { return testBlueprints }
              }
            }).default;
            const action = {
                type: types.UPDATE_PRODUCT,
                product: {
                    id: 1,
                    accountTypeId: 1,
                    officeRangeId: 1,
                    additionalInfo: "",
                    enabled: true
                },
            };
            //act
            const newState = formsReducer(initialState, action);
            //assert
            expect(newState.blueprints).to.be.an('array');
            expect(newState.blueprints).to.equal(testBlueprints);
        }); 
    });
});

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: []});
    }
}

formsHelper.getFormsByProductId is not returning testBlueprints as it should if it were properly stubbed via proxyquire. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire