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);
});
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();