1

我有一个简单的减速器

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);
    });
});

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

当我打电话时, state = bots(state, payloadThatMutates);我期待 deepFreeze 标记的错误。这是因为在我使用的减速器中 state.push(action.bots);,我理解它会发生变异。

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

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

这个单元测试没有像我预期的那样工作。但是,如果我调用 'ADD_BOT_THAT_MUTATES 操作并且 Reducer 我没有得到更新的状态,即 Redux 改变了状态,我的 App/Web 代码就可以工作。

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

4

1 回答 1

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);
    });

});

希望这对任何人都有帮助

于 2017-04-24T21:29:41.443 回答