proxyquire
Redux レデューサーの単体テストに使用しようとしています。テストで 1 つの関数の機能を置き換える必要がありますが、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()
でわかるように、機能を置き換えるだけです。ただし、これを行うと、次のエラーが発生します。BabelまたはFormHelperのmyに問題があるようです。proxyquire
getFormsByProductId()
TypeError: _FormsHelper2.default.addOrRemoveMnDisclosure is not a function
export default
FormsHelper のエクスポートは次のようになります。
export default class FormsHelper { ...methods and whatnot }
.
この問題を解決するにはどうすればよいですか?