All files / src/rules element-name.spec.ts

100% Statements 48/48
100% Branches 0/0
100% Functions 16/16
100% Lines 48/48
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 951x   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 1x 1x        
import HtmlValidate from '../htmlvalidate';
 
describe('rule element-name', function(){
 
	const expect = require('chai').expect;
 
	let htmlvalidate: HtmlValidate;
 
	describe('configured with default pattern', function(){
 
		before(function(){
			htmlvalidate = new HtmlValidate({
				rules: {'element-name': 'error'},
			});
		});
 
		it('should report error when custom element name does not have a dash', function(){
			const report = htmlvalidate.validateString('<foobar></foobar>');
			expect(report).to.be.invalid;
			expect(report).to.have.error('element-name', '<foobar> is not a valid element name');
		});
 
		it('should report error when custom element name does not start with letter', function(){
			const report = htmlvalidate.validateString('<1-foo></1-foo>');
			expect(report).to.be.invalid;
			expect(report).to.have.error('element-name', '<1-foo> is not a valid element name');
		});
 
		it('should not report error when custom element name is valid', function(){
			const report = htmlvalidate.validateString('<foo-bar></foo-bar>');
			expect(report.valid, "linting should report failure").to.be.true;
		});
 
		it('should not report when using builtin elements', function(){
			const report = htmlvalidate.validateString('<span><a><span></span></a></span>');
			expect(report.valid, "linting should report failure").to.be.true;
		});
 
		it('should not report error for xml namespaces', function(){
			const report = htmlvalidate.validateString('<xmlns:foo></xmlns:foo>');
			expect(report.valid, "linting should report failure").to.be.true;
		});
 
	});
 
	describe('configured with custom pattern', function(){
 
		before(function(){
			htmlvalidate = new HtmlValidate({
				rules: {'element-name': ['error', {pattern: '^foo-\\w+$'}]},
			});
		});
 
		it('should report error when custom element name does not match pattern', function(){
			const report = htmlvalidate.validateString('<spam-ham></spam-ham>');
			expect(report).to.be.invalid;
			expect(report).to.have.error('element-name', '<spam-ham> is not a valid element name');
		});
 
		it('should not report error when custom element name does match pattern', function(){
			const report = htmlvalidate.validateString('<foo-bar></foo-bar>');
			expect(report.valid, "linting should report failure").to.be.true;
		});
 
		it('should not report when using builtin elements', function(){
			const report = htmlvalidate.validateString('<span><a><span></span></a></span>');
			expect(report.valid, "linting should report failure").to.be.true;
		});
 
		it('should not report error for xml namespaces', function(){
			const report = htmlvalidate.validateString('<xmlns:foo></xmlns:foo>');
			expect(report.valid, "linting should report failure").to.be.true;
		});
 
	});
 
	it('should ignore whitelisted element', function(){
		htmlvalidate = new HtmlValidate({
			rules: {'element-name': ['error', {whitelist: ['foobar']}]},
		});
		const report = htmlvalidate.validateString('<foobar></foobar>');
		expect(report).to.be.valid;
	});
 
	it('should report error when using blacklisted element', function(){
		htmlvalidate = new HtmlValidate({
			rules: {'element-name': ['error', {blacklist: ['foo-bar']}]},
		});
		const report = htmlvalidate.validateString('<foo-bar></foo-bar>');
		expect(report).to.be.invalid;
		expect(report).to.have.error('element-name', '<foo-bar> element is blacklisted');
	});
 
});