refactor: refs #8581 simplify fillInForm and validateForm

This commit is contained in:
Jorge Penadés 2025-02-20 18:02:07 +01:00
parent c3b6f79965
commit c041877f65
1 changed files with 54 additions and 53 deletions

View File

@ -143,57 +143,59 @@ Cypress.Commands.add('countSelectOptions', (selector, option) => {
cy.get('.q-menu .q-item').should('have.length', option); cy.get('.q-menu .q-item').should('have.length', option);
}); });
Cypress.Commands.add( Cypress.Commands.add('fillInForm', (obj, opts = {}) => {
'fillInForm', const { form = '.q-form > .q-card', attr = 'aria-label' } = opts;
(obj, { form = '.q-form > .q-card', attr = 'aria-label' }) => { cy.waitForElement(form);
cy.waitForElement(form); cy.get(`${form} input`).each(([el]) => {
cy.get(`${form} input`).each(([el]) => { cy.wrap(el)
cy.wrap(el) .invoke('attr', attr)
.invoke('attr', attr) .then((key) => {
.then((key) => { const field = obj[key];
const field = obj[key]; if (!field) return;
if (!field) return; if (typeof field == 'string')
return cy.wrap(el).type(`{selectall}{backspace}${field}`);
const { type, val } = field; const { type, val } = field;
switch (type) { switch (type) {
case 'select': case 'select':
cy.selectOption(el, val); cy.selectOption(el, val);
break; break;
case 'date': case 'date':
cy.get(el).type( cy.get(el).type(
`{selectall}{backspace}${val.split('-').join('')}`, `{selectall}{backspace}${val.split('-').join('')}`,
); );
break; break;
case 'time': case 'time':
cy.get(el).click(); cy.get(el).click();
cy.get('.q-time .q-time__clock').contains(val.h).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__clock').contains(val.m).click();
cy.get('.q-time .q-time__link').contains(val.x).click(); cy.get('.q-time .q-time__link').contains(val.x).click();
break; break;
default: default:
cy.wrap(el).type(`{selectall}{backspace}${val}`); cy.wrap(el).type(`{selectall}{backspace}${val}`);
break; break;
} }
}); });
}); });
}, });
);
Cypress.Commands.add( Cypress.Commands.add('validateForm', (obj, opts = {}) => {
'validateForm', const { form = '.q-form > .q-card', attr = 'data-cy' } = opts;
(obj, { form = '.q-form > .q-card', attr = 'data-cy' }) => { cy.waitForElement(form);
cy.waitForElement(form); cy.get(`${form} input`).each(([el]) => {
cy.get(`${form} input`).each(([el]) => { cy.wrap(el)
cy.wrap(el) .invoke('attr', attr)
.invoke('attr', attr) .then((key) => {
.then((key) => { const field = obj[key];
const field = obj[key]; if (!field) return;
if (!field) return;
const { type, val } = field; const { type, val } = field;
cy.get(el) cy.get(el)
.invoke('val') .invoke('val')
.then((elVal) => { .then((elVal) => {
if (typeof field == 'string')
expect(elVal.toLowerCase()).to.equal(field.toLowerCase());
else
switch (type) { switch (type) {
case 'date': case 'date':
const elDate = moment(elVal, 'DD-MM-YYYY'); const elDate = moment(elVal, 'DD-MM-YYYY');
@ -206,11 +208,10 @@ Cypress.Commands.add(
); );
break; break;
} }
}); });
}); });
}); });
}, });
);
Cypress.Commands.add('checkOption', (selector) => { Cypress.Commands.add('checkOption', (selector) => {
cy.get(selector).find('.q-checkbox__inner').click(); cy.get(selector).find('.q-checkbox__inner').click();