使用DeepFreeze for Redux对象测试突变

时间:2017-04-24 20:12:19

标签: redux immutable.js deepfreeze

我有一个简单的Reducer

const uid = () => Math.random().toString(34).slice(2);
const bots = (state = [] , action) => {

    switch(action.type) {    
      case 'ADD_BOT':

          return [
              ...state, {
                    id: uid(),
                    isDone: false,
                    text: action.bots.text
              }
          ]

          //this will fail
     case 'ADD_BOT_THAT_MUTATES':
          console.log("mutating");
          action.bots.id = uid();
          state.push(action.bots);
          return state;

      default:
        return state;
  }
}
export default bots

我的spec文件是

import deepFreeze from 'deep-freeze';
import bots from '../bots';

describe('Simple test', () =>  {

 function addBot(text) {
  return {
    type: 'ADD_BOT',
    bots: {
      id: 1,
      isDone: false,
      text: text
    }
  };
 }

 function addBotThatMutates(text) {
  return {
    type: 'ADD_BOT_THAT_MUTATES',
    bots: {
      id: 1,
      isDone: false,
      text: text
    }
  };
 }

  let state = []; 
  deepFreeze(state); 

  beforeEach(() => {

        state = bots(state, addBot("initial"));

  });

    it('should fail due to deepFreeze', () => {

        //create a payload
        let payload = addBot("test 1234");
        let payloadThatMutates = addBotThatMutates("test 5678");

        state = bots(state, payload);

        state = bots(state, payloadThatMutates);

        expect(3).toEqual(state.length);
    });
});

当我用state = bots(state, payload);调用Reducer时,我希望它返回一个非变异数组,因为我在Reducer中使用了ES6传播语句。

当我致电state = bots(state, payloadThatMutates);时,我期待一个由deepFreeze标记的错误。这是因为在Reducers中我使用state.push(action.bots);,我理解它会变异。

但我没有得到任何错误,我的结果状态是3个对象的数组。

我是否有不正确的Reducers或者我不理解deepFreeze?

此单元测试无法正常工作。但是如果我调用'ADD_BOT_THAT_MUTATES操作而减速器没有更新状态,即Redux改变状态,我的App / Web代码就可以工作。

或者我刚做过一些愚蠢的事情?

1 个答案:

答案 0 :(得分:1)

自发布以来,我已经设法让我的头脑深入了解了DeepFreeze和突变 以下是我给出预期结果的两个测试

import deepFreeze from 'deep-freeze';
import bots from '../bots';

describe('Simple test', () =>  {

 function addBot(text) {
  return {
    type: 'ADD_BOT',
    bots: {
      id: 1,
      isDone: false,
      text: text
    }
  };
 }

 function addBotThatMutates(text) {
  return {
    type: 'ADD_BOT_THAT_MUTATES',
    bots: {
      id: 1,
      isDone: false,
      text: text
    }
  };
 }

  let state; 

  beforeEach(() => {        
    state = []
        state = bots(state, addBot("initial"));  
  });

    it('should pass due to non-muting reducer ', () => {
        //create a payload
        let payload = addBot("test 1234");
        let state2 = bots(state, payload);
        //state  has non mutated and state2 is a new array
         expect(state.length).toEqual(1);
         expect(state2.length).toEqual(2);
    });

  it('should fail due to deepFreeze', () => {
          deepFreeze(state); 
          //create a payload
          let payloadThatMutates = addBotThatMutates("test 5678");
          //deepFreeze will throw 'object is not extensible' because state is now mutating because of the push in the the reducer
          let state2 = bots(state, payloadThatMutates);          
          expect(state).toEqual(state2);
    });

});

希望这有助于任何人