Migrate Cypress E2E from cy.intercept to MSW service worker
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,17 +12,23 @@ describe("Account duplication check.", () => {
|
||||
beforeEach(() => {
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getUsers");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
});
|
||||
|
||||
it("When an account already exists, show error message on confirm.", () => {
|
||||
const testAccountName = "000000";
|
||||
|
||||
// First creation: account doesn't exist yet
|
||||
cy.intercept("GET", "/api/users/000000", {
|
||||
statusCode: 404,
|
||||
body: { detail: "Not found" },
|
||||
}).as("checkNewUser");
|
||||
// First creation: account doesn't exist yet — override via MSW
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.get("/api/users/000000", () =>
|
||||
HttpResponse.json(
|
||||
{ detail: "Not found" },
|
||||
{ status: 404 },
|
||||
)),
|
||||
);
|
||||
});
|
||||
|
||||
cy.contains("button", "Create New").should("be.visible").click();
|
||||
cy.get("#input_account_field").type(testAccountName);
|
||||
@@ -34,20 +40,22 @@ describe("Account duplication check.", () => {
|
||||
.should("be.visible")
|
||||
.and("be.enabled")
|
||||
.click();
|
||||
cy.wait("@postUser");
|
||||
cy.contains("Account added").should("be.visible");
|
||||
|
||||
// Second creation: now account exists — override to return 200
|
||||
cy.intercept("GET", "/api/users/000000", {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
username: "000000",
|
||||
name: "000000",
|
||||
is_admin: false,
|
||||
is_active: true,
|
||||
roles: [],
|
||||
},
|
||||
}).as("checkExistingUser");
|
||||
// Second creation: now account exists — override to return 200 via MSW
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.get("/api/users/000000", () =>
|
||||
HttpResponse.json({
|
||||
username: "000000",
|
||||
name: "000000",
|
||||
is_admin: false,
|
||||
is_active: true,
|
||||
roles: [],
|
||||
})),
|
||||
);
|
||||
});
|
||||
|
||||
cy.contains("button", "Create New").should("be.visible").click();
|
||||
cy.get("#input_account_field").type(testAccountName);
|
||||
|
||||
@@ -10,7 +10,7 @@ describe("Password validation on create account.", () => {
|
||||
beforeEach(() => {
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getUsers");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
});
|
||||
|
||||
it("When password is too short, confirm button stays disabled.", () => {
|
||||
|
||||
@@ -9,13 +9,22 @@ import { loginWithFixtures } from "../../support/intercept";
|
||||
describe("Create an Account", () => {
|
||||
beforeEach(() => {
|
||||
loginWithFixtures();
|
||||
// Override: new usernames should return 404 (account doesn't exist yet)
|
||||
cy.intercept("GET", "/api/users/unit-test-*", {
|
||||
statusCode: 404,
|
||||
body: { detail: "Not found" },
|
||||
}).as("checkNewUser");
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getUsers");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
// Override: new usernames should return 404 (account doesn't exist yet)
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.get("/api/users/:username", ({ params }) => {
|
||||
if (params.username.startsWith("unit-test-")) {
|
||||
return HttpResponse.json(
|
||||
{ detail: "Not found" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it("Create a new account with admin role; should show saved message.", () => {
|
||||
@@ -30,7 +39,6 @@ describe("Create an Account", () => {
|
||||
.should("be.visible")
|
||||
.and("be.enabled")
|
||||
.click();
|
||||
cy.wait("@postUser");
|
||||
cy.contains("Account added").should("be.visible");
|
||||
});
|
||||
|
||||
|
||||
@@ -10,14 +10,13 @@ describe("Delete an Account", () => {
|
||||
beforeEach(() => {
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getUsers");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
});
|
||||
|
||||
it("Delete button opens confirmation modal and deletes on confirm.", () => {
|
||||
cy.get("img.delete-account").first().click();
|
||||
cy.contains("ARE YOU SURE TO DELETE").should("be.visible");
|
||||
cy.get("#sure_to_delete_acct_btn").click();
|
||||
cy.wait("@deleteUser");
|
||||
cy.contains("Account deleted").should("be.visible");
|
||||
});
|
||||
|
||||
|
||||
@@ -13,20 +13,17 @@ describe("Edit an account", () => {
|
||||
beforeEach(() => {
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getUsers");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
});
|
||||
|
||||
it("Edit an account; modify name and see saved message.", () => {
|
||||
cy.get(".btn-edit").first().click();
|
||||
cy.wait("@getUserDetail");
|
||||
|
||||
cy.contains("h1", MODAL_TITLE_ACCOUNT_EDIT).should("exist");
|
||||
cy.get("#input_name_field").clear();
|
||||
cy.get("#input_name_field").type("Updated Name");
|
||||
|
||||
cy.contains("button", "Confirm").should("be.visible").and("be.enabled");
|
||||
cy.contains("button", "Confirm").click();
|
||||
cy.wait("@putUser");
|
||||
cy.contains(MSG_ACCOUNT_EDITED).should("be.visible");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user