refactor: refs #8581 extract number & date validation

This commit is contained in:
Jorge Penadés 2025-02-24 16:04:28 +01:00
parent d52635b764
commit 702f295403
1 changed files with 32 additions and 28 deletions

View File

@ -455,26 +455,35 @@ Cypress.Commands.add('validateVnTableRows', (opts = {}) => {
.invoke('text') .invoke('text')
.then((text) => { .then((text) => {
if (type === 'string') expect(text.trim()).to.equal(val); if (type === 'string') expect(text.trim()).to.equal(val);
if (type === 'number') { if (type === 'number') cy.checkNumber(text, val, operation);
const num = parseFloat(text.trim().replace(/[^\d.-]/g, '')); if (type === 'date') cy.checkDate(text, val, operation);
});
}
});
});
});
Cypress.Commands.add('checkNumber', (text, expectedVal, operation) => {
const num = parseFloat(text.trim().replace(/[^\d.-]/g, '')); // Remove the currency symbol
switch (operation) { switch (operation) {
case 'equal': case 'equal':
expect(num).to.equal(val); expect(num).to.equal(expectedVal);
break; break;
case 'greater': case 'greater':
expect(num).to.be.greaterThan(val); expect(num).to.be.greaterThan(expectedVal);
break; break;
case 'less': case 'less':
expect(num).to.be.lessThan(val); expect(num).to.be.lessThan(expectedVal);
break; break;
} }
} });
if (type === 'date') {
const date = moment(text.trim(), 'DD/MM/YYYY'); Cypress.Commands.add('checkDate', (rawDate, expectedVal, operation) => {
const compareDate = moment(val, 'DD/MM/YYYY'); const date = moment(rawDate.trim(), 'DD/MM/YYYY');
const compareDate = moment(expectedVal, 'DD/MM/YYYY');
switch (operation) { switch (operation) {
case 'equal': case 'equal':
expect(text.trim()).to.equal(val); expect(text.trim()).to.equal(compareDate);
break; break;
case 'before': case 'before':
expect(date.isBefore(compareDate)).to.be.true; expect(date.isBefore(compareDate)).to.be.true;
@ -482,9 +491,4 @@ Cypress.Commands.add('validateVnTableRows', (opts = {}) => {
case 'after': case 'after':
expect(date.isAfter(compareDate)).to.be.true; expect(date.isAfter(compareDate)).to.be.true;
} }
}
});
}
});
});
}); });