Repository: ember-cli/eslint-plugin-ember
Pull request #1069 (2021-01-11 17:19:50)

Improve tests for `jquery-ember-run` rule
Lots of tests were missing.


Patch:
diff --git a/lib/rules/jquery-ember-run.js b/lib/rules/jquery-ember-run.js
index e7118a52..a5035dde 100644
--- a/lib/rules/jquery-ember-run.js
+++ b/lib/rules/jquery-ember-run.js
@@ -20,6 +20,8 @@ const isRunUsed = function (node) {
   return ember.isModule(node, 'run');
 };
 
+const ERROR_MESSAGE = "Don't use jQuery without Ember Run Loop";
+
 module.exports = {
   meta: {
     type: 'problem',
@@ -34,11 +36,11 @@ module.exports = {
     schema: [],
   },
 
-  create(context) {
-    const message = "Don't use jQuery without Ember Run Loop";
+  ERROR_MESSAGE,
 
+  create(context) {
     const report = function (node) {
-      context.report(node, message);
+      context.report(node, ERROR_MESSAGE);
     };
 
     return {


Test patch:
diff --git a/tests/lib/rules/jquery-ember-run.js b/tests/lib/rules/jquery-ember-run.js
index fc75ba24..23ee48a1 100644
--- a/tests/lib/rules/jquery-ember-run.js
+++ b/tests/lib/rules/jquery-ember-run.js
@@ -9,24 +9,63 @@ const RuleTester = require('eslint').RuleTester;
 // Tests
 // ------------------------------------------------------------------------------
 
+const { ERROR_MESSAGE } = rule;
 const eslintTester = new RuleTester({
   parserOptions: { ecmaVersion: 6, sourceType: 'module' },
 });
 
 eslintTester.run('jquery-ember-run', rule, {
   valid: [
-    'Ember.$("#something-rendered-by-jquery-plugin").on("click", () => {Ember.run.bind(this, this._handlerActionFromController);});',
+    // jQuery/bind from Ember
+    `import Ember from "ember";
+     Ember.$("#item").on("click", () => { Ember.run.bind(this, this.handle); });`,
+
+    // jQuery from Ember with destructuring
+    `import Ember from "ember";
+     const { $ } = Ember;
+     $("#item").on("click", () => { Ember.run.bind(this, this.handle); });`,
+
+    // Global jQuery
+    `import { bind } from "@ember/runloop";
+     $("#item").on("click", () => { bind(this, this.handle); });`,
+
+    // Imported jQuery
+    `import { bind } from "@ember/runloop";
+     import $ from "jquery";
+     $("#item").on("click", () => { bind(this, this.handle); });`,
+
+    // No callback
+    '$("#item");',
+    '$("#item").on("click");',
+
+    // Callback but not jQuery
+    'notJquery("#item").on("click", () => {this.handle();});',
   ],
   invalid: [
     {
+      // jQuery from Ember
+      code: 'import Ember from "ember"; Ember.$("#item").on("click", () => { this.handle(); });',
+      output: null,
+      errors: [{ message: ERROR_MESSAGE, type: 'MemberExpression' }],
+    },
+    {
+      // jQuery from Ember with destructuring
       code:
-        'Ember.$("#something-rendered-by-jquery-plugin").on("click", () => {this._handlerActionFromController();});',
+        'import Ember from "ember"; const { $ } = Ember; $("#item").on("click", () => { this.handle(); });',
+      output: null,
+      errors: [{ message: ERROR_MESSAGE, type: 'MemberExpression' }],
+    },
+    {
+      // Global jQuery
+      code: '$("#item").on("click", () => {this.handle();});',
+      output: null,
+      errors: [{ message: ERROR_MESSAGE, type: 'MemberExpression' }],
+    },
+    {
+      // Imported jQuery
+      code: 'import $ from "jquery"; $("#item").on("click", () => { this.handle(); });',
       output: null,
-      errors: [
-        {
-          message: "Don't use jQuery without Ember Run Loop",
-        },
-      ],
+      errors: [{ message: ERROR_MESSAGE, type: 'MemberExpression' }],
     },
   ],
 });