Skip to content

Commit d808e6b

Browse files
committed
test: write test for tooltipContent component and run prettier
1 parent 8d26428 commit d808e6b

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { TooltipContent } from "./tooltipContent";
2+
3+
describe("TooltipContent", () => {
4+
it("renders plain text when tooltipRenderAsHtml is false", () => {
5+
const el = TooltipContent({
6+
text: "Hello, tooltip!",
7+
tooltipRenderAsHtml: false,
8+
});
9+
10+
expect(el.textContent).toBe("Hello, tooltip!");
11+
expect(el.innerHTML).toBe("Hello, tooltip!");
12+
expect(el.className).toBe("introjs-tooltiptext");
13+
});
14+
15+
it("renders HTML content when tooltipRenderAsHtml is true", () => {
16+
const el = TooltipContent({
17+
text: "<strong>Bold Text</strong>",
18+
tooltipRenderAsHtml: true,
19+
});
20+
21+
expect(el.innerHTML).toBe("<strong>Bold Text</strong>");
22+
expect(el.querySelector("strong")?.textContent).toBe("Bold Text");
23+
});
24+
25+
it("applies a custom class when provided", () => {
26+
const el = TooltipContent({
27+
text: "Custom class test",
28+
className: "my-custom-tooltip",
29+
});
30+
31+
expect(el.className).toBe("my-custom-tooltip");
32+
expect(el.textContent).toBe("Custom class test");
33+
});
34+
35+
it("clears old content on re-derivation", () => {
36+
const el = TooltipContent({
37+
text: "<i>Initial</i>",
38+
tooltipRenderAsHtml: true,
39+
});
40+
41+
el.innerHTML = "<b>Old Content</b>"; // Simulate old content
42+
const updated = TooltipContent({
43+
text: "Updated text",
44+
tooltipRenderAsHtml: false,
45+
});
46+
47+
expect(updated.innerHTML).toBe("Updated text");
48+
expect(updated.querySelector("b")).toBeNull();
49+
});
50+
});

src/packages/tooltip/tooltipContent.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import dom from "../dom";
33
const { div } = dom.tags;
44

55
export type tooltipContentProps = {
6-
text: string;
7-
tooltipRenderAsHtml?: boolean;
8-
className?: string;
9-
};
6+
text: string;
7+
tooltipRenderAsHtml?: boolean;
8+
className?: string;
9+
};
1010

11-
export const TooltipContent = ({ text, tooltipRenderAsHtml, className }: tooltipContentProps) => {
11+
export const TooltipContent = ({
12+
text,
13+
tooltipRenderAsHtml,
14+
className,
15+
}: tooltipContentProps) => {
1216
const container = div({
1317
className: className || "introjs-tooltiptext",
1418
});
@@ -18,7 +22,7 @@ export const TooltipContent = ({ text, tooltipRenderAsHtml, className }: tooltip
1822
if (!el) return;
1923

2024
// Clear existing content
21-
el.innerHTML = '';
25+
el.innerHTML = "";
2226

2327
if (tooltipRenderAsHtml && text) {
2428
const fragment = document.createRange().createContextualFragment(text);

src/packages/tour/components/TourTooltip.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ export const TourTooltip = ({
453453
children.push(Header({ title, skipLabel, onSkipClick }));
454454

455455
children.push(
456-
TooltipContent({
457-
text,
458-
tooltipRenderAsHtml: props.renderAsHtml,
459-
className: tooltipTextClassName,
460-
})
456+
TooltipContent({
457+
text,
458+
tooltipRenderAsHtml: props.renderAsHtml,
459+
className: tooltipTextClassName,
460+
})
461461
);
462462

463463
if (dontShowAgain) {

0 commit comments

Comments
 (0)