Skip to content

Commit

Permalink
refactor: update linter creations in tests for legacy linter
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiKillertO committed Jul 24, 2020
1 parent 5328be9 commit 026503f
Show file tree
Hide file tree
Showing 53 changed files with 905 additions and 899 deletions.
36 changes: 18 additions & 18 deletions lib/rules/attr-bans/__tests__/index.js
Expand Up @@ -3,64 +3,64 @@ const linthtml = require("../../../index");
const none = require("../../../presets").presets.none;

describe("legacy linter | attr-bans", function() {
function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
function createLinter(config) {
return new linthtml.LegacyLinter(linthtml.rules, none, config);
}
it("Should not report an error for a tag named 'style'", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": ["style"] });
const html = "<body><style>hello</style></body>";
const issues = await linter.lint(html, none, { "attr-bans": ["style"] });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report anything when disabled", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": false });
const html = "<button style=\"color: red;\"></button>";
const issues = await linter.lint(html, none, { "attr-bans": false });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should accept a single string as option", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": "style" });
const html = "<button style=\"color: red;\"></button>";
const issues = await linter.lint(html, none, { "attr-bans": "style" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});

it("Banned attributes should be case insensitive", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": ["ban0", "bAN1"] });
const html = "<body ban0 ban1>";
const issues = await linter.lint(html, none, { "attr-bans": ["ban0", "bAN1"] });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});

it("Should accept regexes as config", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": [/on\w+/i] });
const html = "<div onClick='' onfocus='' noop=''></div>";
const issues = await linter.lint(html, none, { "attr-bans": [/on\w+/i] });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});

it("Should throw an error for an invalid config", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": true });
const html = "<button style=\"color: red;\"></button>";
expect(() => linter.lint(html, none, { "attr-bans": true }))
expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"attr-bans\" is invalid: Expected string, RegExp or (string|RegExp)[] got boolean");
});

it("Should throw an error if not given a list of strings as config", function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": ["string", true] });
const html = "<button style=\"color: red;\"></button>";
expect(() => linter.lint(html, none, { "attr-bans": ["string", true] }))
expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"attr-bans\" is invalid: Expected string, RegExp or (string|RegExp)[] got boolean[]");
});

it("Should report an error when the 'style' attribute is present", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-bans": ["style"] });
const html = "<button style=\"color: red;\"></button>";
const issues = await linter.lint(html, none, { "attr-bans": ["style"] });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});
});
Expand Down
56 changes: 28 additions & 28 deletions lib/rules/attr-name-style/__tests__/index.js
Expand Up @@ -3,103 +3,103 @@ const linthtml = require("../../../index");
const none = require("../../../presets").presets.none;

describe("legacy linter | attr-name-style", function() {
function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
function createLinter(config) {
return new linthtml.LegacyLinter(linthtml.rules, none, config);
}
it("Should ignore attributes matching \"raw-ignore-text\"", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "dash", "raw-ignore-regex": "{{.*?}}" });
const html = "<div an-attribute {{ logic }} another-attribute {{ end }}></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "dash", "raw-ignore-regex": "{{.*?}}" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report anything for correctly styled attribute names", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "lowercase" });
const html = "<div abc=\"\" fowj0wo3=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "lowercase" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should ignore ignored attributes", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "dash", "attr-name-ignore-regex": "xlink:href" });
const html = "<xml xlink:href=\"\"></xml>";
const issues = await linter.lint(html, none, { "attr-name-style": "dash", "attr-name-ignore-regex": "xlink:href" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should not report anything when disabled", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": false });
const html = "<div abc=\"\" 2fOwj_0o-3=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": false });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

describe("'lowercase' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "lowercase" });
const html = "<div foo=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "lowercase" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "lowercase" });
const html = "<div FooBar=\"\" foo-bar=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "lowercase" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});
});
describe("'dash' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "dash" });
const html = "<div foo-bar=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "dash" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "dash" });
const html = "<div FooBar=\"\" foo_bar=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "dash" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});
});

it("Should throw an error when an invalid config is passed", function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": ["camel"] });
const html = "<button style=\"color: red;\"></button>";
expect(() => linter.lint(html, none, { "attr-name-style": ["camel"] }))
expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"attr-name-style\" is invalid: Expected string or RegExp got object");
});

describe("'regexp' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": /^[0-9a-o]+$/ });
const html = "<div foo=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": /^[0-9a-o]+$/ });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": /^[0-9a-o]+$/ });
const html = "<div bar></div>";
const issues = await linter.lint(html, none, { "attr-name-style": /^[0-9a-o]+$/ });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});
});
describe("'camel' format", function() {
it("Should not report an error for attributes with valid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "camel" });
const html = "<div FooBar=\"\"></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "camel" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error for attributes with invalid format", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-name-style": "camel" });
const html = "<div foo-bar></div>";
const issues = await linter.lint(html, none, { "attr-name-style": "camel" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});
});
Expand Down
40 changes: 20 additions & 20 deletions lib/rules/attr-new-line/__tests__/index.js
Expand Up @@ -5,92 +5,92 @@ const none = require("../../../presets").presets.none;
// TODO check issues positions

describe("legacy linter | attr-new-line", function() {
function createLinter() {
return new linthtml.LegacyLinter(linthtml.rules);
function createLinter(config) {
return new linthtml.LegacyLinter(linthtml.rules, none, config);
}
it("Should not report errors if the number of atributes is less or equal to the configuration", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": 2 });
const html = `
<div class="foo" id="bar"></div>
<div class="foo"></div>
`;
const issues = await linter.lint(html, none, { "attr-new-line": 2 });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report errors if the number of attributes is superior to the rule's configuration", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": 1 });
const html = `
<div class="foo" id="bar"></div>
`;
const issues = await linter.lint(html, none, { "attr-new-line": 1 });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});

it("Should not report errors when attributes are on new lines", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": 1 });
const html = `
<div class="foo"
id="bar"
attr></div>
`;
const issues = await linter.lint(html, none, { "attr-new-line": 1 });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should accept less attributes per line than the value defined in the configuration", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": 2 });
const html = `
<div class="foo"
id="bar"
attr></div>
`;
const issues = await linter.lint(html, none, { "attr-new-line": 2 });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error when there's one attribute on the first line and configuration is '0'", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": 0 });
const html = "<div class=\"foo\"></div>";
const issues = await linter.lint(html, none, { "attr-new-line": 0 });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});

it("Should not report an error when there's one attribute on the first line and configuration is '+0'", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": "+0" });
const html = "<div class=\"foo\"></div>";
const issues = await linter.lint(html, none, { "attr-new-line": "+0" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(0);
});

it("Should report an error when there's more than one attribute on the first line and configuration is '+0'", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": "+0" });
const html = "<div class=\"foo\" id=\"bar\"></div>";
const issues = await linter.lint(html, none, { "attr-new-line": "+0" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(1);
});

it("Should report an error when there's attributes on line > 1 and configuration is '+0'", async function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": "+0" });
const html = `
<div
class="foo"
id>
</div>
`;
const issues = await linter.lint(html, none, { "attr-new-line": "+0" });
const issues = await linter.lint(html);
expect(issues).to.have.lengthOf(2);
});

it("Should throw an error when an invalid config is provided", function() {
const linter = createLinter();
const linter = createLinter({ "attr-new-line": "toto" });
const html = `
<div
class="foo"
id>
</div>
`;
expect(() => linter.lint(html, none, { "attr-new-line": "toto" }))
expect(() => linter.lint(html))
.to
.throw("Configuration for rule \"attr-new-line\" is invalid: Expected number or \"+0\" got string");
});
Expand Down

0 comments on commit 026503f

Please sign in to comment.