feat: refs #8581 add validateForm command for form validation with date handling

This commit is contained in:
Jorge Penadés 2025-02-20 16:33:46 +01:00
parent 73f3a2c98d
commit 813e677a12
1 changed files with 37 additions and 3 deletions

View File

@ -27,6 +27,7 @@
// DO NOT REMOVE
// Imports Quasar Cypress AE predefined commands
// import { registerCommands } from '@quasar/quasar-app-extension-testing-e2e-cypress';
import moment from 'moment';
Cypress.Commands.add('waitUntil', { prevSubject: 'optional' }, require('./waitUntil'));
Cypress.Commands.add('resetDB', () => {
cy.exec('pnpm run resetDatabase');
@ -107,7 +108,7 @@ function selectItem(selector, option, ariaControl, hasWrite = true) {
getItems(ariaControl).then((items) => {
const matchingItem = items
.toArray()
.find((item) => item.innerText.includes(option));
.find((item) => item.innerText.toLowerCase().includes(option.toLowerCase()));
if (matchingItem) return cy.wrap(matchingItem).click();
if (hasWrite) cy.get(selector).clear().type(option, { delay: 0 });
@ -149,8 +150,8 @@ Cypress.Commands.add(
cy.get(`${form} input`).each(([el]) => {
cy.wrap(el)
.invoke('attr', attr)
.then((ariaLabel) => {
const field = obj[ariaLabel];
.then((key) => {
const field = obj[key];
if (!field) return;
const { type, val } = field;
@ -178,6 +179,39 @@ Cypress.Commands.add(
},
);
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;
const { type, val } = field;
cy.get(el)
.invoke('val')
.then((elVal) => {
switch (type) {
case 'date':
const elDate = moment(elVal, 'DD-MM-YYYY');
const mockDate = moment(val, 'DD-MM-YYYY');
expect(elDate.isSame(mockDate, 'day')).to.be.true;
break;
default:
expect(elVal.toLowerCase()).to.equal(
val.toLowerCase(),
);
break;
}
});
});
});
},
);
Cypress.Commands.add('checkOption', (selector) => {
cy.get(selector).find('.q-checkbox__inner').click();
});