Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions lib/util/hasTooltipParent.js

This file was deleted.

24 changes: 24 additions & 0 deletions lib/util/hasTooltipParent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { elementType } from "jsx-ast-utils";
import { TSESLint } from "@typescript-eslint/utils"; // Assuming context comes from TSESLint
import { JSXOpeningElement } from "estree-jsx";

const hasToolTipParent = (context: TSESLint.RuleContext<string, unknown[]>): boolean => {
const ancestors = context.getAncestors();

if (!ancestors || ancestors.length === 0) {
return false;
}

return ancestors.some(
item =>
item.type === "JSXElement" &&
item.openingElement &&
item.openingElement.type === "JSXOpeningElement" &&
elementType(item.openingElement as unknown as JSXOpeningElement) === "Tooltip"
);
};

export { hasToolTipParent };
70 changes: 70 additions & 0 deletions tests/lib/rules/utils/hasTooltipParent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { hasToolTipParent } from "../../../../lib/util/hasTooltipParent";
import { TSESLint } from "@typescript-eslint/utils";

// Mocking the elementType utility
jest.mock("jsx-ast-utils", () => ({
elementType: (openingElement: any) => openingElement.name.name
}));

describe("hasToolTipParent", () => {
let mockContext: TSESLint.RuleContext<string, unknown[]>;

beforeEach(() => {
mockContext = {
getAncestors: jest.fn()
} as unknown as TSESLint.RuleContext<string, unknown[]>;
});

test("should return false when there are no ancestors", () => {
(mockContext.getAncestors as jest.Mock).mockReturnValue([]);

const result = hasToolTipParent(mockContext);
expect(result).toBe(false);
});

test("should return false when no Tooltip ancestor exists", () => {
const mockAncestors = [
{
type: "JSXElement",
openingElement: {
type: "JSXOpeningElement",
name: { name: "Button" } // Not a Tooltip
}
},
{
type: "JSXElement",
openingElement: {
type: "JSXOpeningElement",
name: { name: "Div" } // Not a Tooltip
}
}
];
(mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors);

const result = hasToolTipParent(mockContext);
expect(result).toBe(false);
});

test("should return true when a Tooltip ancestor exists", () => {
const mockAncestors = [
{
type: "JSXElement",
openingElement: {
type: "JSXOpeningElement",
name: { name: "Div" } // Not a Tooltip
}
},
{
type: "JSXElement",
openingElement: {
type: "JSXOpeningElement",
name: { name: "Tooltip" } // This is a Tooltip
}
}
];
(mockContext.getAncestors as jest.Mock).mockReturnValue(mockAncestors);

const result = hasToolTipParent(mockContext);
expect(result).toBe(true);
});
});
Loading