일부 코드를 단위 테스트에 React Test Utilities 사용하고 있습니다. renderIntoDocument
을 호출하여 사용자 지정 구성 요소를 렌더링 한 다음 findDOMNode
을 사용하여 렌더링 된 내용을 테스트합니다. 제가 실행하고있는 문제는 상태를 업데이트하고 단위 테스트의 컨텍스트 내에서 다시 렌더링을 효과적으로 트리거하는 방법을 모르겠다는 것입니다.React DOM을 사용하여 단위 테스트에서 상태 변경을 트리거하는 방법은 무엇입니까?
는 여기에 몇 가지 예제 코드입니다 - 코드 주석에주의 :
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-dom/test-utils';
import MyCustomComponent from '../../app/components/MyCustomComponent';
describe('My Test Suite',() => {
let component, node;
test('verify state change',() => {
const items = [{'value': '1'}];
component = TestUtils.renderIntoDocument(
<MyCustomComponent items={items} />
);
node = ReactDOM.findDOMNode(component);
expect(node.querySelector('input[type=text]').value).toEqual('1');
component.state.items = [{'value': '2'}];
// do something here to trigger a re-render?
expect(node.querySelector('input[type=text]').value).toEqual('2');
});
});
불행하게도 단순히 아무것도하지 않는 상태 변수를 변경 보인다. 그리고 정의되지 않은 것 같습니다 때문에 component.componentWillReceiveProps()
전화 할 수 없습니다.
동일한 구성 요소가 실제로 새로운 구성 요소로 대체되는 대신 해당 렌더링 기능을 호출하기를 원합니다. 그 이유는 구성 요소가 this.state
대신 this.props
대신에 버그를 발견했기 때문이며, 테스트에서는 항상 초기 값이 아닌 상태의 데이터를 사용하고 있음을 보여주기를 원합니다.
ReactDOM.render (, node)를 이미 사용해 보았습니까? 구성 요소를 강제로 렌더링 하시겠습니까? –
@MarouenMhiri는 컴포넌트의 새로운 인스턴스를 효과적으로 만들지 않을까요? (기존 인스턴스를 다시 렌더링하는 대신) – SoaperGEM
구성 요소를 다시 마운트하지 않고 업데이트합니다! 나는 이것이 당신이 찾고있는 것이라고 생각합니다. –