【发布时间】:2021-09-17 13:25:36
【问题描述】:
我用testing-library
如果您有语法解决方案,我无法定位元素,
我正在尝试检索标签并比较xlinkHref。
我可以获得整个组件,但没有类名或 ID,这让我很困扰
// Type.test.jsx
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import Type from '../index';
describe('Type', () => {
it('Watch Type', () => {
const {container} = render(<Type type="Watch Type" />);
expect(container.firstElementChild.getAttribute('xlinkHref')).toHaveAttribute('xlinkHref', 'https://www.test.com/')
});
});
// Type.jsx
import React from 'react';
const Type = (props) => {
const type = props.type;
if (type === 'Watch Type') {
return (
<span className="platform-icon">
<svg>
<use xlinkHref="https://www.test.com/" />
</svg>
</span>
)
}
}
export default App;
【问题讨论】:
标签: reactjs jestjs react-testing-library