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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
961fc6bfe9
Коммит
ef46f8e164
@@ -5,23 +5,12 @@ import {DateTime} from 'luxon';
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import useTimePostBoxIndicator from 'components/advanced_text_editor/use_post_box_indicator';
|
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 {renderWithContext, fireEvent, screen} from 'tests/react_testing_utils';
|
||||||
|
|
||||||
import CoreMenuOptions from './core_menu_options';
|
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');
|
jest.mock('components/advanced_text_editor/use_post_box_indicator');
|
||||||
const mockedUseTimePostBoxIndicator = jest.mocked(useTimePostBoxIndicator);
|
const mockedUseTimePostBoxIndicator = jest.mocked(useTimePostBoxIndicator);
|
||||||
|
|
||||||
@@ -75,10 +64,12 @@ describe('CoreMenuOptions Component', () => {
|
|||||||
|
|
||||||
function renderComponent(state = initialState, handleOnSelectOverride = handleOnSelect) {
|
function renderComponent(state = initialState, handleOnSelectOverride = handleOnSelect) {
|
||||||
renderWithContext(
|
renderWithContext(
|
||||||
<CoreMenuOptions
|
<WithTestMenuContext>
|
||||||
handleOnSelect={handleOnSelectOverride}
|
<CoreMenuOptions
|
||||||
channelId='channelId'
|
handleOnSelect={handleOnSelectOverride}
|
||||||
/>,
|
channelId='channelId'
|
||||||
|
/>
|
||||||
|
</WithTestMenuContext>,
|
||||||
state,
|
state,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
32
webapp/channels/src/components/menu/menu_context_test.tsx
Обычный файл
32
webapp/channels/src/components/menu/menu_context_test.tsx
Обычный файл
@@ -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>
|
||||||
|
);
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user