tslint-clean-code
Version:
TSLint rules for enforcing Clean Code
109 lines • 6.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var TestHelper_1 = require("./TestHelper");
var noMapWithoutUsageRule_1 = require("../noMapWithoutUsageRule");
var FAILURE_STRING = noMapWithoutUsageRule_1.Rule.FAILURE_STRING;
describe('noMapWithoutUsageRule', function () {
var ruleName = 'no-map-without-usage';
it('should pass on Array.forEach', function () {
var script = "\n arr.forEach(item => doStuff(item));\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with assignment', function () {
var script = "\n const results = arr.map(item => doStuff(item));\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with assignment to an existing variable', function () {
var script = "\n let results;\n results = arr.map(item => doStuff(item));\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with assignment to object property', function () {
var script = "\n const obj = {\n key: arr.map(item => doStuff(item))\n };\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with additional access on result', function () {
var script = "\n arr.map(item => doStuff(item))\n .forEach();\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with results returned', function () {
var script = "\n function () {\n return arr.map(item => doStuff(item));\n }\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with results used in parent call', function () {
var script = "\n doStuff(arr.map(item => doStuff(item)))\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with spread', function () {
var script = "\n doOtherStuff(...arr.map(item => doStuff(item)))\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map within true case of ternary/conditional expression', function () {
var script = "\n b ? arr.map(curr => curr * 2) : [];\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map within false case of ternary/conditional expression', function () {
var script = "\n b ? [] : arr.map(curr => curr * 2);\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map as array value', function () {
var script = "\n const arr2 = [1, arr.map(curr => curr * 2), 3];\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map within Binary Expression after &&', function () {
var script = "\n arr && arr.map(curr => curr * 2);\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map within Binary Expression after ||', function () {
var script = "\n b && firstThing() || arr.map(curr => curr * 2);\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map within arrow function return shorthand', function () {
var script = "\n this.getFiles()\n .then(files => files.map(file => path.join(this.basePath, file)))\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map within arrow function return shorthand inside a constructor', function () {
var script = "\n new Map(\n [{ id: 0, name: 'John' }]\n .map<[number, string]>(entity => [entity.id, entity.name])\n );\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should pass on Array.prototype.map with assignment to a property initializer', function () {
var script = "\n class Example {\n items = [1, 2].map(item => doStuff(item));\n }";
TestHelper_1.TestHelper.assertViolations(ruleName, script, []);
});
it('should fail on Array.prototype.map within arrow function block without return statement', function () {
var script = "\n this.getFiles()\n .then(files => {\n files.map(file => path.join(this.basePath, file));\n })\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, [
{
failure: FAILURE_STRING,
name: 'file.ts',
ruleName: ruleName,
ruleSeverity: 'ERROR',
startPosition: {
character: 17,
line: 4,
},
},
]);
});
it('should fail on Array.prototype.map without assignment', function () {
var script = "\n arr.map(item => doStuff(item));\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, [
{
failure: FAILURE_STRING,
name: 'file.ts',
ruleName: ruleName,
ruleSeverity: 'ERROR',
startPosition: {
character: 9,
line: 2,
},
},
]);
});
context('React', function () {
it('should pass on JavaScript Expression within TSX', function () {
var file = 'test-data/NoMapWithoutUsage/JavaScriptExpressionInReact.tsx';
TestHelper_1.TestHelper.assertViolations(ruleName, file, []);
});
});
});
//# sourceMappingURL=NoMapWithoutUsageRuleTests.js.map
;