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,36 +455,40 @@ Cypress.Commands.add('validateVnTableRows', (opts = {}) => {
.invoke('text')
.then((text) => {
if (type === 'string') expect(text.trim()).to.equal(val);
if (type === 'number') {
const num = parseFloat(text.trim().replace(/[^\d.-]/g, ''));
switch (operation) {
case 'equal':
expect(num).to.equal(val);
break;
case 'greater':
expect(num).to.be.greaterThan(val);
break;
case 'less':
expect(num).to.be.lessThan(val);
break;
}
}
if (type === 'date') {
const date = moment(text.trim(), 'DD/MM/YYYY');
const compareDate = moment(val, 'DD/MM/YYYY');
switch (operation) {
case 'equal':
expect(text.trim()).to.equal(val);
break;
case 'before':
expect(date.isBefore(compareDate)).to.be.true;
break;
case 'after':
expect(date.isAfter(compareDate)).to.be.true;
}
}
if (type === 'number') cy.checkNumber(text, val, operation);
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) {
case 'equal':
expect(num).to.equal(expectedVal);
break;
case 'greater':
expect(num).to.be.greaterThan(expectedVal);
break;
case 'less':
expect(num).to.be.lessThan(expectedVal);
break;
}
});
Cypress.Commands.add('checkDate', (rawDate, expectedVal, operation) => {
const date = moment(rawDate.trim(), 'DD/MM/YYYY');
const compareDate = moment(expectedVal, 'DD/MM/YYYY');
switch (operation) {
case 'equal':
expect(text.trim()).to.equal(compareDate);
break;
case 'before':
expect(date.isBefore(compareDate)).to.be.true;
break;
case 'after':
expect(date.isAfter(compareDate)).to.be.true;
}
});