Chcąc wykonać test, wyświetla się powiadomienie o błędzie:
● Search Bar › Displays when search phrase is matching
Unable to find an element with the text: /element/. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
Ignored nodes: comments, script, style
<body>
<div />
<div
aria-live="polite"
aria-relevant="additions text"
id="a11y-status-message"
role="status"
style="border: 0px; height: 1px; margin: -1px; overflow: hidden; padding: 0px; position: absolute; width: 1px;"
/>
</body>
22 | const input = screen.getByPlaceholderText('Search');
23 | fireEvent.change(input, { target: { value: 'el' } });
> 24 | await screen.findByText(/element/);
| ^
25 | });
at waitForWrapper (node_modules/@testing-library/dom/dist/wait-for.js:163:27)
at findByText (node_modules/@testing-library/dom/dist/query-helpers.js:86:33)
at Object.<anonymous> (src/components/organisms/SearchBar/SearchBar.test.js:24:18)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 2.692 s
Ran all test suites related to changed files.
Watch Usage: Press w to show more.
Oto test:
import { render, screen, fireEvent } from 'test-utils';
import { SearchBar } from './SearchBar';
import { handlers } from 'mocks/handlers';
import { setupServer } from 'msw/node';
export const server = setupServer(...handlers);
describe('Search Bar', () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
it('Displays when search phrase is matching', async () => {
render(<SearchBar />);
const input = screen.getByPlaceholderText('Search');
fireEvent.change(input, { target: { value: 'ad' } });
await screen.findByText(/Adam Romański/);
});
});
Oraz komponent, do którego odwołuję się w teście:
import { Input } from 'components/atoms/Input/Input.styles';
import React, { useState } from 'react';
import debounce from 'lodash.debounce';
import {
SearchBarWrapper,
SearchResults,
SearchResultsItem,
SearchWrapper,
} from 'components/organisms/SearchBar/SearchBar.styles';
import { useElements } from 'hooks/useElements';
import { useCombobox } from 'downshift';
export const SearchBar = () => {
const [matchingElements, setMatchingElements] = useState([]);
const { findElements } = useElements();
const getMatchingStudents = debounce(async ({ inputValue }) => {
const elements = await findElements(inputValue);
setMatchingElements(elements);
}, 500);
const {
isOpen,
getMenuProps,
getInputProps,
getComboboxProps,
highlightedIndex,
getItemProps,
} = useCombobox({
items: matchingElements,
onInputValueChange: getMatchingElements,
});
return (
<SearchBarWrapper>
<SearchWrapper {...getComboboxProps}>
<Input
{...getInputProps()}
name="Search"
id="Search"
placeholder="Search"
/>
<SearchResults
$isVisible={isOpen && matchingElements.length > 0}
{...getMenuProps()}
>
{isOpen &&
matchingElements.map((item, index) => (
<SearchResultsItem
$isHighlighted={highlightedIndex === index}
{...getItemProps({ item, index })}
key={item.id}
>
{item.name}
</SearchResultsItem>
))}
</SearchResults>
</SearchWrapper>
</SearchBarWrapper>
);
};
Sama aplikacja działa prawidłowo - wpisując w Input łańcuch 'el', prawidłowo wyświetla się pod polem tekstowym lista rozwijana wraz z wyszukiwanym elementem 'element'. Lista prawidłowo znika, gdy Input jest pusty lub kliknę gdzieś poza pole wyszukiwania, pojawia się prawidłowo gdy kliknę ponownie w niepusty Input. Konsola przeglądarkowa (F12) nie wyświetla błędów.
Test sprawia wrażenie, jakby nie było żadnego rezultatu po wykonaniu fireEvent.change(input, { target: { value: 'el' } }); - tak jakby w SearchResults (ul) nie było żadnego elementu SearchResultsItem (li), jedynie Input otrzymuje wartość 'el', a lista w ogóle się "nie rozwija".
Nie działają sztuczki w postaci: wprowadzania funkcji expect(), toBeInTheDocument(), toBeVisible(), toBeTruthy(), waitFor(); nadania labelów do komponentów (SearchResults, SearchResultsItem) ; importów TextEncoder, TextDecoder itd. itd. itd. Zastanawiałem się nad reinstalacją do starszych wersji Testing Library lub Mock Service Worker, choć wolałbym tego uniknąć.
Co mam naprawić, aby test przeszedł?