From 6a79d5133c990ac8b2914fb4f27f95c0ccb3fe92 Mon Sep 17 00:00:00 2001 From: meetul Date: Mon, 28 Oct 2024 12:52:20 +0530 Subject: [PATCH] make coderabbit suggested changes --- public/locales/en/translation.json | 3 +- public/locales/fr/translation.json | 3 +- public/locales/hi/translation.json | 3 +- public/locales/sp/translation.json | 3 +- public/locales/zh/translation.json | 3 +- src/components/TagActions/TagActions.test.tsx | 10 ++-- src/components/TagActions/TagActions.tsx | 59 +++++++------------ src/components/TagActions/TagActionsMocks.ts | 15 ++--- src/screens/ManageTag/ManageTag.test.tsx | 7 +++ src/screens/ManageTag/ManageTag.tsx | 18 ++++-- 10 files changed, 61 insertions(+), 63 deletions(-) diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json index 2485fec3e4..19bea8e4b4 100644 --- a/public/locales/en/translation.json +++ b/public/locales/en/translation.json @@ -352,7 +352,8 @@ "tagName": "Name", "tagUpdationSuccess": "Tag updated successfully", "tagRemovalSuccess": "Tag deleted successfully", - "noTagSelected": "No Tag Selected" + "noTagSelected": "No Tag Selected", + "changeNameToEdit": "Change the name to make an update" }, "userListCard": { "addAdmin": "Add Admin", diff --git a/public/locales/fr/translation.json b/public/locales/fr/translation.json index 5d1cf583a8..b961c587ff 100644 --- a/public/locales/fr/translation.json +++ b/public/locales/fr/translation.json @@ -352,7 +352,8 @@ "tagName": "Nom", "tagUpdationSuccess": "Étiquette mise à jour avec succès", "tagRemovalSuccess": "Étiquette supprimée avec succès", - "noTagSelected": "Aucun tag sélectionné" + "noTagSelected": "Aucun tag sélectionné", + "changeNameToEdit": "Modifiez le nom pour faire une mise à jour" }, "userListCard": { "addAdmin": "Ajouter un administrateur", diff --git a/public/locales/hi/translation.json b/public/locales/hi/translation.json index cdc1d5e52b..940be5a73c 100644 --- a/public/locales/hi/translation.json +++ b/public/locales/hi/translation.json @@ -352,7 +352,8 @@ "tagName": "नाम", "tagUpdationSuccess": "टैग सफलतापूर्वक अपडेट की गई", "tagRemovalSuccess": "टैग सफलतापूर्वक हटाई गई", - "noTagSelected": "कोई टैग चयनित नहीं" + "noTagSelected": "कोई टैग चयनित नहीं", + "changeNameToEdit": "अपडेट करने के लिए नाम बदलें" }, "userListCard": { "addAdmin": "व्यवस्थापक जोड़ें", diff --git a/public/locales/sp/translation.json b/public/locales/sp/translation.json index fc6d9eb839..4e65b4f8d2 100644 --- a/public/locales/sp/translation.json +++ b/public/locales/sp/translation.json @@ -352,7 +352,8 @@ "tagName": "Nombre", "tagUpdationSuccess": "Etiqueta actualizada con éxito", "tagRemovalSuccess": "Etiqueta eliminada con éxito", - "noTagSelected": "Ninguna etiqueta seleccionada" + "noTagSelected": "Ninguna etiqueta seleccionada", + "changeNameToEdit": "Cambia el nombre para hacer una actualización" }, "userListCard": { "joined": "Unido", diff --git a/public/locales/zh/translation.json b/public/locales/zh/translation.json index a264056547..d6fe494a04 100644 --- a/public/locales/zh/translation.json +++ b/public/locales/zh/translation.json @@ -352,7 +352,8 @@ "tagName": "名称", "tagUpdationSuccess": "标签更新成功", "tagRemovalSuccess": "标签删除成功", - "noTagSelected": "未选择标签" + "noTagSelected": "未选择标签", + "changeNameToEdit": "更改名称以进行更新" }, "userListCard": { "addAdmin": "添加管理员", diff --git a/src/components/TagActions/TagActions.test.tsx b/src/components/TagActions/TagActions.test.tsx index 3f44b314ce..014748a913 100644 --- a/src/components/TagActions/TagActions.test.tsx +++ b/src/components/TagActions/TagActions.test.tsx @@ -7,6 +7,7 @@ import { fireEvent, cleanup, waitFor, + act, } from '@testing-library/react'; import { Provider } from 'react-redux'; import { MemoryRouter, Route, Routes } from 'react-router-dom'; @@ -26,10 +27,11 @@ import { MOCKS, MOCKS_ERROR } from './TagActionsMocks'; const link = new StaticMockLink(MOCKS, true); const link2 = new StaticMockLink(MOCKS_ERROR, true); -async function wait(): Promise { - await waitFor(() => { - // The waitFor utility automatically uses optimal timing - return Promise.resolve(); +async function wait(ms = 500): Promise { + await act(() => { + return new Promise((resolve) => { + setTimeout(resolve, ms); + }); }); } diff --git a/src/components/TagActions/TagActions.tsx b/src/components/TagActions/TagActions.tsx index a8c0871053..b5c219aa3d 100644 --- a/src/components/TagActions/TagActions.tsx +++ b/src/components/TagActions/TagActions.tsx @@ -82,7 +82,7 @@ const TagActions: React.FC = ({ skip: !assignToTagsModalIsOpen, }); - const userTagsList = orgUserTagsData?.organizations[0].userTags.edges.map( + const userTagsList = orgUserTagsData?.organizations[0]?.userTags.edges.map( (edge) => edge.node, ); @@ -246,49 +246,32 @@ const TagActions: React.FC = ({ }; const [assignToTags] = useMutation(ASSIGN_TO_TAGS); - - const assignToSelectedTags = async ( - e: FormEvent, - ): Promise => { - e.preventDefault(); - - try { - const { data } = await assignToTags({ - variables: { - currentTagId, - selectedTagIds: selectedTags.map((selectedTag) => selectedTag._id), - }, - }); - - if (data) { - toast.success(t('successfullyAssignedToTags')); - hideAssignToTagsModal(); - } - } catch (error: unknown) { - /* istanbul ignore next */ - if (error instanceof Error) { - toast.error(error.message); - } - } - }; - const [removeFromTags] = useMutation(REMOVE_FROM_TAGS); - const removeFromSelectedTags = async ( + const handleTagAction = async ( e: FormEvent, ): Promise => { e.preventDefault(); + const mutationObject = { + variables: { + currentTagId, + selectedTagIds: selectedTags.map((selectedTag) => selectedTag._id), + }, + }; + try { - const { data } = await removeFromTags({ - variables: { - currentTagId, - selectedTagIds: selectedTags.map((selectedTag) => selectedTag._id), - }, - }); + const { data } = + tagActionType === 'assignToTags' + ? await assignToTags(mutationObject) + : await removeFromTags(mutationObject); if (data) { - toast.success(t('successfullyRemovedFromTags')); + if (tagActionType === 'assignToTags') { + toast.success(t('successfullyAssignedToTags')); + } else { + toast.success(t('successfullyRemovedFromTags')); + } hideAssignToTagsModal(); } } catch (error: unknown) { @@ -306,8 +289,6 @@ const TagActions: React.FC = ({
{t('errorOccurredWhileLoadingOrganizationUserTags')} -
- {orgUserTagsError.message}
@@ -337,8 +318,8 @@ const TagActions: React.FC = ({
tagActionType === 'assignToTags' - ? assignToSelectedTags(e) - : removeFromSelectedTags(e) + ? handleTagAction(e) + : handleTagAction(e) } > diff --git a/src/components/TagActions/TagActionsMocks.ts b/src/components/TagActions/TagActionsMocks.ts index c7a9c80ee2..46ef0b85db 100644 --- a/src/components/TagActions/TagActionsMocks.ts +++ b/src/components/TagActions/TagActionsMocks.ts @@ -366,8 +366,8 @@ export const MOCKS = [ }, ], pageInfo: { - startCursor: '1', - endCursor: '10', + startCursor: 'subTag1', + endCursor: 'subTag10', hasNextPage: true, hasPreviousPage: false, }, @@ -382,7 +382,7 @@ export const MOCKS = [ query: USER_TAG_SUB_TAGS, variables: { id: '1', - after: '10', + after: 'subTag10', first: TAGS_QUERY_LIMIT, }, }, @@ -407,8 +407,8 @@ export const MOCKS = [ }, ], pageInfo: { - startCursor: '11', - endCursor: '11', + startCursor: 'subTag11', + endCursor: 'subTag11', hasNextPage: false, hasPreviousPage: true, }, @@ -458,10 +458,7 @@ export const MOCKS_ERROR = [ query: ORGANIZATION_USER_TAGS_LIST, variables: { id: '123', - after: null, - before: null, - first: 5, - last: null, + first: TAGS_QUERY_LIMIT, }, }, error: new Error('Mock Graphql Error'), diff --git a/src/screens/ManageTag/ManageTag.test.tsx b/src/screens/ManageTag/ManageTag.test.tsx index a27ebad8b4..5de5e97c88 100644 --- a/src/screens/ManageTag/ManageTag.test.tsx +++ b/src/screens/ManageTag/ManageTag.test.tsx @@ -49,6 +49,7 @@ async function wait(ms = 500): Promise { jest.mock('react-toastify', () => ({ toast: { success: jest.fn(), + info: jest.fn(), error: jest.fn(), }, })); @@ -394,6 +395,12 @@ describe('Manage Tag Page', () => { }); userEvent.click(screen.getByTestId('editTag')); + userEvent.click(screen.getByTestId('editTagSubmitBtn')); + + await waitFor(() => { + expect(toast.info).toHaveBeenCalledWith(translations.changeNameToEdit); + }); + const tagNameInput = screen.getByTestId('tagNameInput'); await userEvent.clear(tagNameInput); await userEvent.type(tagNameInput, 'tag 1 edited'); diff --git a/src/screens/ManageTag/ManageTag.tsx b/src/screens/ManageTag/ManageTag.tsx index b861ff962b..8099ca8754 100644 --- a/src/screens/ManageTag/ManageTag.tsx +++ b/src/screens/ManageTag/ManageTag.tsx @@ -87,6 +87,10 @@ function ManageTag(): JSX.Element { setAssignToTagsModalIsOpen(false); }; + const showEditTagModal = (): void => { + setEditTagModalIsOpen(true); + }; + const hideEditTagModal = (): void => { setEditTagModalIsOpen(false); }; @@ -159,6 +163,7 @@ function ManageTag(): JSX.Element { const [edit] = useMutation(UPDATE_USER_TAG); const [newTagName, setNewTagName] = useState(''); + const currentTagName = userTagAssignedMembersData?.getUserTag.name ?? ''; useEffect(() => { setNewTagName(userTagAssignedMembersData?.getUserTag.name ?? ''); @@ -167,6 +172,11 @@ function ManageTag(): JSX.Element { const editTag = async (e: FormEvent): Promise => { e.preventDefault(); + if (newTagName === currentTagName) { + toast.info(t('changeNameToEdit')); + return; + } + try { const { data } = await edit({ variables: { @@ -524,18 +534,14 @@ function ManageTag(): JSX.Element {
{ - setEditTagModalIsOpen(true); - }} + onClick={showEditTagModal} className="ms-5 mt-3 mb-2 btn btn-primary btn-sm w-75" data-testid="editTag" > {tCommon('edit')}
{ - setRemoveTagModalIsOpen(true); - }} + onClick={toggleRemoveUserTagModal} className="ms-5 mb-2 btn btn-danger btn-sm w-75" data-testid="removeTag" >