import {test} from 'node:test';
import assert from 'node:assert/strict';
import {render, screen, within} from '@testing-library/react';
import Board from './main.jsx';
test('The board shows nine squares numbered 1 to 9', () => {
render(<Board />);
const labels = screen.getAllByRole('button').map((button) => button.textContent);
assert.deepEqual(labels, ['1', '2', '3', '4', '5', '6', '7', '8', '9'], 'the squares read ' + JSON.stringify(labels));
});
test('The squares sit in three rows of three', () => {
const {container} = render(<Board />);
const rows = [...container.children];
assert.equal(rows.length, 3, 'the board puts ' + rows.length + ' element(s) into #root, not 3 rows');
rows.forEach((row, i) => {
const count = within(row).queryAllByRole('button').length;
assert.equal(count, 3, 'row ' + (i + 1) + ' holds ' + count + ' square(s)');
});
});
test('Rows and squares have their CSS classes', () => {
const {container} = render(<Board />);
const rows = container.querySelectorAll('div.board-row').length;
const squares = container.querySelectorAll('button.square').length;
assert.equal(rows, 3, rows + ' element(s) have the class board-row');
assert.equal(squares, 9, squares + ' button(s) have the class square');
});
test('React prints no warnings', () => {
const warnings = [];
const original = console.error;
console.error = (...args) => warnings.push(args.join(' '));
try {
render(<Board />);
} finally {
console.error = original;
}
assert.deepEqual(warnings, [], 'console.error while rendering');
});