I am trying to write to test toggle action of accordion using cypress testing library. Below is the Markup for accordion, it is being toggled using checkbox hack :
<div class='accordion'>
<div class='accordion__item'>
<input style="display:none" type="checkbox" id="item1"/>
<label class="accordion__itemLabel"for="item1">FAQ's</label>
<div style="display:none" class="accordion__itemContent"> FAQ Content Here </div>
</div>
<div class='accordion__item'>
<input style="display:none" type="checkbox" id="item2"/>
<label class="accordion__itemLabel"for="item2">Contact us details</label>
<div style="display:none" class="accordion__itemContent"> Contact us details here </div>
</div>
</div>
and Tests is below :
it("should toggle accordion ", () => {
const checkbox = cy.findByRole({ role: "checkbox" });
expect(checkbox.checked).equal(false);
cy.get(".accordion__itemContent").invoke("display").equal("none")
fireEvent.click(checkbox)
expect(checkbox.checked).equal(true);
cy.get(".accordion__itemContent").invoke("display").equal("block")
})
The problem is cy.findByRole({ role: "checkbox" });
always returns undefined , how can I fix this or write above test in a right way .
Thanks