小编Cep*_*has的帖子

如何测试组件是否具有属性?

我用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/')
  });

});
Run Code Online (Sandbox Code Playgroud)

// 类型.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>
    )
  } …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs react-testing-library

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

jestjs ×1

react-testing-library ×1

reactjs ×1