All files / src/rules img-req-alt.spec.ts

100% Statements 46/46
100% Branches 0/0
100% Functions 18/18
100% Lines 46/46
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 991x   1x   1x       1x   1x 1x         1x 1x 1x     1x 1x 1x     1x 1x 1x 1x         1x   1x 1x         1x 1x 1x     1x 1x 1x 1x     1x 1x 1x 1x         1x   1x 1x         1x 1x 1x     1x 1x 1x 1x         1x   1x 1x         1x 1x 1x            
import HtmlValidate from '../htmlvalidate';
 
describe('rule img-req-alt', function(){
 
	const expect = require('chai').expect;
 
	let htmlvalidate: HtmlValidate;
 
	describe('with default options', function(){
 
		before(function(){
			htmlvalidate = new HtmlValidate({
				rules: {'img-req-alt': 'error'},
			});
		});
 
		it('should not report when img has alt attribute', function(){
			const report = htmlvalidate.validateString('<img alt="foobar">');
			expect(report).to.be.valid;
		});
 
		it('should not report when img has empty alt attribute', function(){
			const report = htmlvalidate.validateString('<img alt="">');
			expect(report).to.be.valid;
		});
 
		it('should report error when img is missing alt attribute', function(){
			const report = htmlvalidate.validateString('<img>');
			expect(report).to.be.invalid;
			expect(report).to.have.error('img-req-alt', '<img> is missing required alt attribute');
		});
 
	});
 
	describe('with allowEmpty false', function(){
 
		before(function(){
			htmlvalidate = new HtmlValidate({
				rules: {'img-req-alt': ['error', {allowEmpty: false}]},
			});
		});
 
		it('should not report when img has alt attribute', function(){
			const report = htmlvalidate.validateString('<img alt="foobar">');
			expect(report).to.be.valid;
		});
 
		it('should report when img has empty alt attribute', function(){
			const report = htmlvalidate.validateString('<img alt="">');
			expect(report).to.be.invalid;
			expect(report).to.have.error('img-req-alt', '<img> is missing required alt attribute');
		});
 
		it('should report error when img is missing alt attribute', function(){
			const report = htmlvalidate.validateString('<img>');
			expect(report).to.be.invalid;
			expect(report).to.have.error('img-req-alt', '<img> is missing required alt attribute');
		});
 
	});
 
	describe('with alias', function(){
 
		before(function(){
			htmlvalidate = new HtmlValidate({
				rules: {'img-req-alt': ['error', {alias: 'translate-attr'}]},
			});
		});
 
		it('should not report when img has alias attribute set', function(){
			const report = htmlvalidate.validateString('<img translate-attr="...">');
			expect(report).to.be.valid;
		});
 
		it('should report error when img is missing both alt and aliases', function(){
			const report = htmlvalidate.validateString('<img>');
			expect(report).to.be.invalid;
			expect(report).to.have.error('img-req-alt', '<img> is missing required alt attribute');
		});
 
	});
 
	describe('with alias (array)', function(){
 
		before(function(){
			htmlvalidate = new HtmlValidate({
				rules: {'img-req-alt': ['error', {alias: ['translate-attr']}]},
			});
		});
 
		it('should not report when img has alias attribute set', function(){
			const report = htmlvalidate.validateString('<img translate-attr="...">');
			expect(report).to.be.valid;
		});
 
	});
 
});