All files / src/config config.spec.ts

100% Statements 52/52
100% Branches 0/0
100% Functions 21/21
100% Lines 52/52
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1531x 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   1x 1x 1x 1x            
const path = require('path');
import { Config } from './config';
 
describe('config', function(){
 
	const expect = require('chai').expect;
 
	it('should load defaults', function(){
		const config = Config.empty();
		expect(config.get()).to.not.be.undefined;
	});
 
	it('should contain no rules by default', function(){
		const config = Config.empty();
		expect(Object.keys(config.get().rules)).to.have.lengthOf(0);
	});
 
	describe('getRules()', function(){
 
		it('should return parsed rules', function(){
			const config = Config.fromObject({rules: {foo: 'error'}});
			expect(config.get().rules).to.deep.equal({foo: 'error'});
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_ERROR, {}],
			});
		});
 
		it('getRules() should parse severity from string', function(){
			const config = Config.fromObject({
				rules: {
					foo: 'error',
					bar: 'warn',
					baz: 'disable',
				},
			});
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_ERROR, {}],
				bar: [Config.SEVERITY_WARN, {}],
				baz: [Config.SEVERITY_DISABLED, {}],
			});
		});
 
		it('getRules() should retain severity from integer', function(){
			const config = Config.fromObject({
				rules: {
					foo: 2,
					bar: 1,
					baz: 0,
				},
			});
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_ERROR, {}],
				bar: [Config.SEVERITY_WARN, {}],
				baz: [Config.SEVERITY_DISABLED, {}],
			});
		});
 
		it('getRules() should retain options', function(){
			const config = Config.fromObject({
				rules: {
					foo: [2, {foo: true}],
					bar: ["error", {bar: false}],
				},
			});
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_ERROR, {foo: true}],
				bar: [Config.SEVERITY_ERROR, {bar: false}],
			});
		});
 
	});
 
	describe('fromFile()', function(){
 
		it('should support JSON', function(){
			const config = Config.fromFile(`${process.cwd()}/test-files/config.json`);
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_ERROR, {}],
				bar: [Config.SEVERITY_WARN, {}],
				baz: [Config.SEVERITY_DISABLED, {}],
			});
		});
 
	});
 
	describe('extend', function(){
 
		it('should extend base configuration', function(){
			const config = Config.fromObject({
				extends: [`${process.cwd()}/test-files/config.json`],
				rules: {
					foo: 1,
				},
			});
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_WARN, {}],
				bar: [Config.SEVERITY_WARN, {}],
				baz: [Config.SEVERITY_DISABLED, {}],
			});
		});
 
		it('should support deep extending', function(){
			const config = Config.fromObject({
				extends: [`${process.cwd()}/test-files/config-extending.json`],
			});
			expect(config.getRules()).to.deep.equal({
				foo: [Config.SEVERITY_ERROR, {}],
				bar: [Config.SEVERITY_WARN, {}],
				baz: [Config.SEVERITY_ERROR, {}],
			});
		});
 
		it('should support htmlvalidate:recommended', function(){
			const config = Config.fromObject({
				extends: ['htmlvalidate:recommended'],
			});
			expect(config.getRules()).to.be.an('object');
		});
 
	});
 
	describe('expandRelative()', function(){
 
		it('should expand ./foo', function(){
			expect(Config.expandRelative('./foo', '/path')).to.equal(path.join(path.sep, 'path', 'foo'));
		});
 
		it('should expand ../foo', function(){
			expect(Config.expandRelative('../foo', '/path/bar')).to.equal(path.join(path.sep, 'path', 'foo'));
		});
 
		it('should not expand /foo', function(){
			expect(Config.expandRelative('/foo', '/path')).to.equal('/foo');
		});
 
		it('should not expand foo', function(){
			expect(Config.expandRelative('foo', '/path')).to.equal('foo');
		});
 
	});
 
	describe('getMetaTable()', function(){
 
		it('should load metadata', function(){
			const config = Config.empty();
			const metatable = config.getMetaTable();
			expect(Object.keys(metatable.elements)).not.to.have.lengthOf(0);
		});
 
	});
 
});