'Cypress Conditionnal loop

I'd like to make a conditional loop where I can execute a certain action (In this case, clicking a button) until I meet a certain condition (In this case, finding a year in a date-picker).

I have found a work around where I loop 250 time until I find the year I am looking for, but I don't like this approach. Is there a better way to do it ?

export default (placeholder, day, month, year) => {
  describe("Select a date", () =>  {
    it("Select a year", () => {
      cy.get('input[data-placeholder="'+ placeholder +'"].mat-datepicker-input').parent().next().click();
      cy.get('button[aria-label="Choose month and year"]').click();
      cy.get('div.mat-calendar-content').then((content) => {
        cy.wrap(content).as('content');

        if (!(content.text().includes(year))) {
          var array = Array.from({length:250},(v,k)=>k+1);
          for (let i = 0; i < array.length; i++) {
            cy.get('@content', {log: false}).then((tmp) => {
              if (!(tmp.text().includes(year))) {
                previous();
              }
            });
          }
        }
      });

      cy.contains(year).click();
    });
  });
}


Solution 1:[1]

You are doing a lot of complicated stuff on a control that is already tested by Angular Material library.

Why don't you just type in your date

cy.get(`input[data-placeholder="${placeholder}"]`)
  .type('01-01-2022')
  .blur()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fody