|
| 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 | +}); |
0 commit comments