From f646c49a1c6a316d9aa74596b9c85ba53aee915d Mon Sep 17 00:00:00 2001 From: Bohdan Date: Wed, 31 Jul 2024 15:21:20 +0300 Subject: [PATCH] implement some code feature --- .../AddProfessionalCategoryModal.tsx | 32 ++++++++++++++----- .../AddProfessionalCategoryModal.spec.jsx | 30 ----------------- 2 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.tsx b/src/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.tsx index 4fa7846ac..abcbf2c10 100644 --- a/src/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.tsx +++ b/src/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.tsx @@ -57,7 +57,7 @@ function SubjectGroup({ ) const handleDisableOptions = (option: Partial) => { - return disableOptions.some((subject) => subject._id === option._id) + return disableOptions.some((subjects) => subjects._id === option._id) } return ( @@ -132,18 +132,34 @@ const AddProfessionalCategoryModal: FC = ({ }) ) } else { + // Ensure all necessary fields are populated const categoryToAdd: UserMainSubject = { _id: uuidv4(), isDeletionBlocked, ...data } - dispatch( - addCategory({ - category: categoryToAdd, - userRole: userRoleCategory - }) - ) + + if (!categoryToAdd.category?._id || !categoryToAdd.subjects.length) { + console.error( + 'Required fields are missing for the new category:', + categoryToAdd + ) + return + } + + try { + dispatch( + addCategory({ + category: categoryToAdd, + userRole: userRoleCategory + }) + ) + } catch (error) { + console.error('Failed to add category:', error) + // Optionally, show an error message to the user + } } + closeModal() } @@ -201,7 +217,7 @@ const AddProfessionalCategoryModal: FC = ({ const handleBlockOption = (option: CategoryNameInterface) => { const isCurrent = option._id !== data.category?._id const isBlocked = blockedCategoriesOptions.some( - (mainSubject) => mainSubject.category?._id === option._id + (mainSubjects) => mainSubjects.category?._id === option._id ) return isBlocked && isCurrent } diff --git a/tests/unit/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.spec.jsx b/tests/unit/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.spec.jsx index f0305e17b..022317453 100644 --- a/tests/unit/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.spec.jsx +++ b/tests/unit/containers/edit-profile/professional-info-tab/add-professional-category-modal/AddProfessionalCategoryModal.spec.jsx @@ -204,34 +204,4 @@ describe('AddProfessionalCategoryModal with initial value', () => { expect(mockCloseModal).toHaveBeenCalled() }) - - it('should render correctly with empty blockedCategoriesOptions', async () => { - renderWithProviders( - - ) - - const categoryAutocomplete = screen.getByLabelText( - /editProfilePage.profile.professionalTab.mainStudyCategory/ - ) - - expect(categoryAutocomplete).toBeInTheDocument() - }) - - it('should show error message if API call fails', async () => { - mockAxiosClient.onGet(URLs.categories.getNames).reply(500) - renderWithProviders( - - ) - - await waitFor(() => { - const errorMessage = screen.getByText(/error/i) - expect(errorMessage).toBeInTheDocument() - }) - }) })