Skip to content

Commit

Permalink
fix tests by adding validation logic for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Oct 19, 2024
1 parent cade41c commit 96d90ae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const addNamespace = util.deprecate((namespace, namespaces) => {
namespaces.push(namespace);
}
}, 'Using the "namespace" option of @namics/stylelint-bem is deprecated. ' +
'Please use the new namespaces option which allows using multiple namespaces');
'Please use the new namespaces option which allows using multiple namespaces');

module.exports = stylelint.createPlugin(ruleName, (options) => {
options = options || '';
Expand Down Expand Up @@ -176,13 +176,28 @@ module.exports = stylelint.createPlugin(ruleName, (options) => {
return `use the ${getValidSyntax(className, namespaces)} syntax`;
}
}

const isString = (val) => typeof val === 'string' || val instanceof String;
return (root, result) => {
let possible;
if (options === true || options === false) {
possible = [true, false];
} else {
possible = {
patternPrefixes: [isString],
helperPrefixes: [isString],
namespaces: [isString],
namespace: isString,
};
}

const validOptions = stylelint.utils.validateOptions(
result,
ruleName,
{ actual: options },
);
{
actual: options,
optional: true,
possible,
});

if (!validOptions) {
return;
Expand Down
40 changes: 40 additions & 0 deletions test/invalid-config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { ruleName } = require('../index');

testRule({
ruleName,
config: {
invalidKey: 'string',
},
reject: [
{
code: '.test {}',
message: `Invalid option name "invalidKey" for rule "${ruleName}"`,
},
],
});

testRule({
ruleName,
config: {
namespace: true,
},
reject: [
{
code: '.test {}',
message: `Invalid value "true" for option "namespace" of rule "${ruleName}"`,
},
],
});

testRule({
ruleName,
config: {
patternPrefixes: 42,
},
reject: [
{
code: '.test {}',
message: `Invalid value "42" for option "patternPrefixes" of rule "${ruleName}"`,
},
],
});

0 comments on commit 96d90ae

Please sign in to comment.