'Why wont my if statements work in cypress?

I'm trying to write a test that goes back in a calendar to look for a specific date to click. I have come up with an IF statement that should loop to the next month should date not be found.

Given('I test {string}', (control) => {
  if (
    cy
      .get(`[data-cy="${control}"]`)
      .scrollIntoView()
      .should('be.visible')
  ) {
    cy.get(control).first().click();
    // do something if it's active
  } else {
    cy.get(`[data-cy="${'date-picker-left-arrow'}"]`).click();
    // do something else
  }
});

However, my test fails without iterating to the next condition.



Sources

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

Source: Stack Overflow

Solution Source