Skip to content

Commit df4525a

Browse files
author
Iryna Vasylenko
committed
Add unit test for hasValidNestedProp
1 parent eccfd93 commit df4525a

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
jest.mock("jsx-ast-utils", () => ({
5+
getProp: jest.fn(),
6+
getPropValue: jest.fn()
7+
}));
8+
9+
import { getProp, getPropValue } from "jsx-ast-utils";
10+
import { hasValidNestedProp } from "../../../../lib/util/hasValidNestedProp";
11+
12+
describe("hasValidNestedProp", () => {
13+
const opening = { attributes: [] } as any;
14+
15+
beforeEach(() => {
16+
(getProp as jest.Mock).mockReset();
17+
(getPropValue as jest.Mock).mockReset();
18+
});
19+
20+
test("returns false when the prop is not present (e.g. <Tag />)", () => {
21+
// Example: <Tag />
22+
(getProp as jest.Mock).mockReturnValue(undefined);
23+
const result = hasValidNestedProp(opening, "dismissIcon", "aria-label");
24+
expect(result).toBe(false);
25+
expect(getProp).toHaveBeenCalledWith(opening.attributes, "dismissIcon");
26+
});
27+
28+
test("returns false when nested key is missing or empty string", () => {
29+
// Example: <Tag dismissIcon={{}} />
30+
(getProp as jest.Mock).mockReturnValue({});
31+
(getPropValue as jest.Mock).mockReturnValue({});
32+
expect(hasValidNestedProp(opening, "dismissIcon", "aria-label")).toBe(false);
33+
34+
// Example: <Tag dismissIcon={{ "aria-label": " " }} />
35+
(getPropValue as jest.Mock).mockReturnValue({ "aria-label": " " });
36+
expect(hasValidNestedProp(opening, "dismissIcon", "aria-label")).toBe(false);
37+
});
38+
39+
test("returns true when nested key is a non-empty string (e.g. <Tag dismissIcon={{ 'aria-label': 'Dismiss' }} />)", () => {
40+
// Example: <Tag dismissIcon={{ "aria-label": "Dismiss" }} />
41+
(getProp as jest.Mock).mockReturnValue({});
42+
(getPropValue as jest.Mock).mockReturnValue({ "aria-label": "Dismiss" });
43+
expect(hasValidNestedProp(opening, "dismissIcon", "aria-label")).toBe(true);
44+
});
45+
});

tests/lib/rules/utils/ruleFactory.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe("hasAccessibleLabel (unit)", () => {
107107
getSourceCode: jest.fn()
108108
} as unknown as TSESLint.RuleContext<string, []>;
109109

110-
const cfg: Required<LabeledControlConfig> = {
110+
const cfg: LabeledControlConfig = {
111111
component: "RadioGroup",
112112
requiredProps: ["alt"],
113113
labelProps: ["label", "aria-label"],
@@ -120,9 +120,7 @@ describe("hasAccessibleLabel (unit)", () => {
120120
messageId: "errorMsg",
121121
description: "anything",
122122
allowLabeledChild: true,
123-
allowTextContentChild: true,
124-
triggerProp: "",
125-
customValidator: jest.fn().mockReturnValue(false)
123+
allowTextContentChild: true
126124
};
127125

128126
test("returns false when no heuristics pass", () => {
@@ -294,7 +292,7 @@ describe("hasAccessibleLabel (unit)", () => {
294292
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } });
295293

296294
describe("makeLabeledControlRule (RuleTester integration)", () => {
297-
const baseCfg: Required<LabeledControlConfig> = {
295+
const baseCfg: LabeledControlConfig = {
298296
component: "RadioGroup",
299297
requiredProps: ["alt"],
300298
labelProps: ["label", "aria-label"],
@@ -307,9 +305,7 @@ describe("makeLabeledControlRule (RuleTester integration)", () => {
307305
messageId: "noUnlabeledRadioGroup",
308306
description: "Accessibility: RadioGroup must have a programmatic and visual label.",
309307
allowLabeledChild: true,
310-
allowTextContentChild: true,
311-
triggerProp: "",
312-
customValidator: jest.mock
308+
allowTextContentChild: true
313309
};
314310

315311
// 1) No heuristics -> report

0 commit comments

Comments
 (0)