【问题标题】:Confusion after setting a property of a Map设置地图属性后的困惑
【发布时间】:2016-06-02 00:04:12
【问题描述】:

我正在阅读和练习教程:http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html 但我无法通过减速器测试。然后我做了一个新的测试来找出原因。

这是 test.js(mocha with chai):

import {expect} from 'chai';
import {List, Map, fromJS} from 'immutable';

describe('set', () => {
  it('sets map\'s property', () => {
    const state = Map();
    const action = {type: 'SET_ENTRIES', entries: ['Trainspotting']};
    const nextState = state.set('entries', action.entries);

    /*
    expect(nextState).to.equal(Map({
      entries: ['Trainspotting']
    }));
    */
    expect(nextState).to.equal(fromJS({
      entries: ['Trainspotting']
    }));
  });
});

无论是使用Map还是fromJS,测试都没有通过

如果选择“地图”,则显示:

  AssertionError: expected 'Map { "entries": Trainspotting }' to equal 'Map { "entries": Trainspotting }'
  + expected - actual

如果选择“fromJS”,则显示:

  AssertionError: expected 'Map { "entries": Trainspotting }' to equal 'Map { "entries": List [ "Trainspotting" ] }'
  + expected - actual

  -Map { "entries": Trainspotting }
  +Map { "entries": List [ "Trainspotting" ] }

这真是令人困惑。如何让它发挥作用?

【问题讨论】:

    标签: reactjs mocha.js immutability redux immutable.js


    【解决方案1】:

    你试过了吗

    const nextState = state.set('entries', List(action.entries));
    

    【讨论】:

    • 我刚才试过了,正如你所说的第二个“expect”语句(fromJS)。有用。但为什么?这是否意味着我上面提到的教程是错误的?
    • 当你像这样设置你的条目时:state.set('entries', action.entries),action.entries 是一个简单的数组而不是一个不可变列表。另一方面,您无法将其与 fromJS({ entries: ['Trainspotting'] }) 进行比较。它将创建条目的 List 实例。这就是 fromJS() 的威力
    • 这是有道理的。非常感谢!
    猜你喜欢
    • 2020-02-23
    • 1970-01-01
    • 2012-02-27
    • 2016-08-09
    • 1970-01-01
    • 2020-04-13
    • 2018-05-05
    • 2014-02-01
    • 1970-01-01
    相关资源
    最近更新 更多