|
| 1 | +// Import necessary dependencies and mock functions |
| 2 | +import { |
| 3 | + hasLabelledChildImage, |
| 4 | + isImageHidden, |
| 5 | + hasAccessibilityAttributes, |
| 6 | + isJSXIdentifierWithName |
| 7 | +} from "../../../../lib/util/hasLabelledChildImage"; |
| 8 | +import { TSESTree, AST_NODE_TYPES } from "@typescript-eslint/types"; |
| 9 | +import { fluentImageComponents, imageDomNodes } from "../../../../lib/applicableComponents/imageBasedComponents"; |
| 10 | +const mergedImageComponents = [...fluentImageComponents, ...imageDomNodes]; |
| 11 | + |
| 12 | +// Helper function to create mock loc and range |
| 13 | +const createMockLocRange = () => ({ |
| 14 | + loc: { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } }, |
| 15 | + range: [0, 0] as [number, number] |
| 16 | +}); |
| 17 | + |
| 18 | +// Unit tests |
| 19 | +describe("isJSXIdentifierWithName", () => { |
| 20 | + it("returns true for a JSXIdentifier with a valid name", () => { |
| 21 | + const name: TSESTree.JSXIdentifier = { type: AST_NODE_TYPES.JSXIdentifier, name: "img", ...createMockLocRange() }; |
| 22 | + expect(isJSXIdentifierWithName(name, mergedImageComponents)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns false for a JSXIdentifier with an invalid name", () => { |
| 26 | + const name: TSESTree.JSXIdentifier = { type: AST_NODE_TYPES.JSXIdentifier, name: "div", ...createMockLocRange() }; |
| 27 | + expect(isJSXIdentifierWithName(name, mergedImageComponents)).toBe(false); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("hasAccessibilityAttributes", () => { |
| 32 | + it("returns true if any accessible attribute is non-empty", () => { |
| 33 | + const attributes: TSESTree.JSXOpeningElement["attributes"] = [ |
| 34 | + { |
| 35 | + type: AST_NODE_TYPES.JSXAttribute, |
| 36 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "alt", ...createMockLocRange() }, |
| 37 | + value: { |
| 38 | + type: AST_NODE_TYPES.Literal, |
| 39 | + value: "An image description", |
| 40 | + raw: '"An image description"', |
| 41 | + ...createMockLocRange() |
| 42 | + }, |
| 43 | + ...createMockLocRange() |
| 44 | + } |
| 45 | + ]; |
| 46 | + expect(hasAccessibilityAttributes(attributes)).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns false if no accessible attribute is present", () => { |
| 50 | + const attributes: TSESTree.JSXOpeningElement["attributes"] = [ |
| 51 | + { |
| 52 | + type: AST_NODE_TYPES.JSXAttribute, |
| 53 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "aria-hidden", ...createMockLocRange() }, |
| 54 | + value: { type: AST_NODE_TYPES.Literal, value: "true", raw: '"true"', ...createMockLocRange() }, |
| 55 | + ...createMockLocRange() |
| 56 | + } |
| 57 | + ]; |
| 58 | + expect(hasAccessibilityAttributes(attributes)).toBe(false); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("isImageHidden", () => { |
| 63 | + it("returns true if `aria-hidden` is set", () => { |
| 64 | + const attributes: TSESTree.JSXOpeningElement["attributes"] = [ |
| 65 | + { |
| 66 | + type: AST_NODE_TYPES.JSXAttribute, |
| 67 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "aria-hidden", ...createMockLocRange() }, |
| 68 | + value: { type: AST_NODE_TYPES.Literal, value: "true", raw: '"true"', ...createMockLocRange() }, |
| 69 | + ...createMockLocRange() |
| 70 | + } |
| 71 | + ]; |
| 72 | + expect(isImageHidden(attributes)).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it("returns true if `alt` attribute is empty", () => { |
| 76 | + const attributes: TSESTree.JSXOpeningElement["attributes"] = [ |
| 77 | + { |
| 78 | + type: AST_NODE_TYPES.JSXAttribute, |
| 79 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "alt", ...createMockLocRange() }, |
| 80 | + value: { type: AST_NODE_TYPES.Literal, value: "", raw: '""', ...createMockLocRange() }, |
| 81 | + ...createMockLocRange() |
| 82 | + } |
| 83 | + ]; |
| 84 | + expect(isImageHidden(attributes)).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it("returns false if `alt` attribute is non-empty", () => { |
| 88 | + const attributes: TSESTree.JSXOpeningElement["attributes"] = [ |
| 89 | + { |
| 90 | + type: AST_NODE_TYPES.JSXAttribute, |
| 91 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "alt", ...createMockLocRange() }, |
| 92 | + value: { type: AST_NODE_TYPES.Literal, value: "Image description", raw: '"Image description"', ...createMockLocRange() }, |
| 93 | + ...createMockLocRange() |
| 94 | + } |
| 95 | + ]; |
| 96 | + expect(isImageHidden(attributes)).toBe(false); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("hasLabelledChildImage", () => { |
| 101 | + it("returns true if a child image component with accessibility attributes is found", () => { |
| 102 | + const mockChild: TSESTree.JSXElement = { |
| 103 | + type: AST_NODE_TYPES.JSXElement, |
| 104 | + openingElement: { |
| 105 | + type: AST_NODE_TYPES.JSXOpeningElement, |
| 106 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "img", ...createMockLocRange() }, |
| 107 | + attributes: [ |
| 108 | + { |
| 109 | + type: AST_NODE_TYPES.JSXAttribute, |
| 110 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "alt", ...createMockLocRange() }, |
| 111 | + value: { type: AST_NODE_TYPES.Literal, value: "description", raw: '"description"', ...createMockLocRange() }, |
| 112 | + ...createMockLocRange() |
| 113 | + } |
| 114 | + ], |
| 115 | + selfClosing: false, |
| 116 | + ...createMockLocRange() |
| 117 | + }, |
| 118 | + closingElement: null, |
| 119 | + children: [], |
| 120 | + ...createMockLocRange() |
| 121 | + }; |
| 122 | + |
| 123 | + const node: TSESTree.JSXElement = { |
| 124 | + type: AST_NODE_TYPES.JSXElement, |
| 125 | + openingElement: { |
| 126 | + type: AST_NODE_TYPES.JSXOpeningElement, |
| 127 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "Container", ...createMockLocRange() }, |
| 128 | + attributes: [], |
| 129 | + selfClosing: false, |
| 130 | + ...createMockLocRange() |
| 131 | + }, |
| 132 | + closingElement: null, |
| 133 | + children: [mockChild], |
| 134 | + ...createMockLocRange() |
| 135 | + }; |
| 136 | + expect(hasLabelledChildImage(node)).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it("returns false if no image component is found", () => { |
| 140 | + const node: TSESTree.JSXElement = { |
| 141 | + type: AST_NODE_TYPES.JSXElement, |
| 142 | + openingElement: { |
| 143 | + type: AST_NODE_TYPES.JSXOpeningElement, |
| 144 | + name: { type: AST_NODE_TYPES.JSXIdentifier, name: "div", ...createMockLocRange() }, |
| 145 | + attributes: [], |
| 146 | + selfClosing: false, |
| 147 | + ...createMockLocRange() |
| 148 | + }, |
| 149 | + closingElement: null, |
| 150 | + children: [], |
| 151 | + ...createMockLocRange() |
| 152 | + }; |
| 153 | + |
| 154 | + expect(hasLabelledChildImage(node)).toBe(false); |
| 155 | + }); |
| 156 | +}); |
0 commit comments