使用DeepFreeze测试Redux对象的突变

使用DeepFreeze测试Redux对象的突变,redux,immutable.js,deepfreeze,Redux,Immutable.js,Deepfreeze,我有一个简单的减速机 const uid = () => Math.random().toString(34).slice(2); const bots = (state = [] , action) => { switch(action.type) { case 'ADD_BOT': return [ ...state, { id: uid(),

我有一个简单的减速机

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
我的规范文件是

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 spread语句时,我希望它返回一个未变异的数组

当我调用
state=bots时(state,payloadthattmutates)我期待一个由deepFreeze标记的错误。这是因为我在reducer中使用的是
state.push(action.bots)我知道它会变异

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

我是否有不正确的减速器或我是否不了解deepFreeze

这个单元测试没有像我预期的那样工作。但是我的应用程序/网络代码可以工作,如果我调用“ADD_BOT_,它改变了操作,而Reducer我没有得到更新的状态,即Redux改变了状态


还是我只是做了一些愚蠢的事情?

发帖后,我一直在设法让自己的大脑绕过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);
    });

});
希望这对任何人都有帮助