From ced2c0e891fa93d3aabe5ea3b1573005b3ba9a46 Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Mon, 8 Apr 2024 00:34:40 -0700 Subject: [PATCH] Fix the TaxonomyMenu test, which wasn't getting run at all (#930) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: fix the TaxonomyMenu test, which wasn't getting run at all Co-authored-by: RĂ´mulo Penido --- src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx | 10 ++++ .../taxonomy-menu/TaxonomyMenu.test.jsx | 59 +++++++++++-------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx b/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx index c1e611ec67..31d2e0c76d 100644 --- a/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx +++ b/src/taxonomy/taxonomy-menu/TaxonomyMenu.jsx @@ -21,6 +21,16 @@ import { ImportTagsWizard } from '../import-tags'; import { ManageOrgsModal } from '../manage-orgs'; import messages from './messages'; +/** @typedef {import('../data/types.mjs').TaxonomyData} TaxonomyData */ +// Note: to make mocking easier for tests, the types below only specify the subset of TaxonomyData that we actually use. + +/** + * A menu that provides actions for editing a specific taxonomy. + * @type {React.FC<{ + * taxonomy: Pick, + * iconMenu?: boolean + * }>} + */ const TaxonomyMenu = ({ taxonomy, iconMenu, }) => { diff --git a/src/taxonomy/taxonomy-menu/TaxonomyMenu.test.jsx b/src/taxonomy/taxonomy-menu/TaxonomyMenu.test.jsx index 666c2543fb..6272c12da5 100644 --- a/src/taxonomy/taxonomy-menu/TaxonomyMenu.test.jsx +++ b/src/taxonomy/taxonomy-menu/TaxonomyMenu.test.jsx @@ -1,4 +1,7 @@ -import { useMemo } from 'react'; +// @ts-check +/* eslint-disable react/prop-types */ +// ^ eslint doesn't 'see' JSDoc types; remove this lint directive when converting this to .tsx +import React, { useMemo } from 'react'; import { IntlProvider } from '@edx/frontend-platform/i18n'; import { initializeMockApp } from '@edx/frontend-platform'; import { AppProvider } from '@edx/frontend-platform/react'; @@ -25,15 +28,25 @@ const queryClient = new QueryClient(); const mockSetToastMessage = jest.fn(); +/** + * @type {React.FC<{ + * iconMenu: boolean, + * systemDefined?: boolean, + * canChangeTaxonomy?: boolean, + * canDeleteTaxonomy?: boolean, + * }>} + */ const TaxonomyMenuComponent = ({ iconMenu, - systemDefined, - canChangeTaxonomy, - canDeleteTaxonomy, + systemDefined = false, + canChangeTaxonomy = true, + canDeleteTaxonomy = true, }) => { const context = useMemo(() => ({ toastMessage: null, setToastMessage: mockSetToastMessage, + alertProps: null, + setAlertProps: null, }), []); return ( @@ -59,18 +72,7 @@ const TaxonomyMenuComponent = ({ ); }; -TaxonomyMenuComponent.propTypes = TaxonomyMenu.propTypes; - -TaxonomyMenuComponent.defaultProps = { - id: taxonomyId, - name: taxonomyName, - tagsCount: 0, - systemDefined: false, - canChangeTaxonomy: true, - canDeleteTaxonomy: true, -}; - -describe.each([true, false])('', async (iconMenu) => { +describe.each([true, false])('', (iconMenu) => { beforeEach(async () => { initializeMockApp({ authenticatedUser: { @@ -87,30 +89,35 @@ describe.each([true, false])('', async (iconMenu) => jest.clearAllMocks(); }); - test('should open and close menu on button click', async () => { - const { findByTestId, getByTestId, queryByTestId } = render( + test('should open and close menu on button click', () => { + const { getByRole, getByTestId, queryByLabelText } = render( , ); + const menuLabelText = 'Actions'; + const menuAltText = `${taxonomyName} actions`; + const menuButtonText = iconMenu ? menuAltText : menuLabelText; // Menu closed/doesn't exist yet - expect(queryByTestId('taxonomy-menu')).not.toBeInTheDocument(); + expect(queryByLabelText(menuLabelText)).not.toBeInTheDocument(); // Click on the menu button to open - fireEvent.click(await findByTestId('taxonomy-menu-button')); + fireEvent.click(getByRole('button', { name: menuButtonText })); // Menu opened expect(getByTestId('taxonomy-menu')).toBeVisible(); // Click on button again to close the menu - fireEvent.click(await findByTestId('taxonomy-menu-button')); + fireEvent.click(getByRole('button', { name: menuButtonText })); // Menu closed // Jest bug: toBeVisible() isn't checking opacity correctly // expect(getByTestId('taxonomy-menu')).not.toBeVisible(); expect(getByTestId('taxonomy-menu').style.opacity).toEqual('0'); + // TODO: the above should be getByLabelText(menuButtonText) but there is a conflict + // when iconMenu={true} because the