-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathimgAltEmpty.test.js
More file actions
137 lines (125 loc) · 4.26 KB
/
Copy pathimgAltEmpty.test.js
File metadata and controls
137 lines (125 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import axe from 'axe-core';
// Use async/await for the test setup
beforeAll( async () => {
// Dynamically import the modules
const imgAltEmptyRuleModule = await import( '../../../src/pageScanner/rules/img-alt-empty.js' );
const imgAltEmptyCheckModule = await import( '../../../src/pageScanner/checks/img-alt-empty-check.js' );
const imgAltEmptyRule = imgAltEmptyRuleModule.default;
const imgAltEmptyCheck = imgAltEmptyCheckModule.default;
// Configure axe with the imported rules
axe.configure( {
rules: [ imgAltEmptyRule ],
checks: [ imgAltEmptyCheck ],
} );
} );
// Reset the document between tests
beforeEach( () => {
document.body.innerHTML = '';
} );
describe( 'Image Alt Empty Validation', () => {
const testCases = [
// Failing cases - should be flagged as violations
{
name: 'should fail for img with empty alt attribute',
html: '<img src="test.jpg" alt="">',
shouldPass: false,
},
{
name: 'should fail for input[type=image] with empty alt attribute',
html: '<input type="image" src="button.jpg" alt="">',
shouldPass: false,
},
{
name: 'should fail for img with empty alt attribute in a div',
html: '<div><img src="test.jpg" alt=""></div>',
shouldPass: false,
},
{
name: 'should fail for multiple images with empty alt attributes',
html: '<div><img src="test1.jpg" alt=""><img src="test2.jpg" alt=""></div>',
shouldPass: false,
},
// Passing cases
{
name: 'should pass for img with non-empty alt attribute',
html: '<img src="test.jpg" alt="Description of image">',
shouldPass: true,
},
{
name: 'should pass for input[type=image] with non-empty alt attribute',
html: '<input type="image" src="button.jpg" alt="Submit button">',
shouldPass: true,
},
{
name: 'should pass for img with role="presentation"',
html: '<img src="decorative.jpg" alt="" role="presentation">',
shouldPass: true,
},
{
name: 'should pass for img with role="none"',
html: '<img src="decorative.jpg" alt="" role="none">',
shouldPass: true,
},
{
name: 'should pass for img without alt attribute',
html: '<img src="test.jpg">',
shouldPass: true, // This check is specifically for empty alt attributes, not missing ones
},
{
name: 'should pass for input[type=text] with empty alt',
html: '<input type="text" alt="">',
shouldPass: true, // Only input[type=image] should be checked
},
// Test cases for images with captions (simulating old php function edac_img_alt_ignore_inside_valid_caption)
{
name: 'should pass for img with empty alt inside figure with figcaption',
html: '<figure><img src="test.jpg" alt=""><figcaption>Image caption</figcaption></figure>',
shouldPass: true,
},
{
name: 'should pass for img with empty alt inside WordPress caption',
html: '<div class="wp-caption"><img src="test.jpg" alt=""><p class="wp-caption-text">Caption text</p></div>',
shouldPass: true,
},
// Plugin-specific edge cases (simulating old php function edac_img_alt_ignore_plugin_issues)s
{
name: 'should pass for img with class containing "wp-smiley"',
html: '<img src="smiley.jpg" alt="" class="wp-smiley">',
shouldPass: true,
},
// Tracking pixel (1x1) edge cases
{
name: 'should pass for 1x1 tracking pixel with empty alt (gif)',
html: '<img src="track.gif" alt="" width="1" height="1">',
shouldPass: true,
},
{
name: 'should pass for 1x1 tracking pixel with empty alt (arbitrary src)',
html: '<img src="https://example.com/whatever.png" alt="" width="1" height="1">',
shouldPass: true,
},
{
name: 'should fail for 2x1 image with empty alt (not a tracking pixel)',
html: '<img src="tiny.png" alt="" width="2" height="1">',
shouldPass: false,
},
{
name: 'should fail for img with empty alt and no dimension attributes (cannot confirm tracking pixel without dimensions)',
html: '<img src="maybe-tracker.gif" alt="">',
shouldPass: false,
},
];
testCases.forEach( ( testCase ) => {
test( testCase.name, async () => {
document.body.innerHTML = testCase.html;
const results = await axe.run( document.body, {
runOnly: [ 'img_alt_empty' ],
} );
if ( testCase.shouldPass ) {
expect( results.violations.length ).toBe( 0 );
} else {
expect( results.violations.length ).toBeGreaterThan( 0 );
}
} );
} );
} );