I am currently attempting to combine two dates using a rangepicker
.
Below is the command used to select the date:
Cypress.Commands.add('setDatePickerDate', (selector, date) => {
const monthsShort = [
'janv.',
'févr.',
'mars',
'avril',
'mai',
'juin',
'juil.',
'août',
'sept.',
'oct.',
'nov.',
'déc.',
];
const month = monthsShort[date.getMonth()];
const day = date.getDate();
const year = date.getFullYear();
cy.getBySel(selector).should('not.be.disabled');
cy.getBySel(selector).click();
// selecting year
cy.get('.mantine-CalendarHeader-calendarHeaderLevel').click();
cy.get('.mantine-DatePicker-calendarHeaderLevel').click();
recurse(
() => cy.get('.mantine-YearsList-yearsListCell').invoke('text'),
(n) => {
if (!n.includes(year)) {
cy.get('.mantine-DatePicker-calendarHeaderControlIcon').first().click();
return false;
}
cy.get('.mantine-YearsList-yearsListCell').contains(year).click();
return true;
},
{
limit: 12,
}
);
// selecting month
cy.get('.mantine-MonthsList-monthsListCell').contains(month).click();
// selecting day
cy.get('.mantine-DatePicker-day')
.contains(new RegExp(`^(${day})`))
.click();
});
This is the code snippet from my test:
cy.setDatePickerDate(
'filter-date',
new Date('2021-07-01'),
new Date('2021-07-05')
);
The issue I am facing is that only the date 2021-07-01 is being populated in the date field and not the other one.
Could you please provide any guidance on what mistake I might be making?
//updated question as the cy.getBySel(selector).click(); is an optional part
Cypress.Commands.add('setDatePickerDate', (date, clickOnDatePicker) => {
const monthsShort = [
'janv.',
'févr.',
'mars',
'avril',
'mai',
'juin',
'juil.',
'août',
'sept.',
'oct.',
'nov.',
'déc.',
];
if(clickOnDatePicker) {
cy.clickOnDatePicker(clickOnDatePicker);
}
cy.wrap(date).each((date) => {
const month = monthsShort[date.getMonth()];
//rest of the code remains the same
Command for clickOnDatePicker:
Cypress.Commands.add('clickOnDatePicker', (clickOnDatePicker) => {
cy.getBySel(clickOnDatePicker).click()
});
My d.ts file:
setDatePickerDate(object: { clickOnDatePicker?: string }, date: Date): void;
Test code snippet:
cy.setDatePickerDate({ clickOnDatePicker: 'filter-date' }, [
new Date('2021-07-18'),
]);
The test is failing with the error message:
Syntax error, unrecognized expression: [data-testid=Sun Jul 18 2021 04:00:00 GMT+0400 ()]
I wish to execute the below code if a selector needs to be clicked:
cy.setDatePickerDate({ clickOnDatePicker: 'filter-date' }, [
new Date('2021-07-18')]);
otherwise, if there is no selector to click:
cy.setDatePicker([{ new Date('2021-07-18')], new Date[('2021-07-18')]);