Skip to content

Commit 362a590

Browse files
authored
Merge pull request #131 from microsoft/user/aubreyquinn/testCoverage
increased unit test coverage for the has tool tip parent function
2 parents 2fd262d + 5f42145 commit 362a590

File tree

3 files changed

+94
-31
lines changed

3 files changed

+94
-31
lines changed

lib/util/hasTooltipParent.js

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

lib/util/hasTooltipParent.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { elementType } from "jsx-ast-utils";
5+
import { TSESLint } from "@typescript-eslint/utils"; // Assuming context comes from TSESLint
6+
import { JSXOpeningElement } from "estree-jsx";
7+
8+
const hasToolTipParent = (context: TSESLint.RuleContext<string, unknown[]>): boolean => {
9+
const ancestors = context.getAncestors();
10+
11+
if (!ancestors || ancestors.length === 0) {
12+
return false;
13+
}
14+
15+
return ancestors.some(
16+
item =>
17+
item.type === "JSXElement" &&
18+
item.openingElement &&
19+
item.openingElement.type === "JSXOpeningElement" &&
20+
elementType(item.openingElement as unknown as JSXOpeningElement) === "Tooltip"
21+
);
22+
};
23+
24+
export { hasToolTipParent };
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
});

0 commit comments

Comments
 (0)