2022-05-06 12:09:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-12-09 13:47:50 +03:00
|
|
|
import React from "react";
|
2023-03-01 18:59:27 +03:00
|
|
|
import { fireEvent, render } from "@testing-library/react";
|
2021-12-09 13:47:50 +03:00
|
|
|
|
|
|
|
import { Alignment } from "../../../../src/components/views/elements/Tooltip";
|
2021-12-09 15:36:55 +03:00
|
|
|
import TooltipTarget from "../../../../src/components/views/elements/TooltipTarget";
|
2021-12-09 13:47:50 +03:00
|
|
|
|
|
|
|
describe("<TooltipTarget />", () => {
|
|
|
|
const defaultProps = {
|
|
|
|
"tooltipTargetClassName": "test tooltipTargetClassName",
|
|
|
|
"className": "test className",
|
|
|
|
"tooltipClassName": "test tooltipClassName",
|
|
|
|
"label": "test label",
|
|
|
|
"alignment": Alignment.Left,
|
|
|
|
"id": "test id",
|
2023-03-01 18:59:27 +03:00
|
|
|
"data-testid": "test",
|
2021-12-09 13:47:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const getComponent = (props = {}) => {
|
2023-03-01 18:59:27 +03:00
|
|
|
const wrapper = render(
|
2021-12-09 13:47:50 +03:00
|
|
|
// wrap in element so renderIntoDocument can render functional component
|
|
|
|
<span>
|
|
|
|
<TooltipTarget {...defaultProps} {...props}>
|
|
|
|
<span>child</span>
|
|
|
|
</TooltipTarget>
|
|
|
|
</span>,
|
2023-03-01 18:59:27 +03:00
|
|
|
);
|
|
|
|
return wrapper.getByTestId("test");
|
2021-12-09 13:47:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const getVisibleTooltip = () => document.querySelector(".mx_Tooltip.mx_Tooltip_visible");
|
|
|
|
|
|
|
|
it("renders container", () => {
|
|
|
|
const component = getComponent();
|
|
|
|
expect(component).toMatchSnapshot();
|
|
|
|
expect(getVisibleTooltip()).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-08-17 16:35:33 +03:00
|
|
|
const alignmentKeys = Object.keys(Alignment).filter((o: any) => isNaN(o));
|
2023-02-13 14:39:16 +03:00
|
|
|
it.each(alignmentKeys)("displays %s aligned tooltip on mouseover", async (alignment: any) => {
|
2023-02-15 16:36:22 +03:00
|
|
|
const wrapper = getComponent({ alignment: Alignment[alignment] })!;
|
2023-03-01 18:59:27 +03:00
|
|
|
fireEvent.mouseOver(wrapper);
|
2022-08-17 16:35:33 +03:00
|
|
|
expect(getVisibleTooltip()).toMatchSnapshot();
|
|
|
|
});
|
2021-12-09 13:47:50 +03:00
|
|
|
|
|
|
|
it("hides tooltip on mouseleave", () => {
|
2023-02-15 16:36:22 +03:00
|
|
|
const wrapper = getComponent()!;
|
2023-03-01 18:59:27 +03:00
|
|
|
fireEvent.mouseOver(wrapper);
|
2021-12-09 13:47:50 +03:00
|
|
|
expect(getVisibleTooltip()).toBeTruthy();
|
2023-03-01 18:59:27 +03:00
|
|
|
fireEvent.mouseLeave(wrapper);
|
2021-12-09 13:47:50 +03:00
|
|
|
expect(getVisibleTooltip()).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("displays tooltip on focus", () => {
|
2023-02-15 16:36:22 +03:00
|
|
|
const wrapper = getComponent()!;
|
2023-03-01 18:59:27 +03:00
|
|
|
fireEvent.focus(wrapper);
|
2021-12-09 13:47:50 +03:00
|
|
|
expect(getVisibleTooltip()).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("hides tooltip on blur", async () => {
|
2023-02-15 16:36:22 +03:00
|
|
|
const wrapper = getComponent()!;
|
2023-03-01 18:59:27 +03:00
|
|
|
fireEvent.focus(wrapper);
|
2021-12-09 13:47:50 +03:00
|
|
|
expect(getVisibleTooltip()).toBeTruthy();
|
2023-03-01 18:59:27 +03:00
|
|
|
fireEvent.blur(wrapper);
|
2021-12-09 13:47:50 +03:00
|
|
|
expect(getVisibleTooltip()).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|