diff --git a/cypress/e2e/adminText.cy.js b/cypress/e2e/adminText.cy.js index 4e0893c..a92e595 100644 --- a/cypress/e2e/adminText.cy.js +++ b/cypress/e2e/adminText.cy.js @@ -101,79 +101,6 @@ describe('The admin text detail page', () => { .should('contain', 'Shemos') }) - it('pressing Enter in the add-child input submits', () => { - cy.intercept('POST', '/api/nodes').as('createNode') - cy.intercept('GET', '/api/nodes/0').as('getNodesRefresh') - - cy.get('#text-detail > ul > li') - .first() - .children('button.add-child') - .click() - cy.get('#text-detail > ul > li') - .first() - .children('input.child-title') - .type('Enter Child{enter}') - - cy.wait('@createNode').its('response.statusCode').should('eq', 201) - cy.wait('@getNodesRefresh') - - cy.get('#text-detail li').should('contain', 'Enter Child') - }) - - it('opening add-child on another node closes the first one', () => { - cy.get('#text-detail > ul > li') - .first() - .children('button.add-child') - .click() - cy.get('#text-detail > ul > li') - .first() - .children('input.child-title') - .should('be.visible') - - cy.get('#text-detail > ul > li > ul > li') - .first() - .children('button.add-child') - .click() - - cy.get('#text-detail > ul > li') - .first() - .children('input.child-title') - .should('not.exist') - cy.get('#text-detail > ul > li') - .first() - .children('button.save-child') - .should('not.exist') - cy.get('#text-detail > ul > li > ul > li') - .first() - .children('input.child-title') - .should('be.visible') - }) - - it('opening bulk-add closes an open add-child form', () => { - cy.get('#text-detail > ul > li') - .first() - .children('button.add-child') - .click() - cy.get('#text-detail > ul > li') - .first() - .children('input.child-title') - .should('be.visible') - - cy.get('#text-detail > ul > li > ul > li') - .first() - .children('button.bulk-add-children') - .click() - - cy.get('#text-detail > ul > li') - .first() - .children('input.child-title') - .should('not.exist') - cy.get('#text-detail > ul > li > ul > li') - .first() - .children('input.bulk-title') - .should('be.visible') - }) - it('newly added child persists after page reload', () => { cy.intercept('POST', '/api/nodes').as('createNode') cy.intercept('GET', '/api/nodes/0').as('getNodesRefresh') diff --git a/cypress/e2e/adminTextBulkAdd.cy.js b/cypress/e2e/adminTextBulkAdd.cy.js index 47d3c7f..7d1afc2 100644 --- a/cypress/e2e/adminTextBulkAdd.cy.js +++ b/cypress/e2e/adminTextBulkAdd.cy.js @@ -65,30 +65,6 @@ describe('Bulk add children on the admin text detail page', () => { cy.get('@bulkCreate.all').should('have.length', 0) }) - it('pressing Enter in the bulk-count input submits', () => { - cy.intercept('POST', '/api/nodes/bulk').as('bulkCreate') - cy.intercept('GET', '/api/nodes/0').as('getNodesRefresh') - cy.get('#text-detail > ul > li').first().children('button.bulk-add-children').click() - cy.get('#text-detail > ul > li').first().children('input.bulk-title').type('Enter') - cy.get('#text-detail > ul > li').first().children('input.bulk-count').type('2{enter}') - cy.wait('@bulkCreate').its('response.statusCode').should('eq', 201) - cy.wait('@getNodesRefresh') - cy.get('#text-detail li').should('contain', 'Enter 1') - cy.get('#text-detail li').should('contain', 'Enter 2') - }) - - it('pressing Enter in the bulk-title input submits', () => { - cy.intercept('POST', '/api/nodes/bulk').as('bulkCreate') - cy.intercept('GET', '/api/nodes/0').as('getNodesRefresh') - cy.get('#text-detail > ul > li').first().children('button.bulk-add-children').click() - cy.get('#text-detail > ul > li').first().children('input.bulk-count').type('2') - cy.get('#text-detail > ul > li').first().children('input.bulk-title').type('Title{enter}') - cy.wait('@bulkCreate').its('response.statusCode').should('eq', 201) - cy.wait('@getNodesRefresh') - cy.get('#text-detail li').should('contain', 'Title 1') - cy.get('#text-detail li').should('contain', 'Title 2') - }) - it('bulk added nodes persist after page reload', () => { cy.intercept('POST', '/api/nodes/bulk').as('bulkCreate') cy.intercept('GET', '/api/nodes/0').as('getNodesRefresh') diff --git a/public/js/text.js b/public/js/text.js index 90221d2..486f7f5 100644 --- a/public/js/text.js +++ b/public/js/text.js @@ -101,21 +101,6 @@ function renderTree(nodes, textId, depth = 0) { return ul; } -function closeAllAddForms() { - const selectors = [ - 'input.child-title', - 'button.save-child', - 'input.bulk-title', - 'input.bulk-count', - 'button.save-bulk', - ]; - selectors.forEach((selector) => { - document - .querySelectorAll('#text-detail ' + selector) - .forEach((element) => element.remove()); - }); -} - function toggleAddForm(li, parentNodeId, textId) { const existing = li.querySelector('input.child-title'); if (existing) { @@ -124,8 +109,6 @@ function toggleAddForm(li, parentNodeId, textId) { return; } - closeAllAddForms(); - const input = document.createElement('input'); input.type = 'text'; input.className = 'child-title'; @@ -134,8 +117,7 @@ function toggleAddForm(li, parentNodeId, textId) { const saveBtn = document.createElement('button'); saveBtn.textContent = 'Save'; saveBtn.className = 'save-child'; - - function submit() { + saveBtn.addEventListener('click', () => { const title = input.value.trim(); if (!title) return; @@ -151,19 +133,10 @@ function toggleAddForm(li, parentNodeId, textId) { return res.json(); }) .then(() => fetchAndRenderNodes(textId)); - } - - saveBtn.addEventListener('click', submit); - input.addEventListener('keydown', (event) => { - if (event.key === 'Enter') { - event.preventDefault(); - submit(); - } }); li.appendChild(input); li.appendChild(saveBtn); - input.focus(); } function toggleBulkAddForm(li, parentNodeId, textId) { @@ -175,8 +148,6 @@ function toggleBulkAddForm(li, parentNodeId, textId) { return; } - closeAllAddForms(); - const titleInput = document.createElement('input'); titleInput.type = 'text'; titleInput.className = 'bulk-title'; @@ -191,8 +162,7 @@ function toggleBulkAddForm(li, parentNodeId, textId) { const saveBtn = document.createElement('button'); saveBtn.textContent = 'Save'; saveBtn.className = 'save-bulk'; - - function submit() { + saveBtn.addEventListener('click', () => { const titlePrefix = titleInput.value.trim(); const count = parseInt(countInput.value); if (!titlePrefix || !count || count < 1) return; @@ -209,21 +179,9 @@ function toggleBulkAddForm(li, parentNodeId, textId) { return res.json(); }) .then(() => fetchAndRenderNodes(textId)); - } - - function submitOnEnter(event) { - if (event.key === 'Enter') { - event.preventDefault(); - submit(); - } - } - - saveBtn.addEventListener('click', submit); - titleInput.addEventListener('keydown', submitOnEnter); - countInput.addEventListener('keydown', submitOnEnter); + }); li.appendChild(titleInput); li.appendChild(countInput); li.appendChild(saveBtn); - titleInput.focus(); }