Enzyme/Jest를 사용하여 테스트하려고하는 React 구성 요소가 있습니다. 구성 요소가 렌더링되었는지 확인하는 데 가장 적합한 테스트가 무엇인지 알아 내려고 노력 중입니다.React 구성 요소가 렌더링되었는지 테스트합니다.
내 구성 요소에는 shouldRender
이라는 단 어가 있으며, false 일 경우 구성 요소가 렌더링되지 않습니다. 내 구성 요소는 다음과 같습니다 : 구성 요소가 렌더링되지 않는,
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from '../MyComponent';
describe('MyComponent',() => {
it('Should render if we want it to',() => {
const component = shallow(<MyComponent shouldRender />);
expect(component).toBeDefined(); // Passes
});
it('Should not render if we do not want it to',() => {
const component = shallow(<MyComponent />);
expect(component).not.toBeDefined(); // Does not pass, as component isn't undefined.
});
});
내가 두 번째 테스트가 실패하고 싶습니다 :이처럼 보이는 검사를
import React from 'react';
const propTypes = {
shouldRender: React.PropTypes.bool,
};
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
foo: 'bar',
};
}
render() {
if (!this.props.shouldRender) {
return null;
}
return (
<div>
<span>My component</span>
</div>
);
}
}
MyComponent.propTypes = propTypes;
export default MyComponent;
. 구성 요소가 렌더링되었는지 여부를 테스트하는 더 좋은 방법이 있습니까?
필요한 경우 더 많은 정보를 제공해주기 바랍니다.
감사합니다.
예고. 'render'에서 undefined를 반환하는 문서는 유효한 옵션이 아닙니다. "렌더링 된 것을 원하지 않는다는 것을 나타 내기 위해'null' 또는'false'를 반환 할 수도 있습니다." –
Woops - 좀 더 단순화 된 버전을 위해 현장에서 손으로 구성 요소를 작성했습니다. 코드를 수정합니다 지금. 감사! –
그런데'isEmpty' 메소드가 있습니다. https://github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/isEmpty.md –