|
| 1 | +import { Form, TelephoneNumberInput } from "react-hook-form-components"; |
| 2 | +import { faker } from "@faker-js/faker"; |
| 3 | +import { mount } from "cypress/react18"; |
| 4 | + |
| 5 | +it("correctly set telephone number", () => { |
| 6 | + const name = faker.random.alpha(10); |
| 7 | + |
| 8 | + mount( |
| 9 | + <div className="p-4"> |
| 10 | + <Form onSubmit={cy.spy().as("onSubmitSpy")}> |
| 11 | + <TelephoneNumberInput name={name} useBootstrapStyle label="Phone Number" defaultCountry="CH" /> |
| 12 | + <input type="submit" className="mt-4" /> |
| 13 | + </Form> |
| 14 | + </div>, |
| 15 | + ); |
| 16 | + |
| 17 | + cy.get("input[type=submit]").click(); |
| 18 | + cy.get("@onSubmitSpy").should("be.calledWith", { [name]: undefined }); |
| 19 | + |
| 20 | + cy.get(`#${name}`).type("123456789"); |
| 21 | + cy.get("input[type=submit]").click(); |
| 22 | + cy.get("@onSubmitSpy").should("be.calledWith", { [name]: "+41123456789" }); |
| 23 | +}); |
| 24 | + |
| 25 | +it("correctly change country telephone number", () => { |
| 26 | + const name = faker.random.alpha(10); |
| 27 | + |
| 28 | + mount( |
| 29 | + <div className="p-4"> |
| 30 | + <Form onSubmit={cy.spy().as("onSubmitSpy")}> |
| 31 | + <TelephoneNumberInput name={name} useBootstrapStyle label="Phone Number" defaultCountry="CH" /> |
| 32 | + <input type="submit" className="mt-4" /> |
| 33 | + </Form> |
| 34 | + </div>, |
| 35 | + ); |
| 36 | + |
| 37 | + cy.get(`.MuiInputAdornment-root`).click(); |
| 38 | + cy.get(`.MuiInputBase-input:not(#${name})`).clear(); |
| 39 | + cy.get(`.MuiInputBase-input:not(#${name})`).type("IT"); |
| 40 | + cy.get(`.MuiAutocomplete-option`).click(); |
| 41 | + cy.get(`#${name}`).type("1234567890"); |
| 42 | + |
| 43 | + cy.get("input[type=submit]").click(); |
| 44 | + cy.get("@onSubmitSpy").should("be.calledWith", { [name]: "+391234567890" }); |
| 45 | +}); |
| 46 | + |
| 47 | +it("recognize telephone number coming from default values (even if specified in different format)", () => { |
| 48 | + const name = faker.random.alpha(10); |
| 49 | + |
| 50 | + mount( |
| 51 | + <div className="p-4"> |
| 52 | + <Form defaultValues={{ [name]: "+4 4 123456789" }} onSubmit={cy.spy().as("onSubmitSpy")}> |
| 53 | + <TelephoneNumberInput name={name} useBootstrapStyle label="Phone Number" defaultCountry="CH" /> |
| 54 | + <input type="submit" className="mt-4" /> |
| 55 | + </Form> |
| 56 | + </div>, |
| 57 | + ); |
| 58 | + |
| 59 | + cy.get(`.MuiInputAdornment-root`).contains("+44"); |
| 60 | + cy.get(`#${name}`).should("have.value", "123456789"); |
| 61 | +}); |
0 commit comments