import {test} from 'node:test';
import assert from 'node:assert/strict';
import {render, screen, fireEvent} from '@testing-library/react';
import App from './main.jsx';
// Every row's text: "NamePrice" for the header, "Apple$1" for a product, "Fruits" for a category.
const rowTexts = () => screen.getAllByRole('row').map((tr) => tr.textContent);
const productTexts = () => rowTexts().slice(1);
const search = (value) => fireEvent.change(screen.getByRole('textbox'), {target: {value}});
const box = () => screen.getByRole('checkbox', {name: 'Only show products in stock'});
const toggle = () => fireEvent.click(box());
test('All six products show at first', () => {
render(<App />);
assert.deepEqual(productTexts(), ['Apple$1', 'Dragonfruit$1', 'Passionfruit$2', 'Spinach$2', 'Pumpkin$4', 'Peas$1'], 'at first the rows are ' + JSON.stringify(productTexts()));
});
test('The search text filters by part of the name', () => {
render(<App />);
search('fruit');
assert.deepEqual(productTexts(), ['Dragonfruit$1', 'Passionfruit$2'], 'after typing fruit the rows are ' + JSON.stringify(productTexts()));
});
test('The search ignores upper and lower case', () => {
render(<App />);
search('PEA');
assert.deepEqual(productTexts(), ['Peas$1'], 'after typing PEA the rows are ' + JSON.stringify(productTexts()));
});
test('The checkbox hides products that are not in stock', () => {
render(<App />);
toggle();
assert.deepEqual(productTexts(), ['Apple$1', 'Dragonfruit$1', 'Spinach$2', 'Peas$1'], 'with the box checked the rows are ' + JSON.stringify(productTexts()));
toggle();
assert.deepEqual(productTexts(), ['Apple$1', 'Dragonfruit$1', 'Passionfruit$2', 'Spinach$2', 'Pumpkin$4', 'Peas$1'], 'after unchecking the box the rows are ' + JSON.stringify(productTexts()));
});
test('The search text and the checkbox work together', () => {
render(<App />);
search('fruit');
toggle();
assert.deepEqual(productTexts(), ['Dragonfruit$1'], 'with fruit and the box checked the rows are ' + JSON.stringify(productTexts()));
});