|
| 1 | +import { RatingInput, Form } from "react-hook-form-components"; |
| 2 | +import { faker } from "@faker-js/faker"; |
| 3 | + |
| 4 | +it("rating selection works", () => { |
| 5 | + const name = faker.random.alpha(10); |
| 6 | + const selectedRating = faker.datatype.number({ min: 1, max: 5 }); |
| 7 | + |
| 8 | + cy.mount( |
| 9 | + <Form onSubmit={cy.spy().as("onSubmitSpy")}> |
| 10 | + <RatingInput name={name} label={name} /> |
| 11 | + <input type="submit" /> |
| 12 | + </Form>, |
| 13 | + ); |
| 14 | + |
| 15 | + // Click on a specific star |
| 16 | + cy.get(`input[value="${selectedRating}"]`).click({ force: true }); |
| 17 | + |
| 18 | + cy.get("input[type=submit]").click({ force: true }); |
| 19 | + cy.get("@onSubmitSpy").should("be.calledOnceWith", { [name]: selectedRating }); |
| 20 | +}); |
| 21 | + |
| 22 | +it("default rating value works", () => { |
| 23 | + const name = faker.random.alpha(10); |
| 24 | + const defaultRating = faker.datatype.number({ min: 1, max: 5 }); |
| 25 | + |
| 26 | + cy.mount( |
| 27 | + <Form defaultValues={{ [name]: defaultRating }} onSubmit={cy.spy().as("onSubmitSpy")}> |
| 28 | + <RatingInput name={name} label={name} /> |
| 29 | + <input type="submit" /> |
| 30 | + </Form>, |
| 31 | + ); |
| 32 | + |
| 33 | + // Verify the default rating is selected |
| 34 | + cy.get(`input[value="${defaultRating}"]`).should("be.checked"); |
| 35 | + |
| 36 | + cy.get("input[type=submit]").click({ force: true }); |
| 37 | + cy.get("@onSubmitSpy").should("be.calledOnceWith", { [name]: defaultRating }); |
| 38 | +}); |
| 39 | + |
| 40 | +it("disabled rating works", () => { |
| 41 | + const name = faker.random.alpha(10); |
| 42 | + |
| 43 | + cy.mount( |
| 44 | + <Form onSubmit={() => {}}> |
| 45 | + <RatingInput name={name} label={name} disabled /> |
| 46 | + </Form>, |
| 47 | + ); |
| 48 | + |
| 49 | + // All radio buttons should be disabled |
| 50 | + cy.get(`input`).each(($input) => { |
| 51 | + cy.wrap($input).should("be.disabled"); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +it("precision works with half stars", () => { |
| 56 | + const name = faker.random.alpha(10); |
| 57 | + const halfStarRating = 3.5; |
| 58 | + |
| 59 | + cy.mount( |
| 60 | + <Form defaultValues={{ [name]: halfStarRating }} onSubmit={cy.spy().as("onSubmitSpy")}> |
| 61 | + <RatingInput name={name} label={name} precision={0.5} /> |
| 62 | + <input type="submit" /> |
| 63 | + </Form>, |
| 64 | + ); |
| 65 | + |
| 66 | + cy.get("input[type=submit]").click({ force: true }); |
| 67 | + cy.get("@onSubmitSpy").should("be.calledOnceWith", { [name]: halfStarRating }); |
| 68 | +}); |
| 69 | + |
| 70 | +it("onBlur handler gets called", () => { |
| 71 | + const name = faker.random.alpha(10); |
| 72 | + |
| 73 | + cy.mount( |
| 74 | + <Form onSubmit={() => {}}> |
| 75 | + <RatingInput name={name} label={name} onBlur={cy.spy().as("onBlurSpy")} /> |
| 76 | + </Form>, |
| 77 | + ); |
| 78 | + |
| 79 | + cy.get(`input[value="3"]`).focus(); |
| 80 | + cy.get(`input[value="3"]`).blur(); |
| 81 | + cy.get("@onBlurSpy").should("be.called"); |
| 82 | +}); |
| 83 | + |
| 84 | +it("onChange handler gets called", () => { |
| 85 | + const name = faker.random.alpha(10); |
| 86 | + const newRating = 4; |
| 87 | + |
| 88 | + cy.mount( |
| 89 | + <Form onSubmit={() => {}}> |
| 90 | + <RatingInput name={name} label={name} onChange={cy.spy().as("onChangeSpy")} /> |
| 91 | + </Form>, |
| 92 | + ); |
| 93 | + |
| 94 | + cy.get(`input[value="${newRating}"]`).click({ force: true }); |
| 95 | + cy.get("@onChangeSpy").should("be.calledWithMatch", Cypress.sinon.match.any, newRating); |
| 96 | +}); |
0 commit comments