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:
@@ -9,13 +9,18 @@ describe("Edge Cases", () => {
|
||||
describe("Empty states", () => {
|
||||
it("files page handles empty file list", () => {
|
||||
loginWithFixtures();
|
||||
// Override files intercept with empty array
|
||||
cy.intercept("GET", "/api/files", {
|
||||
statusCode: 200,
|
||||
body: [],
|
||||
}).as("getEmptyFiles");
|
||||
// Visit any page first to load the app and MSW
|
||||
cy.visit("/files");
|
||||
cy.contains("sample-process.xes").should("exist");
|
||||
// Override files endpoint with empty array via MSW
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.get("/api/files", () => HttpResponse.json([])),
|
||||
);
|
||||
});
|
||||
// Revisit to trigger the new empty response
|
||||
cy.visit("/files");
|
||||
cy.wait("@getEmptyFiles");
|
||||
// Table should exist but have no file data
|
||||
cy.get("table").should("exist");
|
||||
cy.contains("sample-process.xes").should("not.exist");
|
||||
@@ -23,20 +28,36 @@ describe("Edge Cases", () => {
|
||||
|
||||
it("account admin handles empty user list", () => {
|
||||
loginWithFixtures();
|
||||
cy.intercept("GET", "/api/users", {
|
||||
statusCode: 200,
|
||||
body: [],
|
||||
}).as("getEmptyUsers");
|
||||
cy.visit("/files");
|
||||
cy.contains("sample-process.xes").should("exist");
|
||||
// Override users endpoint with empty array via MSW
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.get("/api/users", () => HttpResponse.json([])),
|
||||
);
|
||||
});
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getEmptyUsers");
|
||||
// Create New button should still work
|
||||
// Create New button should exist even with no users
|
||||
cy.get("#create_new_acct_btn").should("exist");
|
||||
cy.contains("Test Admin").should("not.exist");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Authentication guard", () => {
|
||||
it("unauthenticated user is redirected to login", () => {
|
||||
// No loginWithFixtures - not logged in
|
||||
loginWithFixtures();
|
||||
cy.visit("/files");
|
||||
cy.contains("sample-process.xes").should("exist");
|
||||
// Override my-account to return 401
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.get("/api/my-account", () =>
|
||||
new HttpResponse(null, { status: 401 }),
|
||||
),
|
||||
);
|
||||
});
|
||||
// Clear cookies to simulate logged out
|
||||
cy.clearCookies();
|
||||
cy.visit("/files");
|
||||
cy.url().should("include", "/login");
|
||||
});
|
||||
@@ -54,41 +75,52 @@ describe("Edge Cases", () => {
|
||||
|
||||
describe("Login validation", () => {
|
||||
it("shows error on failed login", () => {
|
||||
cy.intercept("POST", "/api/oauth/token", {
|
||||
statusCode: 401,
|
||||
body: { detail: "Invalid credentials" },
|
||||
}).as("failedLogin");
|
||||
// Visit login page first to load the app
|
||||
cy.visit("/login");
|
||||
cy.get("#account").type("wrong");
|
||||
cy.get("#password").type("wrong");
|
||||
cy.get("form").submit();
|
||||
cy.wait("@failedLogin");
|
||||
// Should stay on login page
|
||||
cy.get("#login_btn_main_btn").should("exist");
|
||||
// Override token endpoint to return 401
|
||||
cy.window().then((win) => {
|
||||
const { http, HttpResponse } = win.__msw__;
|
||||
win.__mswWorker__.use(
|
||||
http.post("/api/oauth/token", () =>
|
||||
HttpResponse.json(
|
||||
{ detail: "Invalid credentials" },
|
||||
{ status: 401 },
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
cy.get("#account").type("wronguser");
|
||||
cy.get("#password").type("wrongpass");
|
||||
cy.get("#login_btn_main_btn").click();
|
||||
cy.url().should("include", "/login");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Account creation validation", () => {
|
||||
beforeEach(() => {
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.wait("@getUsers");
|
||||
cy.get("#create_new_acct_btn").click();
|
||||
});
|
||||
|
||||
it("confirm stays disabled with only account field filled", () => {
|
||||
cy.get("#input_account_field").type("newuser");
|
||||
cy.get(".confirm-btn").should("be.disabled");
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
cy.contains("button", "Create New").click();
|
||||
cy.get("#input_account_field").type("onlyaccount");
|
||||
cy.contains("button", "Confirm").should("be.disabled");
|
||||
});
|
||||
|
||||
it("confirm stays disabled with only name field filled", () => {
|
||||
cy.get("#input_name_field").type("New User");
|
||||
cy.get(".confirm-btn").should("be.disabled");
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
cy.contains("button", "Create New").click();
|
||||
cy.get("#input_name_field").type("onlyname");
|
||||
cy.contains("button", "Confirm").should("be.disabled");
|
||||
});
|
||||
|
||||
it("confirm stays disabled with only password field filled", () => {
|
||||
cy.get("#input_first_pwd").type("password1234");
|
||||
cy.get(".confirm-btn").should("be.disabled");
|
||||
loginWithFixtures();
|
||||
cy.visit("/account-admin");
|
||||
cy.contains("Test Admin").should("exist");
|
||||
cy.contains("button", "Create New").click();
|
||||
cy.get("#input_first_pwd").type("onlypassword");
|
||||
cy.contains("button", "Confirm").should("be.disabled");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user