From 87d508cfcdcac581373eafb216d3e699da01259f Mon Sep 17 00:00:00 2001 From: Christofer Date: Mon, 11 Dec 2023 22:11:53 +0000 Subject: [PATCH] refactor: Fix typing nits and refactor delete mutation --- src/content-tags-drawer/data/apiHooks.jsx | 6 +-- src/content-tags-drawer/data/types.mjs | 5 --- src/taxonomy/TaxonomyLayout.test.jsx | 2 +- src/taxonomy/TaxonomyListPage.jsx | 29 ++++++--------- src/taxonomy/TaxonomyListPage.test.jsx | 37 ++++++++----------- src/taxonomy/data/apiHooks.jsx | 17 +++------ src/taxonomy/data/apiHooks.test.jsx | 2 +- src/taxonomy/data/types.mjs | 16 -------- .../taxonomy-detail/TaxonomyDetailPage.jsx | 27 +++++++------- .../TaxonomyDetailPage.test.jsx | 34 +++++------------ 10 files changed, 60 insertions(+), 115 deletions(-) diff --git a/src/content-tags-drawer/data/apiHooks.jsx b/src/content-tags-drawer/data/apiHooks.jsx index 099c88129d..d30c3db007 100644 --- a/src/content-tags-drawer/data/apiHooks.jsx +++ b/src/content-tags-drawer/data/apiHooks.jsx @@ -7,7 +7,7 @@ import { getTaxonomyTagsData, getContentTaxonomyTagsData, getContentData } from * @param {string} taxonomyId The id of the taxonomy to fetch tags for * @param {string} fullPathProvided Optional param that contains the full URL to fetch data * If provided, we use it instead of generating the URL. This is usually for fetching subTags - * @returns {import("./types.mjs").UseQueryResult} + * @returns {import('@tanstack/react-query').UseQueryResult} */ const useTaxonomyTagsData = (taxonomyId, fullPathProvided) => ( useQuery({ @@ -45,7 +45,7 @@ export const useIsTaxonomyTagsDataLoaded = (taxonomyId, fullPathProvided) => ( /** * Builds the query to get the taxonomy tags applied to the content object * @param {string} contentId The id of the content object to fetch the applied tags for - * @returns {import("./types.mjs").UseQueryResult} + * @returns {import('@tanstack/react-query').UseQueryResult} */ const useContentTaxonomyTagsData = (contentId) => ( useQuery({ @@ -79,7 +79,7 @@ export const useIsContentTaxonomyTagsDataLoaded = (contentId) => ( /** * Builds the query to get meta data about the content object * @param {string} contentId The id of the content object (unit/component) - * @returns {import("./types.mjs").UseQueryResult} + * @returns {import('@tanstack/react-query').UseQueryResult} */ const useContentData = (contentId) => ( useQuery({ diff --git a/src/content-tags-drawer/data/types.mjs b/src/content-tags-drawer/data/types.mjs index 00b3fefd4c..c16cb45ea6 100644 --- a/src/content-tags-drawer/data/types.mjs +++ b/src/content-tags-drawer/data/types.mjs @@ -100,8 +100,3 @@ * @property {TaxonomyTagData[]} results */ -/** - * @typedef {Object} UseQueryResult - * @property {Object} data - * @property {string} status - */ diff --git a/src/taxonomy/TaxonomyLayout.test.jsx b/src/taxonomy/TaxonomyLayout.test.jsx index e11e85b02a..d833809997 100644 --- a/src/taxonomy/TaxonomyLayout.test.jsx +++ b/src/taxonomy/TaxonomyLayout.test.jsx @@ -55,7 +55,7 @@ describe('', async () => { expect(getByTestId('mock-footer')).toBeInTheDocument(); }); - it('shoul show toast', async () => { + it('should show toast', async () => { const { getByTestId, getByText } = render(); act(() => { expect(getByTestId('taxonomy-toast')).toBeInTheDocument(); diff --git a/src/taxonomy/TaxonomyListPage.jsx b/src/taxonomy/TaxonomyListPage.jsx index a9f729ac2d..6f23b90d8d 100644 --- a/src/taxonomy/TaxonomyListPage.jsx +++ b/src/taxonomy/TaxonomyListPage.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useContext } from 'react'; +import React, { useContext } from 'react'; import { CardView, Container, @@ -17,14 +17,19 @@ import { TaxonomyContext } from './common/context'; const TaxonomyListPage = () => { const intl = useIntl(); - const [deletedTaxonomyName, setDeletedTaxonomyName] = useState(''); - const deleteMutation = useDeleteTaxonomy(); + const deleteTaxonomy = useDeleteTaxonomy(); const { setToastMessage } = useContext(TaxonomyContext); - const onDeleteTaxonomy = (id, name) => { - setDeletedTaxonomyName(name); - deleteMutation.mutate({ pk: id }); - }; + const onDeleteTaxonomy = React.useCallback((id, name) => { + deleteTaxonomy({ pk: id }, { + onSuccess: async () => { + setToastMessage(intl.formatMessage(messages.taxonomyDeleteToast, { name })); + }, + onError: async () => { + // TODO: display the error to the user + }, + }); + }, [setToastMessage]); const useTaxonomyListData = () => { const taxonomyListData = useTaxonomyListDataResponse(); @@ -33,16 +38,6 @@ const TaxonomyListPage = () => { }; const { taxonomyListData, isLoaded } = useTaxonomyListData(); - // Verifies the status after run the delete mutation. - // Shows the toast on success or error. - useEffect(() => { - if (deleteMutation.status === 'success') { - setToastMessage(intl.formatMessage(messages.taxonomyDeleteToast, { name: deletedTaxonomyName })); - } else if (deleteMutation.status === 'error') { - setToastMessage(deleteMutation.error.message); - } - }, [deleteMutation.status]); - const getHeaderButtons = () => ( // Download template and import buttons. // TODO Add functionality to this buttons. diff --git a/src/taxonomy/TaxonomyListPage.test.jsx b/src/taxonomy/TaxonomyListPage.test.jsx index 1d61d3f8b2..17c95034e2 100644 --- a/src/taxonomy/TaxonomyListPage.test.jsx +++ b/src/taxonomy/TaxonomyListPage.test.jsx @@ -2,16 +2,17 @@ import React, { useMemo } from 'react'; import { IntlProvider, injectIntl } from '@edx/frontend-platform/i18n'; import { initializeMockApp } from '@edx/frontend-platform'; import { AppProvider } from '@edx/frontend-platform/react'; -import { act, render } from '@testing-library/react'; +import { act, render, fireEvent } from '@testing-library/react'; import initializeStore from '../store'; import TaxonomyListPage from './TaxonomyListPage'; -import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded, useDeleteTaxonomy } from './data/apiHooks'; +import { useTaxonomyListDataResponse, useIsTaxonomyListDataLoaded } from './data/apiHooks'; import { TaxonomyContext } from './common/context'; let store; const mockSetToastMessage = jest.fn(); +const mockDeleteTaxonomy = jest.fn(); const taxonomies = [{ id: 1, name: 'Taxonomy', @@ -21,8 +22,12 @@ const taxonomies = [{ jest.mock('./data/apiHooks', () => ({ useTaxonomyListDataResponse: jest.fn(), useIsTaxonomyListDataLoaded: jest.fn(), - useDeleteTaxonomy: jest.fn(), + useDeleteTaxonomy: () => mockDeleteTaxonomy, })); +jest.mock('./taxonomy-card/TaxonomyCardMenu', () => jest.fn(({ onClickMenuItem }) => ( + // eslint-disable-next-line jsx-a11y/control-has-associated-label +