这是我的测试组件:
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import {configure, shallow} from "enzyme";
import CardEndpoint from "../../../components/cards/CardEndpoint";
configure({adapter: new Adapter()});
describe("<CardEndPoint />", () => {
let wrapper;
beforeAll(() => {
wrapper = shallow(<CardEndpoint />);
});
it("should add slash in front of an 'slash-less' endpoint", () => {
let instance = wrapper.instance();
let expectedEndpoint = '/house/:houseNumber';
let testEndpoint = 'house/:houseNumber';
let slashCheck = instance.checkForBeginSlash(testEndpoint);
expect(slashCheck).toEqual(expectedEndpoint);
});
});
由于赋予'instance.checkForBegingSlash( testEndpoint )'的参数' testEndpoint '未定义,出现以下错误。
TypeError: Cannot read property 'startsWith' of undefined
11 | const position = 0;
12 |
> 13 | if (URI.startsWith(slash)) {
| ^
14 | return URI
15 | } else {
16 | return [URI.slice(0, position), slash, URI.slice(position)].join('');
这是我正在测试的实际功能,其中参数 URI 未定义:
checkForBeginSlash = (URI) => {
const slash = '/';
const position = 0;
if (URI.startsWith(slash)) {
return URI
} else {
return [URI.slice(0, position), slash, URI.slice(position)].join('');
}
};
为什么我的测试没有通过参数?
我要求的整个CardEndPoint组件:
import React from 'react';
import ListGroup from 'react-bootstrap/ListGroup'
import Container from 'react-bootstrap/Container'
import Row from 'react-bootstrap/Row'
import Col from 'react-bootstrap/Col'
class CardEndpoint extends React.Component {
checkForBeginSlash = (URI) => {
const slash = '/';
const position = 0;
if (URI.startsWith(slash)) {
return URI
} else {
return [URI.slice(0, position), slash, URI.slice(position)].join('');
}
};
organizeParams = (params) => {
if (params) {
return Object.values(params).join(', ')
} else {
return 'No parameters detected.'
}
};
render = () => {
return (<React.Fragment>
<ListGroup.Item className={`card--${this.props.method} no-padding`}>
<Container variant="fluid">
<Row>
<Col
className={`card__method--${this.props.method} d-flex justify-content-center align-items-center`}
lg="2" data-testid="createdCardMethod">
{this.props.method}
</Col>
<Col className="card__endpoint__url" lg="10" data-testid="createdCardURL">
{this.checkForBeginSlash(this.props.URI)}
</Col>
</Row>
</Container>
</ListGroup.Item>
<ListGroup.Item className={`card--${this.props.method} no-padding`}>
<Container variant="fluid">
<Row>
<Col>
<b>
Query params:
</b>
{this.organizeParams(this.props.filteredQueryParams)}
</Col>
</Row>
<Row>
<Col>
<b>
Path params:
</b>
{this.organizeParams(this.props.filteredPathParams)}
</Col>
</Row>
</Container>
</ListGroup.Item>
</React.Fragment>
);
};
}
export default CardEndpoint;
答案 0 :(得分:0)
您只需要在测试中将URI属性传递给CardEndpoint
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, shallow } from 'enzyme';
import CardEndpoint from './CardEndpoint';
configure({ adapter: new Adapter() });
describe('<CardEndPoint />', () => {
let wrapper;
beforeAll(() => {
wrapper = shallow(<CardEndpoint URI="" />);
});
it("should add slash in front of an 'slash-less' endpoint", () => {
let instance = wrapper.instance();
let expectedEndpoint = '/house/:houseNumber';
let testEndpoint = 'house/:houseNumber';
let slashCheck = instance.checkForBeginSlash(testEndpoint);
expect(slashCheck).toEqual(expectedEndpoint);
});
});