import {test} from 'node:test';
import assert from 'node:assert/strict';
import {render, screen} from '@testing-library/react';
import Gallery from './main.jsx';
import {people} from './data.js';
test('React prints no warnings', () => {
const warnings = [];
const original = console.error;
console.error = (...args) => warnings.push(args.join(' '));
try {
render(<Gallery />);
} finally {
console.error = original;
}
assert.deepEqual(warnings, [], 'console.error while rendering');
});
test('Four profiles, sorted by name and numbered from 1', () => {
render(<Gallery />);
const names = screen.getAllByRole('heading', {level: 2}).map((h) => h.textContent);
assert.deepEqual(names, ['1. Alan Turing', '2. C. V. Raman', '3. Marie Skłodowska-Curie', '4. Rosalind Franklin'], 'the profiles read ' + JSON.stringify(names));
assert.equal(screen.getByText(/profiles$/).textContent, '4 profiles', 'the count line');
});
test('Each profile shows an avatar named after the person', () => {
render(<Gallery />);
const avatars = screen.getAllByRole('img').map((img) => img.getAttribute('aria-label'));
assert.equal(avatars.length, 4, 'avatars: ' + avatars.length);
assert.ok(avatars.includes('Alan Turing'), 'no avatar is named Alan Turing');
});
test('Rendering does not change the array in data.js', () => {
render(<Gallery />);
const ids = people.map((person) => person.id);
assert.deepEqual(ids, ['msc', 'atu', 'rfr', 'cvr'], 'data.js now holds ' + JSON.stringify(ids));
});
test('Rendering twice gives the same HTML', () => {
const first = render(<Gallery />).container.innerHTML;
const second = render(<Gallery />).container.innerHTML;
assert.equal(second, first, 'the second render differs from the first');
});