2022-05-06 11:09:28 +02: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 11:47:50 +01:00
|
|
|
import React from "react";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { renderIntoDocument, Simulate } from "react-dom/test-utils";
|
2021-12-09 11:47:50 +01:00
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { Alignment } from "../../../../src/components/views/elements/Tooltip";
|
2021-12-09 13:36:55 +01:00
|
|
|
import TooltipTarget from "../../../../src/components/views/elements/TooltipTarget";
|
2021-12-09 11:47:50 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<TooltipTarget />", () => {
|
2021-12-09 11:47:50 +01:00
|
|
|
const defaultProps = {
|
2022-12-12 12:24:14 +01:00
|
|
|
"tooltipTargetClassName": "test tooltipTargetClassName",
|
|
|
|
"className": "test className",
|
|
|
|
"tooltipClassName": "test tooltipClassName",
|
|
|
|
"label": "test label",
|
2021-12-09 11:47:50 +01:00
|
|
|
"alignment": Alignment.Left,
|
2022-12-12 12:24:14 +01:00
|
|
|
"id": "test id",
|
|
|
|
"data-test-id": "test",
|
2021-12-09 11:47:50 +01:00
|
|
|
};
|
|
|
|
|
2022-08-17 15:35:33 +02:00
|
|
|
afterEach(() => {
|
|
|
|
// clean up renderer tooltips
|
2022-12-12 12:24:14 +01:00
|
|
|
const wrapper = document.querySelector(".mx_Tooltip_wrapper");
|
2022-08-17 15:35:33 +02:00
|
|
|
while (wrapper?.firstChild) {
|
|
|
|
wrapper.removeChild(wrapper.lastChild);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-09 11:47:50 +01:00
|
|
|
const getComponent = (props = {}) => {
|
|
|
|
const wrapper = renderIntoDocument<HTMLSpanElement>(
|
2022-12-12 12:24:14 +01:00
|
|
|
// wrap in element so renderIntoDocument can render functional component
|
2021-12-09 11:47:50 +01:00
|
|
|
<span>
|
|
|
|
<TooltipTarget {...defaultProps} {...props}>
|
|
|
|
<span>child</span>
|
|
|
|
</TooltipTarget>
|
|
|
|
</span>,
|
|
|
|
) as HTMLSpanElement;
|
2022-12-12 12:24:14 +01:00
|
|
|
return wrapper.querySelector("[data-test-id=test]");
|
2021-12-09 11:47:50 +01:00
|
|
|
};
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const getVisibleTooltip = () => document.querySelector(".mx_Tooltip.mx_Tooltip_visible");
|
2021-12-09 11:47:50 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders container", () => {
|
2021-12-09 11:47:50 +01:00
|
|
|
const component = getComponent();
|
|
|
|
expect(component).toMatchSnapshot();
|
|
|
|
expect(getVisibleTooltip()).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-08-17 15:35:33 +02:00
|
|
|
const alignmentKeys = Object.keys(Alignment).filter((o: any) => isNaN(o));
|
2023-02-13 12:39:16 +01:00
|
|
|
it.each(alignmentKeys)("displays %s aligned tooltip on mouseover", async (alignment: any) => {
|
2022-08-17 15:35:33 +02:00
|
|
|
const wrapper = getComponent({ alignment: Alignment[alignment] });
|
|
|
|
act(() => {
|
|
|
|
Simulate.mouseOver(wrapper);
|
|
|
|
});
|
|
|
|
expect(getVisibleTooltip()).toMatchSnapshot();
|
|
|
|
});
|
2021-12-09 11:47:50 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("hides tooltip on mouseleave", () => {
|
2021-12-09 11:47:50 +01:00
|
|
|
const wrapper = getComponent();
|
|
|
|
act(() => {
|
|
|
|
Simulate.mouseOver(wrapper);
|
|
|
|
});
|
|
|
|
expect(getVisibleTooltip()).toBeTruthy();
|
|
|
|
act(() => {
|
|
|
|
Simulate.mouseLeave(wrapper);
|
|
|
|
});
|
|
|
|
expect(getVisibleTooltip()).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("displays tooltip on focus", () => {
|
2021-12-09 11:47:50 +01:00
|
|
|
const wrapper = getComponent();
|
|
|
|
act(() => {
|
|
|
|
Simulate.focus(wrapper);
|
|
|
|
});
|
|
|
|
expect(getVisibleTooltip()).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("hides tooltip on blur", async () => {
|
2021-12-09 11:47:50 +01:00
|
|
|
const wrapper = getComponent();
|
|
|
|
act(() => {
|
|
|
|
Simulate.focus(wrapper);
|
|
|
|
});
|
|
|
|
expect(getVisibleTooltip()).toBeTruthy();
|
2022-08-17 15:35:33 +02:00
|
|
|
act(() => {
|
|
|
|
Simulate.blur(wrapper);
|
2021-12-09 11:47:50 +01:00
|
|
|
});
|
|
|
|
expect(getVisibleTooltip()).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|