import {test} from 'node:test';
import assert from 'node:assert/strict';
import {loadPage} from './learnrun.js';
const item = (page, n) => page.document.querySelectorAll('#list li')[n];
const texts = (page) => [...page.document.querySelectorAll('#list li span')].map((span) => span.textContent);
const done = (page) => [...page.document.querySelectorAll('#list li')].map((li) => li.classList.contains('done'));
test('Remove takes out its item', async () => {
const page = await loadPage();
item(page, 1).querySelector('.remove').click();
assert.deepEqual(texts(page), ['Water the plants', 'Call Mum'], 'after removing Buy bread the list shows ' + JSON.stringify(texts(page)));
});
test('A click on the text marks the item done', async () => {
const page = await loadPage();
item(page, 0).querySelector('span').click();
assert.deepEqual(texts(page), ['Water the plants', 'Buy bread', 'Call Mum'], 'after clicking a text the list shows ' + JSON.stringify(texts(page)));
assert.deepEqual(done(page), [true, false, false], 'after clicking the first text, done is ' + JSON.stringify(done(page)));
});
test('A second click on the text marks it not done', async () => {
const page = await loadPage();
item(page, 2).querySelector('span').click();
item(page, 2).querySelector('span').click();
assert.deepEqual(done(page), [false, false, false], 'after two clicks on the last text, done is ' + JSON.stringify(done(page)));
});
test('A click on the list itself changes nothing', async () => {
const page = await loadPage();
page.document.querySelector('#list').click();
assert.deepEqual(texts(page), ['Water the plants', 'Buy bread', 'Call Mum'], 'after a click between the items the list shows ' + JSON.stringify(texts(page)));
const today = page.document.querySelector('#today').classList.contains('done');
assert.equal(today, false, 'the section Today got the class done');
});
test('Removing works again after the list is rebuilt', async () => {
const page = await loadPage();
item(page, 0).querySelector('.remove').click();
item(page, 0).querySelector('.remove').click();
assert.deepEqual(texts(page), ['Call Mum'], 'after removing the first item twice the list shows ' + JSON.stringify(texts(page)));
});
test('Clicks in the list still reach the section counter', async () => {
const page = await loadPage();
item(page, 0).querySelector('span').click();
item(page, 1).querySelector('.remove').click();
const clicks = page.document.querySelector('#clicks').textContent;
assert.equal(clicks, '2', 'after two clicks in the list the counter shows ' + JSON.stringify(clicks));
});