1
내 반응 구성 요소의 단위 테스트를 작성하려고합니다.내장 된 기능을 인식하지 못해서 반응 구성 요소를 테스트하지 못했습니다.
import React from "react";
require("../../../../css/ads/ads.css");
export class Ads extends React.Component {
render() {
return (
<div className="ads-container txt-flex-alignment">
<a id={"dummyAdsLink" + this.props.channel.removeAllSpaces().toLowerCase() + this.props.adsId}
href="#"
target="_top"><img
id={"dummyAdsImg" + this.props.channel.removeAllSpaces().toLowerCase() + this.props.adsId}
className="img-responsive" src={this.url}
/></a>
</div>
);
}
} 위의 코드 removeAllSpace에서
다음과 같이 내가 자바 스크립트 프로토 타입의 확장에 의해 생성 된 기능입니다 : 여기 내 구성 요소입니다 여기
String.prototype.removeAllSpaces = function() {
return this.replace(/\s+/g, '');
}
을 그리고 나의 테스트입니다 :
import React from 'react';
import expect from 'expect.js';
import {shallow} from 'enzyme';
import {Ads} from "../../src/app/components/story/ads/Ads";
import renderer from 'react-test-renderer';
describe('<Ads />',() => {
it('should render 1 <a /> components',() => {
const wrapper = shallow(<Ads channel="whatevername"/>);
expect(wrapper.find('a')).to.have.length(1);
});
});
이제 테스트를 실행할 때 다음 오류가 표시됩니다.
TypeError: this.props.channel.removeAllSpaces is not a function
분명히 내 removeAllSpaces 기능이 마음에 들지 않습니다. 어떤 생각입니까?
업데이트 : 나는 removeAllSpaces 모든 것을 당신이 파일을 테스트에 문자열 프로토 타입에 removeAllSpaces 방법을 추가 할 수있는 좋은