reactjs React测试库没有检测到我的title标记,但它实际上存在

aurhwmvo  于 2023-01-12  发布在  React
关注(0)|答案(2)|浏览(151)

基本上,我试图检查当所有组件呈现时,屏幕中是否有一个"过滤器"标题,但由于某种原因,React测试库没有检测到它,它只是向我抛出了这个错误。
我会让这里所有的代码和图像的错误.

FilterBox.js

import React from 'react';
import './FiltersBox.css';

function FiltersBox({ onSortSelected, onCategorySelected }) {
    function sortByPrice(e) {
        onSortSelected(e.target.value);
    }

    function sortByCategory(e) {
        onCategorySelected(e.target.value);
    }

    return (
        <div className='items_container-filter_box'>
            <h3 className='filter_box-title'>Filters</h3>

            <div className='filter_box-filters_container'>
                <div className='filter_box-filter filter_box-first_filter'>
                    <h5>By Price:</h5>

                    <div className='filter_box-select'>
                        <select onChange={sortByPrice}>
                            <option value='none'>-</option>
                            <option value='ascending'>Ascending</option>
                            <option value='descending'>Descending</option>
                        </select>
                    </div>
                </div>

                <div className='filter_box-filter filter_box-second_filter'>
                    <h5>By Category:</h5>

                    <div className='filter_box-select'>
                        <select onChange={sortByCategory}>
                            <option value='none'>-</option>
                            <option value="men's clothing">Men&apos;s Clothing</option>
                            <option value="women's clothing">Women&apos;s Clothing</option>
                            <option value='jewelery'>Jewelery</option>
                            <option value='electronics'>Electronics</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    );
}

MainSection.test.js

import React from 'react';
import { render, screen } from '@testing-library/react';
import MainSection from './MainSection.js';

beforeAll(() => {
    render(<MainSection />);
});

test('Check if renders the Search Bar', () => {
    const textInput = screen.getByTestId('text-input');

    expect(textInput).toBeInTheDocument();
    expect(textInput).toHaveAttribute('type', 'text');
    expect(textInput).toHaveAttribute('placeholder', 'Search here!');
});

test('Check if renders the Filters Box', () => {
    const filterTitle = screen.getByText(/Filters/i);

    const filterLabelByPrice = screen.getByText(/by price:/i);
    const filterLabelByCategory = screen.getByText(/by category:/i);

    const filtersComboBoxes = screen.getAllByRole('combobox');

    expect(filterTitle).toBeInTheDocument();

    expect(filterLabelByPrice).toBeInTheDocument();
    expect(filterLabelByCategory).toBeInTheDocument();

    expect(filtersComboBoxes).toHaveLength(2);
});

我什么都试过了,但没有成功.

    • 更新日期:**

我尝试了@nbjorling的用户解决方案。我想它现在工作了...因为它仍然给我错误,但两个测试都通过了(绿色)。

xzv2uavs

xzv2uavs1#

也许您实际上并不呈现Filter组件?
如果你尝试这个会发生什么。

beforeAll(() => {
    render(
      <MainSection>
          <FiltersBox />
      <MainSection>
    );
});

或者尝试像这样设置它(使用正确的导入ofc):

test("Check if renders the Filters Box", () => {
  render(<FiltersBox />);

  const filterTitle = screen.getByText(/Filters/i);
  const filterLabelByPrice = screen.getByText(/by price:/i);
  const filterLabelByCategory = screen.getByText(/by category:/i);
  const filtersComboBoxes = screen.getAllByRole("combobox");

  expect(filterTitle).toBeInTheDocument();
  expect(filterLabelByPrice).toBeInTheDocument();
  expect(filterLabelByCategory).toBeInTheDocument();
  expect(filtersComboBoxes).toHaveLength(2);
});
mpbci0fu

mpbci0fu2#

我认为在所有组件之前只渲染一次不会使测试彼此分离。
const filterTitle = screen.getByText(/Filters/i);之前使用debug();,它将给予您了解屏幕上呈现的内容。
或尝试其他选择器尝试使用getByRole('heading', {name: "Filter"});
我希望,它会有帮助!

相关问题