Skip to content

Commit

Permalink
Build: Improve Test Coverage of no-assign-require rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Casey Visco committed Aug 14, 2016
1 parent 17cc698 commit dda9314
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
36 changes: 0 additions & 36 deletions tests/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,32 +691,6 @@ require(['path/to/a.js'], function (a) {
});
`;

exports.ASSIGN_TO_FOO_REQUIRE = `
foo.require = {
bar: 'bar'
};
`;

exports.ASSIGN_TO_REQUIRE = `
require = {
deps: ['path/to/a', 'path/to/b'],
callback: function (a, b) {
a.foo();
b.bar();
}
};
`;

exports.ASSIGN_TO_WINDOW_REQUIRE = `
window.require = {
deps: ['path/to/a', 'path/to/b'],
callback: function (a, b) {
a.foo();
b.bar();
}
};
`;

exports.BAD_DEFINE = `
define('foo', 'bar', false);
`;
Expand Down Expand Up @@ -960,16 +934,6 @@ define(function (require) {
});
`;

exports.DECLARE_REQUIRE = `
var require = {
deps: ['path/to/a', 'path/to/b'],
callback: function (a, b) {
a.foo();
b.bar();
}
};
`;

exports.DYNAMIC_AMD_REQUIREJS_WITH_ERRBACK = `
requirejs(
[someCondition ? 'path/to/a' : 'path/to/b'],
Expand Down
75 changes: 69 additions & 6 deletions tests/lib/rules/no-assign-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,84 @@
"use strict";

const testRule = require("../../rule-tester");
const fixtures = require("../../fixtures");
const rule = require("../../../lib/rules/no-assign-require");

const MESSAGE = "Invalid assignment to `require`.";
// -----------------------------------------------------------------------------
// Fixtures
// -----------------------------------------------------------------------------

const OK_PROPERTY_ASSIGNMENT = `
foo.require = {
bar: 'bar'
};
`;

const OK_SIMILAR_VARIABLE = `
var required = true;
`;

const BAD_DECLARE_REQUIRE = `
var require = {
deps: ['path/to/a', 'path/to/b'],
callback: function (a, b) {
a.foo();
b.bar();
}
};
`;

const BAD_ASSIGN_TO_REQUIRE = `
require = {
deps: ['path/to/a', 'path/to/b'],
callback: function (a, b) {
a.foo();
b.bar();
}
};
`;

const BAD_ASSIGN_TO_WINDOW_REQUIRE = `
window.require = {
deps: ['path/to/a', 'path/to/b'],
callback: function (a, b) {
a.foo();
b.bar();
}
};
`;

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

const ERROR_MSG = "Invalid assignment to `require`.";

testRule("no-assign-require", rule, {

valid: [
fixtures.ASSIGN_TO_FOO_REQUIRE
OK_PROPERTY_ASSIGNMENT,
OK_SIMILAR_VARIABLE
],

invalid: [
{ code: fixtures.DECLARE_REQUIRE, errors: [{ message: MESSAGE, type: "VariableDeclarator" }] },
{ code: fixtures.ASSIGN_TO_REQUIRE, errors: [{ message: MESSAGE, type: "AssignmentExpression" }] },
{ code: fixtures.ASSIGN_TO_WINDOW_REQUIRE, errors: [{ message: MESSAGE, type: "AssignmentExpression" }] }
{
code: BAD_DECLARE_REQUIRE,
errors: [
{ message: ERROR_MSG, type: "VariableDeclarator" }
]
},
{
code: BAD_ASSIGN_TO_REQUIRE,
errors: [
{ message: ERROR_MSG, type: "AssignmentExpression" }
]
},
{
code: BAD_ASSIGN_TO_WINDOW_REQUIRE,
errors: [
{ message: ERROR_MSG, type: "AssignmentExpression" }
]
}
]

});

0 comments on commit dda9314

Please sign in to comment.