|
| 1 | +import axe from 'axe-core'; |
| 2 | + |
| 3 | +beforeAll( async () => { |
| 4 | + const textSmallRuleModule = await import( '../../../src/pageScanner/rules/text-small.js' ); |
| 5 | + const textSizeTooSmallCheckModule = await import( '../../../src/pageScanner/checks/text-size-too-small.js' ); |
| 6 | + const textSmallRule = textSmallRuleModule.default; |
| 7 | + const textSizeTooSmallCheck = textSizeTooSmallCheckModule.default; |
| 8 | + |
| 9 | + axe.configure( { |
| 10 | + rules: [ textSmallRule ], |
| 11 | + checks: [ textSizeTooSmallCheck ], |
| 12 | + } ); |
| 13 | +} ); |
| 14 | + |
| 15 | +beforeEach( () => { |
| 16 | + document.body.innerHTML = ''; |
| 17 | +} ); |
| 18 | + |
| 19 | +describe( 'Text Small Validation', () => { |
| 20 | + const testCases = [ |
| 21 | + // PASSING CASES |
| 22 | + { |
| 23 | + name: 'should pass for text above the size threshold', |
| 24 | + html: '<p style="font-size: 14px;">Normal sized text</p>', |
| 25 | + shouldPass: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: 'should pass for text exactly above the threshold', |
| 29 | + html: '<span style="font-size: 11px;">Slightly above threshold</span>', |
| 30 | + shouldPass: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'should pass for element with no text content', |
| 34 | + html: '<p style="font-size: 8px;"></p>', |
| 35 | + shouldPass: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: 'should pass for container element with no direct text children', |
| 39 | + html: '<div style="font-size: 8px;"><span>Child text</span></div>', |
| 40 | + shouldPass: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'should pass for screen-reader-only text using classic clip pattern', |
| 44 | + html: '<span style="position: absolute; width: 1px; height: 1px; clip: rect(0px, 0px, 0px, 0px); overflow: hidden; font-size: 8px;">Screen reader only</span>', |
| 45 | + shouldPass: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: 'should pass for screen-reader-only text using clip-path pattern', |
| 49 | + html: '<span style="clip-path: inset(50%); font-size: 8px;">Screen reader only</span>', |
| 50 | + shouldPass: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: 'should pass for screen-reader-only text using dimension-based pattern (no clip)', |
| 54 | + html: '<span style="position: absolute; width: 1px; height: 1px; overflow: hidden; font-size: 8px;">Screen reader only</span>', |
| 55 | + shouldPass: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'should pass for screen-reader-only text nested inside a visible element (Elementor social icon pattern)', |
| 59 | + html: '<a href="#" style="font-size: 8px;"><span style="position: absolute; width: 1px; height: 1px; overflow: hidden;">Facebook</span><svg aria-hidden="true"></svg></a>', |
| 60 | + shouldPass: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'should pass for text with font-size: 0 inherited from a layout container (Elementor social icons pattern)', |
| 64 | + html: '<div style="font-size: 0;"><span>Screen reader label</span></div>', |
| 65 | + shouldPass: true, |
| 66 | + }, |
| 67 | + |
| 68 | + // FAILING CASES |
| 69 | + { |
| 70 | + name: 'should fail for clip without absolute/fixed position (clip has no visual effect)', |
| 71 | + html: '<span style="position: static; clip: rect(0px, 0px, 0px, 0px); font-size: 8px;">Clip but not positioned</span>', |
| 72 | + shouldPass: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'should fail for text at the threshold boundary (10px)', |
| 76 | + html: '<span style="font-size: 10px;">Threshold text</span>', |
| 77 | + shouldPass: false, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: 'should fail for text below the threshold', |
| 81 | + html: '<p style="font-size: 8px;">Small text</p>', |
| 82 | + shouldPass: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'should fail for small text in a heading', |
| 86 | + html: '<h2 style="font-size: 9px;">Small heading</h2>', |
| 87 | + shouldPass: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'should fail for small text in a link', |
| 91 | + html: '<a href="#" style="font-size: 7px;">Tiny link</a>', |
| 92 | + shouldPass: false, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'should fail for small text in a list item', |
| 96 | + html: '<ul><li style="font-size: 8px;">Small list item</li></ul>', |
| 97 | + shouldPass: false, |
| 98 | + }, |
| 99 | + ]; |
| 100 | + |
| 101 | + testCases.forEach( ( testCase ) => { |
| 102 | + it( testCase.name, async () => { |
| 103 | + document.body.innerHTML = testCase.html; |
| 104 | + |
| 105 | + const results = await axe.run( document, { |
| 106 | + runOnly: [ 'text_small' ], |
| 107 | + } ); |
| 108 | + |
| 109 | + if ( testCase.shouldPass ) { |
| 110 | + expect( results.violations.length ).toBe( 0 ); |
| 111 | + } else { |
| 112 | + expect( results.violations.length ).toBeGreaterThan( 0 ); |
| 113 | + expect( results.violations[ 0 ].id ).toBe( 'text_small' ); |
| 114 | + } |
| 115 | + } ); |
| 116 | + } ); |
| 117 | +} ); |
0 commit comments