Skip to content

Commit 88e7e01

Browse files
Added tests to increase coverage.
1 parent 009cdbd commit 88e7e01

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

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

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,157 @@ describe("hasLabelledChildImage", () => {
156156
expect(hasLabelledChildImage(node)).toBe(false);
157157
});
158158
});
159+
160+
describe("hasLabelledChildImage - missing coverage", () => {
161+
it("covers line 34 - handles non-JSXElement children", () => {
162+
// This test covers the case where children exist but are not JSXElement types
163+
const mockTextChild: TSESTree.JSXText = {
164+
type: AST_NODE_TYPES.JSXText,
165+
value: "Some text content",
166+
raw: "Some text content",
167+
...createMockLocRange()
168+
};
169+
170+
const mockExpressionChild: TSESTree.JSXExpressionContainer = {
171+
type: AST_NODE_TYPES.JSXExpressionContainer,
172+
expression: {
173+
type: AST_NODE_TYPES.Literal,
174+
value: "expression",
175+
raw: '"expression"',
176+
...createMockLocRange()
177+
},
178+
...createMockLocRange()
179+
};
180+
181+
const node: TSESTree.JSXElement = {
182+
type: AST_NODE_TYPES.JSXElement,
183+
openingElement: {
184+
type: AST_NODE_TYPES.JSXOpeningElement,
185+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "Container", ...createMockLocRange() },
186+
attributes: [],
187+
selfClosing: false,
188+
...createMockLocRange()
189+
},
190+
closingElement: null,
191+
children: [mockTextChild, mockExpressionChild], // Non-JSXElement children
192+
...createMockLocRange()
193+
};
194+
195+
// This should execute line 34 and return false since no JSXElement children are found
196+
expect(hasLabelledChildImage(node)).toBe(false);
197+
});
198+
199+
it("covers line 34 - JSXElement child with non-image component name", () => {
200+
// This covers the case where we have JSXElement children but they're not image components
201+
const mockDivChild: TSESTree.JSXElement = {
202+
type: AST_NODE_TYPES.JSXElement,
203+
openingElement: {
204+
type: AST_NODE_TYPES.JSXOpeningElement,
205+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "div", ...createMockLocRange() }, // Not an image component
206+
attributes: [
207+
{
208+
type: AST_NODE_TYPES.JSXAttribute,
209+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "alt", ...createMockLocRange() },
210+
value: { type: AST_NODE_TYPES.Literal, value: "description", raw: '"description"', ...createMockLocRange() },
211+
...createMockLocRange()
212+
}
213+
],
214+
selfClosing: false,
215+
...createMockLocRange()
216+
},
217+
closingElement: null,
218+
children: [],
219+
...createMockLocRange()
220+
};
221+
222+
const node: TSESTree.JSXElement = {
223+
type: AST_NODE_TYPES.JSXElement,
224+
openingElement: {
225+
type: AST_NODE_TYPES.JSXOpeningElement,
226+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "Container", ...createMockLocRange() },
227+
attributes: [],
228+
selfClosing: false,
229+
...createMockLocRange()
230+
},
231+
closingElement: null,
232+
children: [mockDivChild],
233+
...createMockLocRange()
234+
};
235+
236+
// This should execute line 34 and return false since child is not an image component
237+
expect(hasLabelledChildImage(node)).toBe(false);
238+
});
239+
});
240+
241+
describe("isImageHidden - missing coverage for aria-label handling", () => {
242+
it("covers lines 70-72 - returns false when aria-label has non-empty value", () => {
243+
// This covers the case where aria-label is present and has a value (lines 70-72)
244+
const attributes: TSESTree.JSXOpeningElement["attributes"] = [
245+
{
246+
type: AST_NODE_TYPES.JSXAttribute,
247+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "aria-label", ...createMockLocRange() },
248+
value: {
249+
type: AST_NODE_TYPES.Literal,
250+
value: "Descriptive label",
251+
raw: '"Descriptive label"',
252+
...createMockLocRange()
253+
},
254+
...createMockLocRange()
255+
}
256+
];
257+
258+
// Should return false because aria-label has a value, meaning image is not hidden
259+
expect(isImageHidden(attributes)).toBe(false);
260+
});
261+
262+
it("covers lines 70-72 - returns false when aria-labelledby has non-empty value", () => {
263+
// This covers the case where aria-labelledby is present and has a value
264+
const attributes: TSESTree.JSXOpeningElement["attributes"] = [
265+
{
266+
type: AST_NODE_TYPES.JSXAttribute,
267+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "aria-labelledby", ...createMockLocRange() },
268+
value: {
269+
type: AST_NODE_TYPES.Literal,
270+
value: "label-id",
271+
raw: '"label-id"',
272+
...createMockLocRange()
273+
},
274+
...createMockLocRange()
275+
}
276+
];
277+
278+
// Should return false because aria-labelledby has a value, meaning image is not hidden
279+
expect(isImageHidden(attributes)).toBe(false);
280+
});
281+
282+
it("handles case with both aria-label and aria-labelledby present", () => {
283+
// Additional test to ensure proper handling when both attributes are present
284+
const attributes: TSESTree.JSXOpeningElement["attributes"] = [
285+
{
286+
type: AST_NODE_TYPES.JSXAttribute,
287+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "aria-label", ...createMockLocRange() },
288+
value: {
289+
type: AST_NODE_TYPES.Literal,
290+
value: "Primary label",
291+
raw: '"Primary label"',
292+
...createMockLocRange()
293+
},
294+
...createMockLocRange()
295+
},
296+
{
297+
type: AST_NODE_TYPES.JSXAttribute,
298+
name: { type: AST_NODE_TYPES.JSXIdentifier, name: "aria-labelledby", ...createMockLocRange() },
299+
value: {
300+
type: AST_NODE_TYPES.Literal,
301+
value: "secondary-label",
302+
raw: '"secondary-label"',
303+
...createMockLocRange()
304+
},
305+
...createMockLocRange()
306+
}
307+
];
308+
309+
// Should return false because aria-label has a value
310+
expect(isImageHidden(attributes)).toBe(false);
311+
});
312+
});

0 commit comments

Comments
 (0)