【发布时间】:2023-01-11 00:15:40
【问题描述】:
基本上我试图检查当所有组件都被渲染时,屏幕上是否有一个“过滤器”标题,但由于某种原因 React Testing Library 没有检测到它并且它只是抛出那个错误。
我将在此处提供所有代码和错误图像。
过滤框.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's Clothing</option>
<option value="women's clothing">Women'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 当然 :)
-
如果你评论那个,你会卡在下一个还是它能找到那个?
-
@nbjorling 哦......它给了我与其他标题相同的错误。但只能在“Filters Box”中测试。
标签: reactjs testing jestjs react-testing-library