Skip to content

Commit

Permalink
test: improve tests for jquery-ember-run rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Jan 11, 2021
1 parent 4db2732 commit 44363ab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/rules/jquery-ember-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 {
Expand Down
53 changes: 46 additions & 7 deletions tests/lib/rules/jquery-ember-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
},
],
});

0 comments on commit 44363ab

Please sign in to comment.