'Cypress - if - else condition doesn't work

I need to automate an if/else condition with Cypress, but it doesn't work in my script. If an if element is present (or visible) then click Yes otherwise proceed. But in my case, if the first condition is not present the test try to click Yes anyway.

it('Logout', () => {
      const out = new logout();
      const lged = new logged();
      
      lged.logged().then($button => {
        if ($button.is(':visible')){
          lged.logged().click()
          out.bar().click()
          out.clickout()
        }
        else {
          out.bar().click()
          out.clickout()
        }
      })
    })

in my pageObjects I have the class "logged":

    class logged {

    logged(){
        return cy.contains('Yes', { timeout: 10000 })
    }
}

export default logged


Sources

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

Source: Stack Overflow

Solution Source