Skip to content

Commit

Permalink
Tooltip add show/hide events (#335)
Browse files Browse the repository at this point in the history
* tooltip tests

* tooltip tests
  • Loading branch information
RonNachmany authored Nov 16, 2021
1 parent b4b3aac commit a3a4863
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/components/Tooltip/Tooltip.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react/jsx-props-no-spreading */
import React from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import isFunction from "lodash/isFunction";
import Dialog from "../Dialog/Dialog";
Expand Down Expand Up @@ -65,16 +66,20 @@ export default class Tooltip extends React.PureComponent {

onTooltipShow() {
if (!this.wasShown) {
const { onTooltipShow } = this.props;
globalState.openTooltipsCount++;
this.wasShown = true;
onTooltipShow && onTooltipShow();
}
}

onTooltipHide() {
if (this.wasShown) {
const { onTooltipHide } = this.props;
globalState.lastTooltipHideTS = Date.now();
globalState.openTooltipsCount--;
this.wasShown = false;
onTooltipHide && onTooltipHide();
}
}

Expand Down Expand Up @@ -148,5 +153,27 @@ Tooltip.defaultProps = {
containerSelector: "#tooltips-container",
immediateShowDelay: null,
tip: true,
hideWhenReferenceHidden: false
hideWhenReferenceHidden: false,
onTooltipHide: null,
onTooltipShow: null
};

Tooltip.propTypes = {
style: PropTypes.object,
arrowPosition: PropTypes.string,
moveBy: PropTypes.shape({ main: PropTypes.number, secondary: PropTypes.number }),
theme: PropTypes.string,
position: PropTypes.string,
justify: PropTypes.string,
hideDelay: PropTypes.number,
showDelay: PropTypes.number,
disableDialogSlide: PropTypes.bool,
animationType: PropTypes.string,
withoutDialog: PropTypes.bool,
containerSelector: PropTypes.string,
immediateShowDelay: PropTypes.number,
tip: PropTypes.bool,
hideWhenReferenceHidden: PropTypes.bool,
onTooltipHide: PropTypes.func,
onTooltipShow: PropTypes.func
};
33 changes: 33 additions & 0 deletions src/components/Tooltip/__tests__/tooltip.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import { render, screen, waitFor, waitForElementToBeRemoved, fireEvent } from "@testing-library/react";
import Tooltip from "../Tooltip";

describe("Tooltip Tests", () => {
describe("Integration Tests", () => {
it("Should trigger onTooltipShow", async () => {
const onTooltipShow = jest.fn()
const { getByText } = render(
<Tooltip content={"content"} onTooltipShow={onTooltipShow}>
<div>hello</div>
</Tooltip>
);
fireEvent.mouseOver(getByText('hello'))
await waitFor(() => screen.getByText('content'))
expect(onTooltipShow).toHaveBeenCalledTimes(1)
})

it("Should trigger onTooltipHide", async () => {
const onTooltipHide = jest.fn()
const { getByText } = render(
<Tooltip content={"content"} onTooltipHide={onTooltipHide}>
<div>hello</div>
</Tooltip>
);
fireEvent.mouseOver(getByText('hello'))
await waitFor(() => screen.getByText('content'))
fireEvent.mouseLeave(getByText('hello'))
await waitForElementToBeRemoved(() => screen.getByText('content'))
expect(onTooltipHide).toHaveBeenCalledTimes(1)
})
});
});

0 comments on commit a3a4863

Please sign in to comment.