我尝试使用proxyquire
对我的Redux缩减器进行单元测试。我需要在测试中替换一个函数的功能,但保留另一个函数的原始功能,这可以根据proxyquire's
文档进行。
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: []});
}
}
我需要替换formsHelper.getFormsByProductId()
的功能,但保留formsHelper.addOrRemoveMnDisclosure()
的原始功能 - 正如您在proxyquire
块中看到的那样,我只替换{{1}功能。但是,当我这样做时会收到以下错误:getFormsByProductId()
。对于使用babel或使用我的TypeError: _FormsHelper2.default.addOrRemoveMnDisclosure is not a function
进行FormHelper看起来是一个问题。
FormsHelper的导出如下所示:
export default
。
如何解决此问题?