From 8502d7a0eccc5f0b248698c17eb98d57f0878302 Mon Sep 17 00:00:00 2001 From: Soumya Raju Date: Tue, 10 Dec 2024 02:39:52 +0530 Subject: [PATCH] Increase code covergae for OverFlowMenuV2 (#17884) * test: overflow menu v2- adding tests * test: overflow menu v2- adding tests * test: addressing review comments * test: addressing review comments * test: addressing review comments * test: replaced fireevent with userEvent * test: change test names for consistency --------- Co-authored-by: Nikhil Tomar <63502271+2nikhiltom@users.noreply.github.com> Co-authored-by: Ariella Gilmore --- .../OverflowMenuV2/OverFlowMenuV2-test.js | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 packages/react/src/components/OverflowMenuV2/OverFlowMenuV2-test.js diff --git a/packages/react/src/components/OverflowMenuV2/OverFlowMenuV2-test.js b/packages/react/src/components/OverflowMenuV2/OverFlowMenuV2-test.js new file mode 100644 index 000000000000..500fdfe350f7 --- /dev/null +++ b/packages/react/src/components/OverflowMenuV2/OverFlowMenuV2-test.js @@ -0,0 +1,101 @@ +/** + * Copyright IBM Corp. 2016, 2024 + * + * This source code is licensed under the Apache-2.0 license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { act, render, screen } from '@testing-library/react'; + +import { FeatureFlags } from '../FeatureFlags'; +import { MenuItem } from '../Menu'; +import { OverflowMenuV2 } from './'; +import React from 'react'; +import { action } from '@storybook/addon-actions'; +import userEvent from '@testing-library/user-event'; + +describe('', () => { + let consoleWarnSpy; + + beforeEach(() => { + consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + jest.clearAllMocks(); + }); + + afterEach(() => { + consoleWarnSpy.mockRestore(); + }); + + it('should log the deprecation warning when rendering OverflowMenuV2', () => { + const onClick = action('onClick (MenuItem)'); + + render( + + + + ); + + // Check if the deprecation warning is logged + expect(consoleWarnSpy).toHaveBeenCalledWith( + expect.stringContaining( + '`` is deprecated and will be removed in the next major version.' + ) + ); + }); + + it('should render correctly with feature flag enabled', async () => { + const { getByRole, findByText } = render( + + + + + + + ); + + await act(async () => { + const button = getByRole('button', { name: /options/i }); + button.click(); + }); + + expect(await findByText('Stop app')).toBeInTheDocument(); + expect(await findByText('Delete app')).toBeInTheDocument(); + }); + + it('should respect align prop', () => { + const { container } = render( + + + + ); + + expect(container.firstChild).toHaveClass('cds--autoalign'); + }); + + it('should render OverflowMenu with MenuItem children', async () => { + render( + + + + + + + ); + + const button = screen.getByRole('button', { name: /options/i }); + + await act(async () => { + await userEvent.click(button); + }); + + const stopAppMenuItem = await screen.findByRole('menuitem', { + name: /Stop app/i, + }); + const deleteAppMenuItem = await screen.findByRole('menuitem', { + name: /Delete app/i, + }); + + expect(stopAppMenuItem).toBeInTheDocument(); + expect(deleteAppMenuItem).toBeInTheDocument(); + }); +});