Files
salona/tests/Factor.test.tsx
farnoosh-krm ed3906fad6 add tests and UI modified
(fix): add appointment code in requests and profile completion reward
(test): add test coverage for multiple pages and components

(fix) - Added appointment code to the Requests section
(fix) - Added "Claim Gift" feature for completing the profile

(test) Added tests for the following pages/components:
1- Active Devices
2- Add Service
3- Appointment Request (cards display section)
4- Create Gift Card
5- Customer List component (used in Quick Booking, Advanced Booking, etc.)
6- Customer List in Customer Club
7- Delete and Edit buttons (used across the entire app)
8- Invoice
9- Create Individual Schedule Table
10- Notification SMS in SMS Center
11- Loyalty Card List
12- Loyal Customers in ResAssistant
13- Map component
14- Staff List in Staff Management
15- Services List (used in General Link, Custom Link, Advanced Booking, etc.)
16- Services List (used in Quick Booking, Gift Card, Discount Center, etc.)
2026-04-13 16:32:38 +03:30

228 lines
5.1 KiB
TypeScript

import Factor from "@/apps/new-ui/pages/ReportAll/factor";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import * as FactorService from "@/apps/new-ui/services/Factor";
jest.mock("@/apps/new-ui/services/User", () => ({
useGetUserInfo: () => ({
data: {
current_user: {
id: 1,
name: "test user",
phone_number: "09120000000",
profile_picture: "",
user_id: "XX1",
address: "",
city: "",
province: "",
latitude: 0,
longitude: 0,
wallet_balance: 0,
sms_balance: 0,
services: [],
score: 0,
specialty: "",
date_of_birth: "",
},
feeling_status: "",
profile_complete: {
active_time: null,
has_profile_action: false
},
user_abilities_list: [
{}, {}, {},
{
ability_action: "accountant_service",
active: true
}
]
},
isLoading: false
})
}));
/* ===========================
mocks: router
=========================== */
jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: () => jest.fn(),
useLocation: () => ({
pathname: "/factor",
search: "",
}),
}));
/* ===========================
mocks: services
=========================== */
jest.mock("@/apps/new-ui/services/Factor", () => ({
useGetCombinationAccountingData: jest.fn(),
useDownloadTransactionsExcel: () => ({
mutate: jest.fn(),
}),
useGetFinancialSummary: () => ({
data: null,
isLoading: false,
}),
}));
jest.mock("@/apps/new-ui/services/IncomeAndCost", () => ({
useDeleteIncome: () => ({
mutate: jest.fn(),
}),
useDeleteExpense: () => ({
mutate: jest.fn(),
}),
}));
/* ===========================
mocks: components
=========================== */
jest.mock("@/apps/new-ui/pages/ReportAll/+components/Header.tsx", () => () => (
<div data-testid="header" />
));
jest.mock(
"@/apps/new-ui/pages/ReportAll/factor/+components/Loader.tsx",
() =>
({ text }: any) => <div data-testid="loader">{text}</div>,
);
jest.mock(
"@/apps/new-ui/pages/ReportAll/+components/EmptyState.tsx",
() =>
({ text }: any) => <div data-testid="empty">{text}</div>,
);
jest.mock(
"@/apps/new-ui/pages/ReportAll/factor/+components/IncomeSwipper.tsx",
() => () => <div data-testid="income-swipper" />,
);
jest.mock(
"@/apps/new-ui/components/SubscriptionWrapper",
() =>
({ children }: any) => <div>{children}</div>,
);
jest.mock(
"@/apps/new-ui/pages/ReportAll/+components/CardList.tsx",
() =>
({ items }: any) => (
<div data-testid="card-list">
{items.map((it: any) => (
<div key={it.id}>{it.description}</div>
))}
</div>
),
);
jest.mock(
"@/apps/new-ui/components/CustomeComponents/ButtonActionsExcelIcon.tsx",
() =>
({ onCreate }: any) => (
<button data-testid="excel-btn" onClick={onCreate}>
download
</button>
),
);
/* ===========================
helpers
=========================== */
const mockUseGetCombinationAccountingData =
FactorService.useGetCombinationAccountingData as jest.Mock;
/* ===========================
tests
=========================== */
describe("Factor page", () => {
afterEach(() => {
jest.clearAllMocks();
});
it("renders loader when loading", () => {
mockUseGetCombinationAccountingData.mockReturnValue({
data: {
transactions: [
{ id: 1, type: "income", description: "درآمد تست", amount: 100 },
{ id: 2, type: "expense", description: "هزینه تست", amount: 50 }
]
},
isLoading: false,
isError: false,
refetch: jest.fn(),
});
render(
<MemoryRouter>
<Factor />
</MemoryRouter>,
);
expect(screen.getByTestId("loader")).toBeInTheDocument();
});
it("renders empty state when no transactions exist", () => {
mockUseGetCombinationAccountingData.mockReturnValue({
data: { transactions: [] },
isLoading: false,
isError: false,
refetch: jest.fn(),
});
render(
<MemoryRouter>
<Factor />
</MemoryRouter>,
);
expect(
screen.getByText("هنوز درآمد یا هزینه‌ای ثبت نشده است."),
).toBeInTheDocument();
});
it("renders card list when transactions exist", async () => {
mockUseGetCombinationAccountingData.mockReturnValue({
data: {
transactions: [
{
id: 1,
description: "درآمد تست",
type: "income",
amount: 1000,
date: "1405-01-15",
},
{
id: 2,
description: "هزینه تست",
type: "expense",
amount: 500,
date: "1405-01-10",
},
],
},
isLoading: false,
isError: false,
refetch: jest.fn(),
});
render(
<MemoryRouter>
<Factor />
</MemoryRouter>
);
expect(await screen.findByText("درآمد تست")).toBeInTheDocument();
expect(await screen.findByText("هزینه تست")).toBeInTheDocument();
});
});