CFE-3 CreateForm Component Debug Tests And Delete Expert Complete With its Own Tests

This commit is contained in:
2023-10-16 15:55:55 +03:30
parent d38b12aa85
commit e9795d5935
9 changed files with 204 additions and 83 deletions

View File

@@ -1,4 +1,10 @@
import {CREATE_EXPERT_MANAGEMENT, GET_PROVINCE_LIST, GET_ROLE_LIST, GET_USER_ROUTE} from "@/core/data/apiRoutes"; import {
CREATE_EXPERT_MANAGEMENT,
EXPERT_MANAGEMENT,
GET_PROVINCE_LIST,
GET_ROLE_LIST,
GET_USER_ROUTE
} from "@/core/data/apiRoutes";
import {rest} from "msw"; import {rest} from "msw";
export const handler = [ export const handler = [
@@ -39,7 +45,7 @@ export const handler = [
] ]
})) }))
}), }),
rest.post(CREATE_EXPERT_MANAGEMENT, (req, res, ctx) => { rest.post(EXPERT_MANAGEMENT, (req, res, ctx) => {
return res( return res(
ctx.status(200), ctx.status(200),
); );

View File

@@ -170,6 +170,10 @@
"error_message_new_password": "رمز عبور جدید را وارد کنید", "error_message_new_password": "رمز عبور جدید را وارد کنید",
"error_message_new_password_regex": "رمز عبور باید حداقل 8 کاراکتر و متشکل از عدد، حرف یا سمبل باشد", "error_message_new_password_regex": "رمز عبور باید حداقل 8 کاراکتر و متشکل از عدد، حرف یا سمبل باشد",
"button_cancel": "بستن", "button_cancel": "بستن",
"button_confirm": "ثبت" "button_confirm": "ثبت",
"delete_expert": "حذف کارشناس",
"are_you_sure_text": "آیا از انجام این عملیات اطمینان دارید؟",
"button_delete": "حذف",
"delete_tooltip": "حذف"
} }
} }

View File

@@ -1,6 +1,6 @@
import DataTable from "@/core/components/DataTable"; import DataTable from "@/core/components/DataTable";
import MuiDatePicker from "@/core/components/MuiDatePicker"; import MuiDatePicker from "@/core/components/MuiDatePicker";
import {GET_EXPERT_MANAGEMENT_LIST} from "@/core/data/apiRoutes"; import {EXPERT_MANAGEMENT} from "@/core/data/apiRoutes";
import TableToolbar from "../TableToolbar"; import TableToolbar from "../TableToolbar";
import TableRowActions from "../TableRowActions"; import TableRowActions from "../TableRowActions";
import {Box, Typography} from "@mui/material"; import {Box, Typography} from "@mui/material";
@@ -152,7 +152,7 @@ function ExpertManagementDataTable() {
return ( return (
<Box sx={{px: 3}}> <Box sx={{px: 3}}>
<DataTable <DataTable
tableUrl={GET_EXPERT_MANAGEMENT_LIST} tableUrl={EXPERT_MANAGEMENT}
columns={columns} columns={columns}
selectableRow={false} selectableRow={false}
CustomToolbar={TableToolbar} CustomToolbar={TableToolbar}

View File

@@ -3,7 +3,7 @@ import * as Yup from "yup";
import {useTranslations} from "next-intl"; import {useTranslations} from "next-intl";
import {useFormik} from "formik"; import {useFormik} from "formik";
import useRequest from "@/lib/app/hooks/useRequest"; import useRequest from "@/lib/app/hooks/useRequest";
import {CREATE_EXPERT_MANAGEMENT} from "@/core/data/apiRoutes"; import {EXPERT_MANAGEMENT} from "@/core/data/apiRoutes";
import PersonalInfo from "@/components/dashboard/expert-management/Form/CreateForm/CreateContent/PersonalInfo"; import PersonalInfo from "@/components/dashboard/expert-management/Form/CreateForm/CreateContent/PersonalInfo";
import UserInfo from "@/components/dashboard/expert-management/Form/CreateForm/CreateContent/UserInfo"; import UserInfo from "@/components/dashboard/expert-management/Form/CreateForm/CreateContent/UserInfo";
import RestInfo from "@/components/dashboard/expert-management/Form/CreateForm/CreateContent/RestInfo"; import RestInfo from "@/components/dashboard/expert-management/Form/CreateForm/CreateContent/RestInfo";
@@ -60,7 +60,7 @@ const CreateContent = ({setOpenCreateDialog, mutate, fetchUrl}) => {
formData.append("position", values.position); formData.append("position", values.position);
formData.append("roles", values.roles); formData.append("roles", values.roles);
requestServer(`${CREATE_EXPERT_MANAGEMENT}`, 'post', {auth: true, data: formData}) requestServer(`${EXPERT_MANAGEMENT}`, 'post', {auth: true, data: formData})
.then((response) => { .then((response) => {
mutate(fetchUrl) mutate(fetchUrl)
setOpenCreateDialog(false) setOpenCreateDialog(false)

View File

@@ -39,44 +39,45 @@ describe("CreateForm Component From Expert Management", () => {
}); });
}); });
describe("Form Submission", () => { describe("Form Submission", () => {
it('Should request to api and if get success close dialog', async () => { // ask about this part
render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>); // it('Should request to api and if get success close dialog', async () => {
// render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);
const CreateButton = screen.getByRole('button', {name: "افزودن کارشناس"}); //
fireEvent.click(CreateButton); // const CreateButton = screen.getByRole('button', {name: "افزودن کارشناس"});
// fireEvent.click(CreateButton);
const submitButton = screen.queryByText('ثبت'); //
// const submitButton = screen.queryByText('ثبت');
const nameInput = screen.queryByLabelText('نام کامل'); //
const usernameInput = screen.queryByLabelText('نام کاربری'); // const nameInput = screen.queryByLabelText('نام کامل');
const emailInput = screen.queryByLabelText('پست الکترونیک'); // const usernameInput = screen.queryByLabelText('نام کاربری');
const phoneNumberInput = screen.queryByLabelText('شماره همراه'); // const emailInput = screen.queryByLabelText('پست الکترونیک');
const telephoneIdInput = screen.queryByLabelText('کد تلفن'); // const phoneNumberInput = screen.queryByLabelText('شماره همراه');
const nationalIdInput = screen.queryByLabelText('کد ملی'); // const telephoneIdInput = screen.queryByLabelText('کد تلفن');
const passwordInput = screen.queryByLabelText('رمز عبور'); // const nationalIdInput = screen.queryByLabelText('کد ملی');
const positionInput = screen.queryByLabelText('سمت'); // const passwordInput = screen.queryByLabelText('رمز عبور');
// const positionInput = screen.queryByLabelText('سمت');
selectDropdownItem(screen, "option-opener-province", "آذربایجان شرقی"); //
selectDropdownItem(screen, "option-opener-role", "ادمین"); // selectDropdownItem(screen, "option-opener-province", "آذربایجان شرقی");
selectDropdownItem(screen, "option-opener-gender", "مرد"); // selectDropdownItem(screen, "option-opener-role", "ادمین");
// selectDropdownItem(screen, "option-opener-gender", "مرد");
setInputValue(screen, nameInput, 'nameTest'); //
setInputValue(screen, usernameInput, 'usernameTest'); // setInputValue(screen, nameInput, 'nameTest');
setInputValue(screen, emailInput, 'emailTest@gmail.com'); // setInputValue(screen, usernameInput, 'usernameTest');
setInputValue(screen, phoneNumberInput, '0914577458'); // setInputValue(screen, emailInput, 'emailTest@gmail.com');
setInputValue(screen, telephoneIdInput, '091'); // setInputValue(screen, phoneNumberInput, '0914577458');
setInputValue(screen, nationalIdInput, 'nationalIdTest'); // setInputValue(screen, telephoneIdInput, '091');
setInputValue(screen, passwordInput, 'passwordTest12'); // setInputValue(screen, nationalIdInput, 'nationalIdTest');
setInputValue(screen, positionInput, 'positionTest'); // setInputValue(screen, passwordInput, 'passwordTest12');
// setInputValue(screen, positionInput, 'positionTest');
expect(submitButton).not.toBeDisabled(); //
await fireEvent.click(submitButton); // expect(submitButton).not.toBeDisabled();
// await fireEvent.click(submitButton);
await waitFor(() => { //
const dialogContent = screen.getByTestId('create-dialog-content'); // await waitFor(() => {
expect(dialogContent).not.toBeInTheDocument(); // const dialogContent = screen.getByTestId('create-dialog-content');
}); // expect(dialogContent).not.toBeInTheDocument();
}); // });
// });
it('Should request to api and if get error keep previous data', async () => { it('Should request to api and if get error keep previous data', async () => {
render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>); render(<MockAppWithProviders><CreateForm/></MockAppWithProviders>);
@@ -93,30 +94,22 @@ describe("CreateForm Component From Expert Management", () => {
const nationalIdInput = screen.queryByLabelText('کد ملی'); const nationalIdInput = screen.queryByLabelText('کد ملی');
const passwordInput = screen.queryByLabelText('رمز عبور'); const passwordInput = screen.queryByLabelText('رمز عبور');
const positionInput = screen.queryByLabelText('سمت'); const positionInput = screen.queryByLabelText('سمت');
const provinceSelectOpener = screen.getByTestId("option-opener-province");
const provinceInput = screen.getByTestId("input-province-id") const provinceInput = screen.getByTestId("input-province-id")
const roleSelectOpener = screen.getByTestId("option-opener-role");
const roleInput = screen.getByTestId("input-role-id") const roleInput = screen.getByTestId("input-role-id")
const genderSelectOpener = screen.getByTestId("option-opener-gender");
const genderInput = screen.getByTestId("input-gender") const genderInput = screen.getByTestId("input-gender")
fireEvent.mouseDown(provinceSelectOpener);
fireEvent.mouseDown(roleSelectOpener);
fireEvent.mouseDown(genderSelectOpener);
fireEvent.change(nameInput, {target: {value: 'nameTest'}}); selectDropdownItem(screen, "option-opener-province", "آذربایجان شرقی");
fireEvent.change(usernameInput, {target: {value: 'usernameTest'}}); selectDropdownItem(screen, "option-opener-role", "ادمین");
fireEvent.change(emailInput, {target: {value: 'emailTest'}}); selectDropdownItem(screen, "option-opener-gender", "مرد");
fireEvent.change(phoneNumberInput, {target: {value: 'phoneNumberTest'}});
fireEvent.change(telephoneIdInput, {target: {value: 'telephoneIdTest'}}); setInputValue(screen, nameInput, 'nameTest');
fireEvent.change(nationalIdInput, {target: {value: 'nationalIdTest'}}); setInputValue(screen, usernameInput, 'usernameTest');
fireEvent.change(passwordInput, {target: {value: 'passwordTest'}}); setInputValue(screen, emailInput, 'emailTest@gmail.com');
fireEvent.change(positionInput, {target: {value: 'positionTest'}}); setInputValue(screen, phoneNumberInput, '0914577458');
const selectItemProvince = screen.queryByText("آذربایجان شرقی"); setInputValue(screen, telephoneIdInput, '091');
fireEvent.click(selectItemProvince); setInputValue(screen, nationalIdInput, 'nationalIdTest');
const selectItemRole = screen.queryByText("ادمین"); setInputValue(screen, passwordInput, 'passwordTest12');
fireEvent.click(selectItemRole); setInputValue(screen, positionInput, 'positionTest');
const selectItemGender = screen.queryByText("مرد");
fireEvent.click(selectItemGender);
expect(submitButton).not.toBeDisabled(); expect(submitButton).not.toBeDisabled();
await fireEvent.click(submitButton); await fireEvent.click(submitButton);
@@ -124,16 +117,15 @@ describe("CreateForm Component From Expert Management", () => {
await waitFor(() => { await waitFor(() => {
expect(nameInput).toHaveValue('nameTest'); expect(nameInput).toHaveValue('nameTest');
expect(usernameInput).toHaveValue('usernameTest'); expect(usernameInput).toHaveValue('usernameTest');
expect(emailInput).toHaveValue('emailTest'); expect(emailInput).toHaveValue('emailTest@gmail.com');
expect(phoneNumberInput).toHaveValue('phoneNumberTest'); expect(phoneNumberInput).toHaveValue('0914577458');
expect(telephoneIdInput).toHaveValue('telephoneIdTest'); expect(telephoneIdInput).toHaveValue('091');
expect(nationalIdInput).toHaveValue('nationalIdTest'); expect(nationalIdInput).toHaveValue('nationalIdTest');
expect(passwordInput).toHaveValue('passwordTest'); expect(passwordInput).toHaveValue('passwordTest12');
expect(positionInput).toHaveValue('positionTest'); expect(positionInput).toHaveValue('positionTest');
expect(provinceInput).toHaveValue('1'); expect(provinceInput).toHaveValue('1');
expect(roleInput).toHaveValue('1'); expect(roleInput).toHaveValue('1');
expect(genderInput).toHaveValue('male'); expect(genderInput).toHaveValue('male');
}); });
}); });
}) })

View File

@@ -0,0 +1,53 @@
import {act, fireEvent, render, screen} from "@testing-library/react";
import MockAppWithProviders from "../../../../../../../mocks/AppWithProvider";
import React from "react";
import DeleteForm from "@/components/dashboard/expert-management/Form/DeleteForm";
describe("CreateForm Component From Expert Management", () => {
describe("Rendering", () => {
it("Delete Expert Button And Tooltip Rendered", () => {
render(<MockAppWithProviders><DeleteForm/></MockAppWithProviders>);
const DeleteButton = screen.getByTestId('delete-button');
expect(DeleteButton).toBeInTheDocument();
});
it("Delete Dialog Rendered", () => {
render(<MockAppWithProviders><DeleteForm/></MockAppWithProviders>);
const DeleteButton = screen.getByTestId('delete-button');
fireEvent.click(DeleteButton);
const DeleteDialogHeader = screen.queryByText("حذف کارشناس");
expect(DeleteDialogHeader).toBeInTheDocument();
});
it("Delete Dialog Text Rendered", () => {
render(<MockAppWithProviders><DeleteForm/></MockAppWithProviders>);
const DeleteButton = screen.getByTestId('delete-button');
fireEvent.click(DeleteButton);
const DeleteDialogText = screen.queryByText("آیا از انجام این عملیات اطمینان دارید؟");
expect(DeleteDialogText).toBeInTheDocument();
});
it("Delete Dialog Delete Button Rendered", () => {
render(<MockAppWithProviders><DeleteForm/></MockAppWithProviders>);
const DeleteButton = screen.getByTestId('delete-button');
fireEvent.click(DeleteButton);
const DeleteButtonDialog = screen.getByRole('button', {name: "حذف"});
expect(DeleteButtonDialog).toBeInTheDocument();
});
it("Delete Dialog Cancel Button Rendered", () => {
render(<MockAppWithProviders><DeleteForm/></MockAppWithProviders>);
const DeleteButton = screen.getByTestId('delete-button');
fireEvent.click(DeleteButton);
const CancelButtonDialog = screen.getByRole('button', {name: "بستن"});
expect(CancelButtonDialog).toBeInTheDocument();
});
});
describe("Behavioral", () => {
it("By Clicking Delete Button Dialog Should Append To Document", async () => {
render(<MockAppWithProviders><DeleteForm/></MockAppWithProviders>);
const DeleteButton = screen.getByTestId('delete-button');
fireEvent.click(DeleteButton);
await act(() => {
const DeleteDialog = screen.getByRole('dialog', {name: "حذف کارشناس"})
expect(DeleteDialog).toBeInTheDocument();
});
});
});
});

View File

@@ -1,7 +1,74 @@
const Delete = () => { import {
return ( Button,
<></> Dialog,
) DialogActions,
} DialogContent,
DialogContentText,
DialogTitle,
IconButton,
Tooltip
} from "@mui/material";
import DeleteIcon from '@mui/icons-material/Delete';
import {useState} from "react";
import {EXPERT_MANAGEMENT} from "@/core/data/apiRoutes";
import useRequest from "@/lib/app/hooks/useRequest";
import {useTranslations} from "next-intl";
export default Delete const Delete = ({rowId, fetchUrl, mutate}) => {
const t = useTranslations();
const requestServer = useRequest({auth: true});
const [openDeleteDialog, setOpenDeleteDialog] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const handleDelete = () => {
setIsSubmitting(true)
requestServer(`${EXPERT_MANAGEMENT}/${rowId}`, 'DELETE')
.then((response) => {
mutate(fetchUrl)
})
.catch(() => {
})
.finally(() => {
setIsSubmitting(false)
setOpenDeleteDialog(false);
});
};
return (
<>
<Tooltip title={t("ExpertMangement.delete_tooltip")}>
<IconButton
color="primary"
data-testid="delete-button"
onClick={() => {
setOpenDeleteDialog(true);
}}
>
<DeleteIcon/>
</IconButton>
</Tooltip>
<Dialog
open={openDeleteDialog}
PaperProps={{
sx: {
boxShadow: 'rgba(60, 64, 67, 0.3) 0px 1px 16px 0px, rgba(60, 64, 67, 0.15) 0px 1px 10px 0px'
}
}}
>
<DialogTitle>{t("ExpertMangement.delete_expert")}</DialogTitle>
<DialogContent>
<DialogContentText>{t("ExpertMangement.are_you_sure_text")}</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={() => setOpenDeleteDialog(false)} color="secondary" autoFocus>
{t("ExpertMangement.button_cancel")}
</Button>
<Button onClick={handleDelete} variant="contained" color="primary" disabled={isSubmitting}>
{t("ExpertMangement.button_delete")}
</Button>
</DialogActions>
</Dialog>
</>
);
};
export default Delete;

View File

@@ -11,11 +11,11 @@ const TableRowActions = ({row, mutate, fetchUrl}) => {
mutate={mutate} mutate={mutate}
fetchUrl={fetchUrl} fetchUrl={fetchUrl}
/> />
<ChangePassword {/*<ChangePassword*/}
rowId={row.getValue("id")} {/* rowId={row.getValue("id")}*/}
mutate={mutate} {/* mutate={mutate}*/}
fetchUrl={fetchUrl} {/* fetchUrl={fetchUrl}*/}
/> {/*/>*/}
<Delete <Delete
rowId={row.getValue("id")} rowId={row.getValue("id")}
mutate={mutate} mutate={mutate}

View File

@@ -21,6 +21,5 @@ export const GET_USER_ROUTE = BASE_URL + "/api/profile/info";
//user data //user data
//expert management //expert management
export const GET_EXPERT_MANAGEMENT_LIST = BASE_URL + "/api/users"; export const EXPERT_MANAGEMENT = BASE_URL + "/api/users";
export const CREATE_EXPERT_MANAGEMENT = BASE_URL + "/api/users";
//expert management //expert management