Prevent useContextMenu isOpen from being true if the button ref goes away (#9418)

This commit is contained in:
Michael Telatynski 2022-10-17 17:42:04 +01:00 committed by GitHub
parent e1d631cb47
commit 13db1b17be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 35 deletions

View file

@ -15,16 +15,13 @@ limitations under the License.
*/
import React from 'react';
// eslint-disable-next-line deprecate/import
import { mount } from 'enzyme';
import { render, screen, fireEvent } from "@testing-library/react";
import { mocked } from 'jest-mock';
import { MatrixClient } from 'matrix-js-sdk/src/matrix';
import { act } from "react-dom/test-utils";
import SpacePanel from '../../../../src/components/views/spaces/SpacePanel';
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
import { SpaceKey } from '../../../../src/stores/spaces';
import { findByTestId } from '../../../test-utils';
import { shouldShowComponent } from '../../../../src/customisations/helpers/UIComponents';
import { UIComponent } from '../../../../src/settings/UIFeature';
@ -47,10 +44,6 @@ jest.mock('../../../../src/customisations/helpers/UIComponents', () => ({
}));
describe('<SpacePanel />', () => {
const defaultProps = {};
const getComponent = (props = {}) =>
mount(<SpacePanel {...defaultProps} {...props} />);
const mockClient = {
getUserId: jest.fn().mockReturnValue('@test:test'),
isGuest: jest.fn(),
@ -67,26 +60,21 @@ describe('<SpacePanel />', () => {
describe('create new space button', () => {
it('renders create space button when UIComponent.CreateSpaces component should be shown', () => {
const component = getComponent();
expect(findByTestId(component, 'create-space-button').length).toBeTruthy();
render(<SpacePanel />);
screen.getByTestId("create-space-button");
});
it('does not render create space button when UIComponent.CreateSpaces component should not be shown', () => {
mocked(shouldShowComponent).mockReturnValue(false);
const component = getComponent();
render(<SpacePanel />);
expect(shouldShowComponent).toHaveBeenCalledWith(UIComponent.CreateSpaces);
expect(findByTestId(component, 'create-space-button').length).toBeFalsy();
expect(screen.queryByTestId("create-space-button")).toBeFalsy();
});
it('opens context menu on create space button click', async () => {
const component = getComponent();
await act(async () => {
findByTestId(component, 'create-space-button').at(0).simulate('click');
component.setProps({});
});
expect(component.find('SpaceCreateMenu').length).toBeTruthy();
it('opens context menu on create space button click', () => {
render(<SpacePanel />);
fireEvent.click(screen.getByTestId("create-space-button"));
screen.getByTestId("create-space-button");
});
});
});