Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code covergae for OverFlowMenuV2 #17884

Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions packages/react/src/components/OverflowMenuV2/OverFlowMenuV2-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* 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, fireEvent, render, screen } from '@testing-library/react';
ariellalgilmore marked this conversation as resolved.
Show resolved Hide resolved

import { FeatureFlags } from '../FeatureFlags';
import { MenuItem } from '../Menu';
import { OverflowMenuV2 } from './';
import React from 'react';
import { action } from '@storybook/addon-actions';

// Mocking console.warn to track deprecation warning
jest.spyOn(console, 'warn').mockImplementation(() => {});
ariellalgilmore marked this conversation as resolved.
Show resolved Hide resolved

describe('<OverflowMenuV2 />', () => {
let consoleWarnSpy;

beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.clearAllMocks();
});

afterEach(() => {
consoleWarnSpy.mockRestore();
});

it('logs the deprecation warning when rendering OverflowMenuV2', () => {
const onClick = action('onClick (MenuItem)');

render(
<OverflowMenuV2>
<MenuItem label="Stop app" onClick={onClick} />
</OverflowMenuV2>
);

// Check if the deprecation warning is logged
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining(
'`<OverflowMenuV2>` is deprecated and will be removed in the next major version.'
)
);
});

it('renders with FeatureFlags and passes the correct flag', async () => {
ariellalgilmore marked this conversation as resolved.
Show resolved Hide resolved
const { getByRole, findByText } = render(
<FeatureFlags enableV12Overflowmenu>
<OverflowMenuV2>
<MenuItem label="Stop app" />
<MenuItem label="Delete app" kind="danger" />
</OverflowMenuV2>
</FeatureFlags>
);

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('passes new props to OverflowMenu and applies autoAlign', () => {
ariellalgilmore marked this conversation as resolved.
Show resolved Hide resolved
const { container } = render(
<OverflowMenuV2 autoAlign>
<MenuItem label="Stop app" />
</OverflowMenuV2>
);

expect(container.firstChild).toHaveClass('cds--autoalign');
});

it('renders OverflowMenu with MenuItem children', async () => {
render(
<FeatureFlags enableV12Overflowmenu>
<OverflowMenuV2>
<MenuItem label="Stop app" />
<MenuItem label="Delete app" kind="danger" />
</OverflowMenuV2>
</FeatureFlags>
);

const button = screen.getByRole('button', { name: /options/i });

await act(async () => {
fireEvent.click(button);
ariellalgilmore marked this conversation as resolved.
Show resolved Hide resolved
});

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();
});
});
Loading