admin/cypress/e2e/users-admin.cy.ts

350 lines
10 KiB
TypeScript

/// <reference types="../support" />
describe("Users Admin Management", () => {
beforeEach(() => {
cy.login();
cy.visit("/users-admin");
cy.waitForLoading();
});
it("should display users admin list page", () => {
cy.contains("مدیریت کاربران").should("be.visible");
cy.getByTestId("create-user-button").should("be.visible");
});
it("should navigate to create user page", () => {
cy.getByTestId("create-user-button").click();
cy.url().should("include", "/users-admin/create");
cy.contains("ایجاد کاربر جدید").should("be.visible");
});
it("should create a new user", () => {
cy.getByTestId("create-user-button").click();
// Fill basic information
cy.getByTestId("first-name-input").type("محمد");
cy.getByTestId("last-name-input").type("احمدی");
cy.getByTestId("phone-number-input").type("09123456789");
cy.getByTestId("email-input").type("mohammad.ahmadi@example.com");
cy.getByTestId("national-code-input").type("1234567890");
cy.getByTestId("password-input").type("password123");
// Set verification status
cy.getByTestId("verified-true-radio").check();
// Submit form
cy.getByTestId("submit-button").click();
// Verify creation
cy.url().should("include", "/users-admin/");
cy.url().should("not.include", "/create");
});
it("should validate required fields", () => {
cy.getByTestId("create-user-button").click();
// Submit button should be disabled initially
cy.getByTestId("submit-button").should("be.disabled");
// Fill only first name
cy.getByTestId("first-name-input").type("محمد");
cy.getByTestId("submit-button").should("be.disabled");
// Fill all required fields
cy.getByTestId("last-name-input").type("احمدی");
cy.getByTestId("phone-number-input").type("09123456789");
// Now submit button should be enabled
cy.getByTestId("submit-button").should("not.be.disabled");
});
it("should validate phone number format", () => {
cy.getByTestId("create-user-button").click();
// Test invalid phone number
cy.getByTestId("phone-number-input").type("123456");
cy.getByTestId("first-name-input").type("محمد");
cy.getByTestId("last-name-input").type("احمدی");
cy.get(".text-red-600").should("contain", "شماره تلفن معتبر نیست");
// Fix phone number
cy.getByTestId("phone-number-input").clear().type("09123456789");
cy.get(".text-red-600").should("not.contain", "شماره تلفن معتبر نیست");
});
it("should validate email format", () => {
cy.getByTestId("create-user-button").click();
// Test invalid email
cy.getByTestId("email-input").type("invalid-email");
cy.getByTestId("first-name-input").type("محمد");
cy.get(".text-red-600").should("contain", "ایمیل معتبر نیست");
// Fix email
cy.getByTestId("email-input").clear().type("valid@example.com");
cy.get(".text-red-600").should("not.contain", "ایمیل معتبر نیست");
});
it("should search users", () => {
// Search by text
cy.getByTestId("search-users-input").type("محمد");
cy.getByTestId("search-button").click();
cy.wait(500);
// Clear search
cy.getByTestId("clear-filters-button").click();
cy.getByTestId("search-users-input").should("have.value", "");
});
it("should filter users by status", () => {
// Filter by verified status
cy.getByTestId("status-filter-select").select("verified");
cy.getByTestId("search-button").click();
cy.wait(500);
// Filter by unverified status
cy.getByTestId("status-filter-select").select("unverified");
cy.getByTestId("search-button").click();
cy.wait(500);
// Reset filter
cy.getByTestId("status-filter-select").select("all");
cy.getByTestId("search-button").click();
});
it("should handle user verification toggle", () => {
// Mock API response for users list
cy.intercept("GET", "**/users**", {
statusCode: 200,
body: {
users: [
{
id: 1,
phone_number: "+989123456789",
first_name: "محمد",
last_name: "احمدی",
email: "mohammad@example.com",
verified: false,
},
],
total: 1,
limit: 20,
offset: 0,
},
}).as("getUsers");
// Mock verify API
cy.intercept("POST", "**/users/1/verify", {
statusCode: 200,
body: { message: "User verified successfully" },
}).as("verifyUser");
cy.visit("/users-admin");
cy.wait("@getUsers");
// Click verify button
cy.getByTestId("verify-user-1").click();
cy.wait("@verifyUser");
// Check for success message
cy.contains("کاربر با موفقیت تأیید شد").should("be.visible");
});
it("should view user details", () => {
// Mock API response
cy.intercept("GET", "**/users**", {
statusCode: 200,
body: {
users: [
{
id: 1,
phone_number: "+989123456789",
first_name: "محمد",
last_name: "احمدی",
email: "mohammad@example.com",
verified: true,
},
],
},
}).as("getUsers");
cy.intercept("GET", "**/users/1", {
statusCode: 200,
body: {
id: 1,
phone_number: "+989123456789",
first_name: "محمد",
last_name: "احمدی",
email: "mohammad@example.com",
verified: true,
},
}).as("getUser");
cy.visit("/users-admin");
cy.wait("@getUsers");
// Click view button
cy.getByTestId("view-user-1").click();
cy.wait("@getUser");
cy.url().should("include", "/users-admin/1");
cy.contains("جزئیات کاربر").should("be.visible");
cy.contains("محمد احمدی").should("be.visible");
});
it("should edit user", () => {
// Mock get user API
cy.intercept("GET", "**/users/1", {
statusCode: 200,
body: {
id: 1,
phone_number: "+989123456789",
first_name: "محمد",
last_name: "احمدی",
email: "mohammad@example.com",
verified: true,
},
}).as("getUser");
// Mock update user API
cy.intercept("PUT", "**/users/1", {
statusCode: 200,
body: {
id: 1,
phone_number: "+989123456789",
first_name: "محمد",
last_name: "احمدی ویرایش شده",
email: "mohammad.updated@example.com",
verified: true,
},
}).as("updateUser");
cy.visit("/users-admin/1/edit");
cy.wait("@getUser");
// Edit user information
cy.getByTestId("last-name-input").clear().type("احمدی ویرایش شده");
cy.getByTestId("email-input").clear().type("mohammad.updated@example.com");
// Submit form
cy.getByTestId("submit-button").click();
cy.wait("@updateUser");
// Check for success message
cy.contains("کاربر با موفقیت به‌روزرسانی شد").should("be.visible");
});
it("should delete user with confirmation", () => {
// Mock API responses
cy.intercept("GET", "**/users**", {
statusCode: 200,
body: {
users: [
{
id: 1,
phone_number: "+989123456789",
first_name: "محمد",
last_name: "احمدی",
email: "mohammad@example.com",
verified: true,
},
],
},
}).as("getUsers");
cy.intercept("DELETE", "**/users/1", {
statusCode: 200,
body: { message: "User deleted successfully" },
}).as("deleteUser");
cy.visit("/users-admin");
cy.wait("@getUsers");
// Click delete button
cy.getByTestId("delete-user-1").click();
// Confirm deletion in modal
cy.contains("آیا از حذف کاربر").should("be.visible");
cy.contains("button", "حذف").click();
cy.wait("@deleteUser");
// Check for success message
cy.contains("کاربر با موفقیت حذف شد").should("be.visible");
});
it("should handle form cancellation", () => {
cy.getByTestId("create-user-button").click();
// Fill some data
cy.getByTestId("first-name-input").type("محمد");
cy.getByTestId("last-name-input").type("احمدی");
// Click cancel
cy.getByTestId("cancel-button").click();
// Should return to list page
cy.url().should("include", "/users-admin");
cy.url().should("not.include", "/create");
});
it("should show empty state when no users found", () => {
// Mock empty users response
cy.intercept("GET", "**/users**", {
statusCode: 200,
body: {
users: [],
total: 0,
limit: 20,
offset: 0,
},
}).as("getEmptyUsers");
cy.visit("/users-admin");
cy.wait("@getEmptyUsers");
cy.contains("هیچ کاربری یافت نشد").should("be.visible");
cy.contains("برای شروع یک کاربر ایجاد کنید").should("be.visible");
});
it("should work on mobile viewport", () => {
cy.viewport("iphone-6");
cy.getByTestId("create-user-button").should("be.visible");
cy.getByTestId("create-user-button").click();
cy.contains("ایجاد کاربر جدید").should("be.visible");
// Form should be usable on mobile
cy.getByTestId("first-name-input").type("محمد");
cy.getByTestId("last-name-input").type("احمدی");
cy.getByTestId("phone-number-input").type("09123456789");
cy.getByTestId("submit-button").should("be.visible");
});
it("should be accessible", () => {
cy.getByTestId("create-user-button").click();
// Check for proper labels
cy.get("label").should("have.length.greaterThan", 5);
// Check for required field indicators
cy.getByTestId("first-name-input").should(
"have.attr",
"aria-required",
"true"
);
cy.getByTestId("last-name-input").should(
"have.attr",
"aria-required",
"true"
);
// Check for proper form structure
cy.get("form").should("exist");
cy.get(".bg-gradient-to-r").should("have.length.greaterThan", 1);
});
});