2 * @fileoverview Tests for ConfigOps
3 * @author Ian VanSchooten
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const assert
= require("chai").assert
,
13 ConfigRule
= require("../../../lib/init/config-rule"),
14 builtInRules
= require("../../../lib/rules"),
15 schema
= require("../../fixtures/config-rule/schemas");
17 //------------------------------------------------------------------------------
19 //------------------------------------------------------------------------------
23 describe("ConfigRule", () => {
25 describe("generateConfigsFromSchema()", () => {
28 it("should create a config with only severity for an empty schema", () => {
29 actualConfigs
= ConfigRule
.generateConfigsFromSchema([]);
30 assert
.deepStrictEqual(actualConfigs
, [SEVERITY
]);
33 it("should create a config with only severity with no arguments", () => {
34 actualConfigs
= ConfigRule
.generateConfigsFromSchema();
35 assert
.deepStrictEqual(actualConfigs
, [SEVERITY
]);
38 describe("for a single enum schema", () => {
41 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.enum);
44 it("should create an array of configs", () => {
45 assert
.isArray(actualConfigs
);
46 assert
.strictEqual(actualConfigs
.length
, 3);
49 it("should include the error severity (2) without options as the first config", () => {
50 assert
.strictEqual(actualConfigs
[0], SEVERITY
);
53 it("should set all configs to error severity (2)", () => {
54 actualConfigs
.forEach(actualConfig
=> {
55 if (Array
.isArray(actualConfig
)) {
56 assert
.strictEqual(actualConfig
[0], SEVERITY
);
61 it("should return configs with each enumerated value in the schema", () => {
62 assert
.sameDeepMembers(actualConfigs
, [SEVERITY
, [SEVERITY
, "always"], [SEVERITY
, "never"]]);
66 describe("for a object schema with a single enum property", () => {
69 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.objectWithEnum
);
72 it("should return configs with option objects", () => {
74 // Skip first config (severity only)
75 actualConfigs
.slice(1).forEach(actualConfig
=> {
76 const actualConfigOption
= actualConfig
[1]; // severity is first element, option is second
78 assert
.isObject(actualConfigOption
);
82 it("should use the object property name from the schema", () => {
83 const propName
= "enumProperty";
85 assert
.strictEqual(actualConfigs
.length
, 3);
86 actualConfigs
.slice(1).forEach(actualConfig
=> {
87 const actualConfigOption
= actualConfig
[1];
89 assert
.property(actualConfigOption
, propName
);
93 it("should have each enum as option object values", () => {
94 const propName
= "enumProperty",
97 actualConfigs
.slice(1).forEach(actualConfig
=> {
98 const configOption
= actualConfig
[1];
100 actualValues
.push(configOption
[propName
]);
102 assert
.sameMembers(actualValues
, ["always", "never"]);
106 describe("for a object schema with a multiple enum properties", () => {
109 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.objectWithMultipleEnums
);
112 it("should create configs for all properties in each config", () => {
113 const expectedProperties
= ["firstEnum", "anotherEnum"];
115 assert
.strictEqual(actualConfigs
.length
, 7);
116 actualConfigs
.slice(1).forEach(actualConfig
=> {
117 const configOption
= actualConfig
[1];
118 const actualProperties
= Object
.keys(configOption
);
120 assert
.sameMembers(actualProperties
, expectedProperties
);
124 it("should create configs for every possible combination", () => {
125 const expectedConfigs
= [
126 { firstEnum
: "always", anotherEnum
: "var" },
127 { firstEnum
: "always", anotherEnum
: "let" },
128 { firstEnum
: "always", anotherEnum
: "const" },
129 { firstEnum
: "never", anotherEnum
: "var" },
130 { firstEnum
: "never", anotherEnum
: "let" },
131 { firstEnum
: "never", anotherEnum
: "const" }
133 const actualConfigOptions
= actualConfigs
.slice(1).map(actualConfig
=> actualConfig
[1]);
135 assert
.sameDeepMembers(actualConfigOptions
, expectedConfigs
);
140 describe("for a object schema with a single boolean property", () => {
143 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.objectWithBool
);
146 it("should return configs with option objects", () => {
147 assert
.strictEqual(actualConfigs
.length
, 3);
148 actualConfigs
.slice(1).forEach(actualConfig
=> {
149 const actualConfigOption
= actualConfig
[1];
151 assert
.isObject(actualConfigOption
);
155 it("should use the object property name from the schema", () => {
156 const propName
= "boolProperty";
158 assert
.strictEqual(actualConfigs
.length
, 3);
159 actualConfigs
.slice(1).forEach(actualConfig
=> {
160 const actualConfigOption
= actualConfig
[1];
162 assert
.property(actualConfigOption
, propName
);
166 it("should include both true and false configs", () => {
167 const propName
= "boolProperty",
170 actualConfigs
.slice(1).forEach(actualConfig
=> {
171 const configOption
= actualConfig
[1];
173 actualValues
.push(configOption
[propName
]);
175 assert
.sameMembers(actualValues
, [true, false]);
179 describe("for a object schema with a multiple bool properties", () => {
182 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.objectWithMultipleBools
);
185 it("should create configs for all properties in each config", () => {
186 const expectedProperties
= ["firstBool", "anotherBool"];
188 assert
.strictEqual(actualConfigs
.length
, 5);
189 actualConfigs
.slice(1).forEach(config
=> {
190 const configOption
= config
[1];
191 const actualProperties
= Object
.keys(configOption
);
193 assert
.sameMembers(actualProperties
, expectedProperties
);
197 it("should create configs for every possible combination", () => {
198 const expectedConfigOptions
= [
199 { firstBool
: true, anotherBool
: true },
200 { firstBool
: true, anotherBool
: false },
201 { firstBool
: false, anotherBool
: true },
202 { firstBool
: false, anotherBool
: false }
204 const actualConfigOptions
= actualConfigs
.slice(1).map(config
=> config
[1]);
206 assert
.sameDeepMembers(actualConfigOptions
, expectedConfigOptions
);
210 describe("for a schema with an enum and an object", () => {
213 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.mixedEnumObject
);
216 it("should create configs with only the enum values", () => {
217 assert
.strictEqual(actualConfigs
[1].length
, 2);
218 assert
.strictEqual(actualConfigs
[2].length
, 2);
219 const actualOptions
= [actualConfigs
[1][1], actualConfigs
[2][1]];
221 assert
.sameMembers(actualOptions
, ["always", "never"]);
224 it("should create configs with a string and an object", () => {
225 assert
.strictEqual(actualConfigs
.length
, 7);
226 actualConfigs
.slice(3).forEach(config
=> {
227 assert
.isString(config
[1]);
228 assert
.isObject(config
[2]);
233 describe("for a schema with an enum followed by an object with no usable properties", () => {
235 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.mixedEnumObjectWithNothing
);
238 it("should create config only for the enum", () => {
239 const expectedConfigs
= [2, [2, "always"], [2, "never"]];
241 assert
.sameDeepMembers(actualConfigs
, expectedConfigs
);
245 describe("for a schema with an enum preceded by an object with no usable properties", () => {
247 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.mixedObjectWithNothingEnum
);
250 it("should not create a config for the enum", () => {
251 const expectedConfigs
= [2];
253 assert
.sameDeepMembers(actualConfigs
, expectedConfigs
);
257 describe("for a schema with an enum preceded by a string", () => {
259 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.mixedStringEnum
);
262 it("should not create a config for the enum", () => {
263 const expectedConfigs
= [2];
265 assert
.sameDeepMembers(actualConfigs
, expectedConfigs
);
269 describe("for a schema with oneOf", () => {
272 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.oneOf
);
275 it("should create a set of configs", () => {
276 assert
.isArray(actualConfigs
);
280 describe("for a schema with nested objects", () => {
283 actualConfigs
= ConfigRule
.generateConfigsFromSchema(schema
.nestedObjects
);
286 it("should create a set of configs", () => {
287 assert
.isArray(actualConfigs
);
292 describe("createCoreRuleConfigs()", () => {
294 const rulesConfig
= ConfigRule
.createCoreRuleConfigs();
296 it("should create a rulesConfig containing all core rules", () => {
298 expectedRules
= Array
.from(builtInRules
.keys()),
299 actualRules
= Object
.keys(rulesConfig
);
301 assert
.sameMembers(actualRules
, expectedRules
);
304 it("should allow to ignore deprecated rules", () => {
305 const expectedRules
= Array
.from(builtInRules
.entries())
306 .filter(([, rule
]) => {
307 const isDeprecated
= (typeof rule
=== "function") ? rule
.deprecated
: rule
.meta
.deprecated
;
309 return !isDeprecated
;
312 actualRules
= Object
.keys(ConfigRule
.createCoreRuleConfigs(true));
314 assert
.sameMembers(actualRules
, expectedRules
);
316 // Make sure it doesn't contain deprecated rules.
317 assert
.notInclude(actualRules
, "newline-after-var");
320 it("should create arrays of configs for rules", () => {
321 assert
.isArray(rulesConfig
.quotes
);
322 assert
.include(rulesConfig
.quotes
, 2);
325 it("should create configs for rules with meta", () => {
326 assert(rulesConfig
["accessor-pairs"].length
> 1);