import {test} from 'node:test';
import assert from 'node:assert/strict';
import {render, screen, fireEvent} from '@testing-library/react';
import FilterableList from './main.jsx';
const search = () => screen.getByLabelText('Search:');
const shown = () => screen.queryAllByRole('listitem').map((li) => li.textContent).join(', ');
test('All six fruits show at the start', () => {
render(<FilterableList />);
assert.equal(shown(), 'Strawberry, Blueberry, Banana, Gooseberry, Cherry, Raspberry', 'at the start the list shows ' + JSON.stringify(shown()));
});
test('Typing berry keeps only the berries', () => {
render(<FilterableList />);
fireEvent.change(search(), {target: {value: 'berry'}});
assert.equal(search().value, 'berry', 'the search field holds ' + JSON.stringify(search().value));
assert.equal(shown(), 'Strawberry, Blueberry, Gooseberry, Raspberry', 'after typing berry the list shows ' + JSON.stringify(shown()));
});
test('The search ignores upper and lower case', () => {
render(<FilterableList />);
fireEvent.change(search(), {target: {value: 'CHERRY'}});
assert.equal(shown(), 'Cherry', 'after typing CHERRY the list shows ' + JSON.stringify(shown()));
});
test('Clearing the search shows every fruit again', () => {
render(<FilterableList />);
fireEvent.change(search(), {target: {value: 'ban'}});
assert.equal(shown(), 'Banana', 'after typing ban the list shows ' + JSON.stringify(shown()));
fireEvent.change(search(), {target: {value: ''}});
assert.equal(shown(), 'Strawberry, Blueberry, Banana, Gooseberry, Cherry, Raspberry', 'after clearing the search the list shows ' + JSON.stringify(shown()));
});