|
| 1 | +const noEmptyStringAltRule = require("../src/rules/no-empty-alt-text"); |
| 2 | +const runTest = require("./utils/run-test").runTest; |
| 3 | + |
| 4 | +describe("GH003: No Empty Alt Text", () => { |
| 5 | + describe("successes", () => { |
| 6 | + test("html image", async () => { |
| 7 | + const strings = [ |
| 8 | + '<img alt="A helpful description" src="https://user-images.githubusercontent.com/abcdef.png">', |
| 9 | + "`<img alt='' src='image.png'>`", // code block |
| 10 | + ]; |
| 11 | + |
| 12 | + const results = await runTest(strings, noEmptyStringAltRule); |
| 13 | + expect(results).toHaveLength(0); |
| 14 | + }); |
| 15 | + }); |
| 16 | + describe("failures", () => { |
| 17 | + test("HTML example", async () => { |
| 18 | + const strings = [ |
| 19 | + '<img alt="" src="https://user-images.githubusercontent.com/abcdef.png">', |
| 20 | + "<img alt='' src='https://user-images.githubusercontent.com/abcdef.png'>", |
| 21 | + '<img src="cat.png" alt="" /> <img src="dog.png" alt="" />', |
| 22 | + ]; |
| 23 | + |
| 24 | + const results = await runTest(strings, noEmptyStringAltRule); |
| 25 | + |
| 26 | + const failedRules = results |
| 27 | + .map((result) => result.ruleNames) |
| 28 | + .flat() |
| 29 | + .filter((name) => !name.includes("GH")); |
| 30 | + |
| 31 | + expect(failedRules).toHaveLength(4); |
| 32 | + for (const rule of failedRules) { |
| 33 | + expect(rule).toBe("no-empty-alt-text"); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + test("error message", async () => { |
| 38 | + const strings = [ |
| 39 | + '<img alt="" src="https://user-images.githubusercontent.com/abcdef.png">', |
| 40 | + '<img src="cat.png" alt="" /> <img src="dog.png" alt="" />', |
| 41 | + ]; |
| 42 | + |
| 43 | + const results = await runTest(strings, noEmptyStringAltRule); |
| 44 | + |
| 45 | + expect(results[0].ruleDescription).toMatch( |
| 46 | + "Please provide an alternative text for the image.", |
| 47 | + ); |
| 48 | + expect(results[0].errorRange).toEqual([6, 6]); |
| 49 | + |
| 50 | + expect(results[1].ruleDescription).toMatch( |
| 51 | + "Please provide an alternative text for the image.", |
| 52 | + ); |
| 53 | + expect(results[1].errorRange).toEqual([20, 6]); |
| 54 | + expect(results[2].ruleDescription).toMatch( |
| 55 | + "Please provide an alternative text for the image.", |
| 56 | + ); |
| 57 | + expect(results[2].errorRange).toEqual([49, 6]); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments