From c041877f657a111bb2b90bad59295a6382f77ff2 Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 20 Feb 2025 18:02:07 +0100 Subject: [PATCH] refactor: refs #8581 simplify fillInForm and validateForm --- test/cypress/support/commands.js | 107 ++++++++++++++++--------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/test/cypress/support/commands.js b/test/cypress/support/commands.js index a50504cf1..791fd46ec 100755 --- a/test/cypress/support/commands.js +++ b/test/cypress/support/commands.js @@ -143,57 +143,59 @@ Cypress.Commands.add('countSelectOptions', (selector, option) => { cy.get('.q-menu .q-item').should('have.length', option); }); -Cypress.Commands.add( - 'fillInForm', - (obj, { form = '.q-form > .q-card', attr = 'aria-label' }) => { - cy.waitForElement(form); - cy.get(`${form} input`).each(([el]) => { - cy.wrap(el) - .invoke('attr', attr) - .then((key) => { - const field = obj[key]; - if (!field) return; +Cypress.Commands.add('fillInForm', (obj, opts = {}) => { + const { form = '.q-form > .q-card', attr = 'aria-label' } = opts; + cy.waitForElement(form); + cy.get(`${form} input`).each(([el]) => { + cy.wrap(el) + .invoke('attr', attr) + .then((key) => { + const field = obj[key]; + if (!field) return; + if (typeof field == 'string') + return cy.wrap(el).type(`{selectall}{backspace}${field}`); - const { type, val } = field; - switch (type) { - case 'select': - cy.selectOption(el, val); - break; - case 'date': - cy.get(el).type( - `{selectall}{backspace}${val.split('-').join('')}`, - ); - break; - case 'time': - cy.get(el).click(); - cy.get('.q-time .q-time__clock').contains(val.h).click(); - cy.get('.q-time .q-time__clock').contains(val.m).click(); - cy.get('.q-time .q-time__link').contains(val.x).click(); - break; - default: - cy.wrap(el).type(`{selectall}{backspace}${val}`); - break; - } - }); - }); - }, -); + const { type, val } = field; + switch (type) { + case 'select': + cy.selectOption(el, val); + break; + case 'date': + cy.get(el).type( + `{selectall}{backspace}${val.split('-').join('')}`, + ); + break; + case 'time': + cy.get(el).click(); + cy.get('.q-time .q-time__clock').contains(val.h).click(); + cy.get('.q-time .q-time__clock').contains(val.m).click(); + cy.get('.q-time .q-time__link').contains(val.x).click(); + break; + default: + cy.wrap(el).type(`{selectall}{backspace}${val}`); + break; + } + }); + }); +}); -Cypress.Commands.add( - 'validateForm', - (obj, { form = '.q-form > .q-card', attr = 'data-cy' }) => { - cy.waitForElement(form); - cy.get(`${form} input`).each(([el]) => { - cy.wrap(el) - .invoke('attr', attr) - .then((key) => { - const field = obj[key]; - if (!field) return; +Cypress.Commands.add('validateForm', (obj, opts = {}) => { + const { form = '.q-form > .q-card', attr = 'data-cy' } = opts; + cy.waitForElement(form); + cy.get(`${form} input`).each(([el]) => { + cy.wrap(el) + .invoke('attr', attr) + .then((key) => { + const field = obj[key]; + if (!field) return; - const { type, val } = field; - cy.get(el) - .invoke('val') - .then((elVal) => { + const { type, val } = field; + cy.get(el) + .invoke('val') + .then((elVal) => { + if (typeof field == 'string') + expect(elVal.toLowerCase()).to.equal(field.toLowerCase()); + else switch (type) { case 'date': const elDate = moment(elVal, 'DD-MM-YYYY'); @@ -206,11 +208,10 @@ Cypress.Commands.add( ); break; } - }); - }); - }); - }, -); + }); + }); + }); +}); Cypress.Commands.add('checkOption', (selector) => { cy.get(selector).find('.q-checkbox__inner').click();