MM-61560 Make it so that Menu.Items can be tested without a Menu (#29427)

* MM-61650 Make it so that Menu.Items can be tested without a Menu

* Rename file to stop Jest from complaining
Этот коммит содержится в:
Harrison Healey
2024-11-29 12:57:21 -05:00
коммит произвёл GitHub
родитель 961fc6bfe9
Коммит ef46f8e164
2 изменённых файлов: 39 добавлений и 16 удалений

Просмотреть файл

@@ -5,23 +5,12 @@ import {DateTime} from 'luxon';
import React from 'react';
import useTimePostBoxIndicator from 'components/advanced_text_editor/use_post_box_indicator';
import {WithTestMenuContext} from 'components/menu/menu_context_test';
import {renderWithContext, fireEvent, screen} from 'tests/react_testing_utils';
import CoreMenuOptions from './core_menu_options';
jest.mock('components/menu', () => ({
__esModule: true,
Item: jest.fn(({labels, trailingElements, children, ...props}) => (
<div {...props}>
{labels}
{children}
{trailingElements}
</div>
)),
Separator: jest.fn(() => <div className='menu-separator'/>),
}));
jest.mock('components/advanced_text_editor/use_post_box_indicator');
const mockedUseTimePostBoxIndicator = jest.mocked(useTimePostBoxIndicator);
@@ -75,10 +64,12 @@ describe('CoreMenuOptions Component', () => {
function renderComponent(state = initialState, handleOnSelectOverride = handleOnSelect) {
renderWithContext(
<CoreMenuOptions
handleOnSelect={handleOnSelectOverride}
channelId='channelId'
/>,
<WithTestMenuContext>
<CoreMenuOptions
handleOnSelect={handleOnSelectOverride}
channelId='channelId'
/>
</WithTestMenuContext>,
state,
);
}

Просмотреть файл

@@ -0,0 +1,32 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useEffect, useState} from 'react';
import {MenuContext, useMenuContextValue} from './menu_context';
type Props = {
children: React.ReactNode;
}
export function WithTestMenuContext({
children,
}: Props) {
const [show, setShow] = useState(true);
const menuContextValue = useMenuContextValue(() => setShow(false), show);
useEffect(() => {
if (!show) {
menuContextValue.handleClosed();
}
// We only want to call this when the menu is closed
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [show]);
return (
<MenuContext.Provider value={menuContextValue}>
{children}
</MenuContext.Provider>
);
}