Skip to content

Commit 88e9fc8

Browse files
committed
improved unit test coverage for has tool tip parent function
1 parent 532682d commit 88e9fc8

File tree

3 files changed

+128
-31
lines changed

3 files changed

+128
-31
lines changed

lib/util/hasTooltipParent.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/util/hasTooltipParent.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { elementType } from "jsx-ast-utils";
5+
import { TSESLint, TSESTree } from "@typescript-eslint/utils";
6+
import { JSXOpeningElement } from "estree-jsx";
7+
8+
// Type guard to check if a node is a JSXElement
9+
const isJSXElement = (node: TSESTree.Node): node is TSESTree.JSXElement => {
10+
return node.type === "JSXElement";
11+
};
12+
13+
const hasToolTipParent = (context: TSESLint.RuleContext<string, unknown[]>): boolean => {
14+
const ancestors = context.getAncestors();
15+
16+
if (!ancestors || ancestors.length === 0) {
17+
return false;
18+
}
19+
20+
// Iterate through ancestors and return false if a non-JSXElement is found before a Tooltip
21+
for (const item of ancestors) {
22+
if (!isJSXElement(item)) {
23+
return false; // Stop if a non-JSXElement is encountered
24+
}
25+
26+
const { openingElement } = item;
27+
if (
28+
openingElement &&
29+
openingElement.type === "JSXOpeningElement" &&
30+
elementType(openingElement as unknown as JSXOpeningElement) === "Tooltip"
31+
) {
32+
return true; // Return true if we find a Tooltip
33+
}
34+
}
35+
36+
return false;
37+
};
38+
39+
export { hasToolTipParent };
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { hasToolTipParent } from "../../../../lib/util/hasTooltipParent";
2+
import { TSESLint } from "@typescript-eslint/utils";
3+
4+
// Mocking the elementType utility
5+
jest.mock("jsx-ast-utils", () => ({
6+
elementType: (openingElement: any) => openingElement.name.name
7+
}));
8+
9+
describe("hasToolTipParent", () => {
10+
let mockContext: TSESLint.RuleContext<string, unknown[]>;
11+
12+
beforeEach(() => {
13+
mockContext = {
14+
getAncestors: jest.fn()
15+
} as unknown as TSESLint.RuleContext<string, unknown[]>;
16+
});
17+
18+
test("should return false when there are no ancestors", () => {
19+
(mockContext.getAncestors as jest.Mock).mockReturnValue([]);
20+
21+
const result = hasToolTipParent(mockContext);
22+
expect(result).toBe(false);
23+
});
24+
25+
test("should return false when no Tooltip ancestor exists", () => {
26+
const mockAncestors = [
27+
{
28+
type: "JSXElement",
29+
openingElement: {
30+
type: "JSXOpeningElement",
31+
name: { name: "Button" } // Not a Tooltip
32+
}
33+
},
34+
{
35+
type: "JSXElement",
36+
openingElement: {
37+
type: "JSXOpeningElement",
38+
name: { name: "Div" } // Not a Tooltip
39+
}
40+
}
41+
];
42+
(mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors);
43+
44+
const result = hasToolTipParent(mockContext);
45+
expect(result).toBe(false);
46+
});
47+
48+
test("should return true when a Tooltip ancestor exists", () => {
49+
const mockAncestors = [
50+
{
51+
type: "JSXElement",
52+
openingElement: {
53+
type: "JSXOpeningElement",
54+
name: { name: "Div" } // Not a Tooltip
55+
}
56+
},
57+
{
58+
type: "JSXElement",
59+
openingElement: {
60+
type: "JSXOpeningElement",
61+
name: { name: "Tooltip" } // This is a Tooltip
62+
}
63+
}
64+
];
65+
(mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors);
66+
67+
const result = hasToolTipParent(mockContext);
68+
expect(result).toBe(true);
69+
});
70+
71+
test("should return false when the ancestor is not a JSXElement", () => {
72+
const mockAncestors = [
73+
{
74+
type: "Literal" // Not a JSXElement
75+
},
76+
{
77+
type: "JSXElement",
78+
openingElement: {
79+
type: "JSXOpeningElement",
80+
name: { name: "Tooltip" } // Tooltip exists but first ancestor is invalid
81+
}
82+
}
83+
];
84+
(mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors);
85+
86+
const result = hasToolTipParent(mockContext);
87+
expect(result).toBe(false);
88+
});
89+
});

0 commit comments

Comments
 (0)