Функция обновления, вызываемая updateIn (), возвращает неизменяемый объект вместо значения - PullRequest
0 голосов
/ 22 ноября 2018

У нас есть реактивная установка, в которой в качестве основы используются ImmutableJS, Jest и Enzyme.

Мы столкнулись с проблемой при тестировании редуктора с помощью ImmutableJS updateIn () .

Страница работает нормально, но мы не можем проверитькак и в тесте редуктора, функция неизменяемого средства обновления получает неизменяемый объект, но мы ожидаем, что обычный объект JS будет работать с ним.

reducer.js:

case ACTION_TYPE: {

  const targetSectionId = action.sectionId;
  return state.updateIn(['allSections'], (allSections) =>

    allSections.map((section) => {

      // section is wrapped in immutable when testing
      // on regular execution, section.get() will fail
      // as it is plain javascript object
      const updatedSection = Object.assign({}, section);
      const sectionIdMatched = updatedSection.sectionId === targetSectionId;

      if (sectionIdMatched) {
        // on tests, does not get here
        // on regular execution, we get here
        updatedSection.commonSectionNames = action.commonSections;
        return updatedSection;
      }
      return updatedSection;
    })

  );

}

=============

reducer.test.js:

it('should handle the action correctly', () => {
  // arrange

  const sectionId = 15;
  const valueToSet = ['fixture'];
  const initialState = fromJS({
     allSections: [{ sectionId: 15, commonSectionNames: [] }]
  });
  const expectedState = fromJS({
     allSections: [{ sectionId: 15, commonSectionNames: valueToSet }] 
  });


  // act
  const result = reducer(initialState, action(sectionId, valueToSet));


  // assert
  expect(result).toEqual(expectedState);
});

============

Любой совет с этим приветствуется.

1 Ответ

0 голосов
/ 13 марта 2019

Так оно и есть.Функция неизменяемого обновления получит неизменный объект.Если вам нужен объект plainJS, используйте toJS () , например:

allSections.map((tempSection) => {
    const section = tempSection.toJS();
// perform operations with plainJS "section" here
...