// Mocks the API via cy.intercept — see auth.cy.ts for the rationale. const authenticatedProfile = { id: 1, firstName: "Alice", lastName: "Martin", email: "alice@example.com", tokenVersion: 0, houseId: 1, dietId: 2, }; describe("Dietary preferences (/parametres/preferences) — hot saving", () => { beforeEach(() => { cy.intercept("GET", "**/auth/me", { statusCode: 200, body: authenticatedProfile }); cy.intercept("GET", "**/reference/diets", { statusCode: 200, body: [ { id: 1, name: "Omnivore" }, { id: 2, name: "Végétarien" }, ], }); cy.intercept("GET", "**/reference/allergies", { statusCode: 200, body: [ { id: 1, name: "Arachides", kind: "ALLERGY" }, { id: 2, name: "Gluten", kind: "INTOLERANCE" }, ], }); cy.intercept("GET", "**/profile/allergies", { statusCode: 200, body: [2] }); // The page also loads the reference ingredient list + the profile's // disliked-ingredients selection for `DislikedIngredientsField` — added // alongside `getDiets`/`getAllergies` in the same `Promise.all` (see // PreferencesPage.tsx), so both need mocking here too or that `Promise.all` // rejects and the whole page renders its error state instead of the form, // taking `#diet`/the allergy checkboxes down with it. cy.intercept("GET", "**/reference/ingredients", { statusCode: 200, body: [] }); cy.intercept("GET", "**/profile/disliked-ingredients", { statusCode: 200, body: [] }); }); it("loads the current regime, and shows allergies/intolerances as two groups", () => { cy.visit("/parametres/preferences"); cy.get("#diet").should("have.value", "2"); cy.contains("legend", "Allergies").should("be.visible"); cy.contains("legend", "Intolérances").should("be.visible"); cy.contains("label", "Gluten").find("input[type=checkbox]").should("be.checked"); cy.contains("label", "Arachides").find("input[type=checkbox]").should("not.be.checked"); }); it("has no explicit save button anywhere on the page", () => { cy.visit("/parametres/preferences"); cy.contains("button", "Enregistrer").should("not.exist"); }); it("autosaves the regime as soon as it's selected", () => { cy.intercept("PATCH", "**/profile/diet", { statusCode: 200, body: { ...authenticatedProfile, dietId: 1 }, }).as("updateDiet"); cy.visit("/parametres/preferences"); cy.get("#diet").select("Omnivore"); cy.wait("@updateDiet").its("request.body").should("deep.equal", { dietId: 1 }); }); it("autosaves allergies and intolerances together after checking boxes", () => { cy.intercept("PATCH", "**/profile/allergies", { statusCode: 200, body: [2, 1] }).as( "updateAllergies", ); cy.visit("/parametres/preferences"); cy.contains("label", "Arachides").find("input[type=checkbox]").check(); cy.wait("@updateAllergies") .its("request.body") .should("deep.equal", { allergyIds: [2, 1] }); cy.contains("Enregistré ✓").should("be.visible"); }); });