DEV: uses find() helper instead of this.$() in js tests (#7062)

DEV: uses find() helper instead of this.$() in js tests (#7062)

diff --git a/Dangerfile b/Dangerfile
index 2f6e4c3..d1c3326 100644
--- a/Dangerfile
+++ b/Dangerfile
@@ -22,7 +22,10 @@ files = (git.added_files + git.modified_files)
   .select { |path| !path.start_with?("plugins/") }
   .select { |path| path.end_with?("es6") || path.end_with?("rb") }
 
+js_test_files = files.select { |path| path.end_with?("-test.js.es6") }
+
 super_offenses = []
+jquery_find_offenses = []
 
 files.each do |path|
   diff = git.diff_for_file(path)
@@ -34,9 +37,26 @@ files.each do |path|
   end
 end
 
+js_test_files.each do |path|
+  diff = git.diff_for_file(path)
+
+  next if !diff
+
+  diff.patch.lines.grep(/^\+\s\s/).each do |added_line|
+    jquery_find_offenses << path if added_line['this.$(']
+  end
+end
+
 if !super_offenses.empty?
   warn(%{
 When possible use `this._super(...arguments)` instead of `this._super()`\n
 #{super_offenses.uniq.map { |o| github.html_link(o) }.join("\n")}
   })
 end
+
+if !jquery_find_offenses.empty?
+  warn(%{
+Use `find()` instead of `this.$` in js tests`\n
+#{jquery_find_offenses.uniq.map { |o| github.html_link(o) }.join("\n")}
+  })
+end
diff --git a/app/assets/javascripts/wizard/test/components/invite-list-test.js.es6 b/app/assets/javascripts/wizard/test/components/invite-list-test.js.es6
index 95436fb..a6fa548 100644
--- a/app/assets/javascripts/wizard/test/components/invite-list-test.js.es6
+++ b/app/assets/javascripts/wizard/test/components/invite-list-test.js.es6
@@ -10,13 +10,10 @@ componentTest("can add users", {
 
   async test(assert) {
     assert.ok(
-      this.$(".users-list .invite-list-user").length === 0,
+      find(".users-list .invite-list-user").length === 0,
       "no users at first"
     );
-    assert.ok(
-      this.$(".new-user .invalid").length === 0,
-      "not invalid at first"
-    );
+    assert.ok(find(".new-user .invalid").length === 0, "not invalid at first");
 
     const firstVal = JSON.parse(this.get("field.value"));
     assert.equal(firstVal.length, 0, "empty JSON at first");
@@ -28,19 +25,19 @@ componentTest("can add users", {
 
     await click(".add-user");
     assert.ok(
-      this.$(".users-list .invite-list-user").length === 0,
+      find(".users-list .invite-list-user").length === 0,
       "doesn't add a blank user"
     );
-    assert.ok(this.$(".new-user .invalid").length === 1);
+    assert.ok(find(".new-user .invalid").length === 1);
 
     await fillIn(".invite-email", "eviltrout@example.com");
     await click(".add-user");
 
     assert.ok(
-      this.$(".users-list .invite-list-user").length === 1,
+      find(".users-list .invite-list-user").length === 1,
       "adds the user"
     );
-    assert.ok(this.$(".new-user .invalid").length === 0);
+    assert.ok(find(".new-user .invalid").length === 0);
 
     const val = JSON.parse(this.get("field.value"));
     assert.equal(val.length, 1);
@@ -56,23 +53,23 @@ componentTest("can add users", {
     await click(".add-user");
 
     assert.ok(
-      this.$(".users-list .invite-list-user").length === 1,
+      find(".users-list .invite-list-user").length === 1,
       "can't add the same user twice"
     );
-    assert.ok(this.$(".new-user .invalid").length === 1);
+    assert.ok(find(".new-user .invalid").length === 1);
 
     await fillIn(".invite-email", "not-an-email");
     await click(".add-user");
 
     assert.ok(
-      this.$(".users-list .invite-list-user").length === 1,
+      find(".users-list .invite-list-user").length === 1,
       "won't add an invalid email"
     );
-    assert.ok(this.$(".new-user .invalid").length === 1);
+    assert.ok(find(".new-user .invalid").length === 1);
 
     await click(".invite-list .invite-list-user:eq(0) .remove-user");
     assert.ok(
-      this.$(".users-list .invite-list-user").length === 0,
+      find(".users-list .invite-list-user").length === 0,
       "removed the user"
     );
   }
diff --git a/plugins/poll/test/javascripts/widgets/discourse-poll-standard-results-test.js.es6 b/plugins/poll/test/javascripts/widgets/discourse-poll-standard-results-test.js.es6
index bfcd7be..33ee000 100644
--- a/plugins/poll/test/javascripts/widgets/discourse-poll-standard-results-test.js.es6
+++ b/plugins/poll/test/javascripts/widgets/discourse-poll-standard-results-test.js.es6
@@ -19,8 +19,8 @@ widgetTest("options in descending order", {
   },
 
   test(assert) {
-    assert.equal(this.$(".option .percentage:eq(0)").text(), "56%");
-    assert.equal(this.$(".option .percentage:eq(1)").text(), "44%");
+    assert.equal(find(".option .percentage:eq(0)").text(), "56%");
+    assert.equal(find(".option .percentage:eq(1)").text(), "44%");
   }
 });
 
@@ -38,8 +38,8 @@ widgetTest("options in ascending order", {
   },
 
   test(assert) {
-    assert.equal(this.$(".option .percentage:eq(0)").text(), "56%");
-    assert.equal(this.$(".option .percentage:eq(1)").text(), "44%");
+    assert.equal(find(".option .percentage:eq(0)").text(), "56%");
+    assert.equal(find(".option .percentage:eq(1)").text(), "44%");
   }
 });
 
@@ -65,12 +65,12 @@ widgetTest("multiple options in descending order", {
   },
 
   test(assert) {
-    assert.equal(this.$(".option .percentage:eq(0)").text(), "41%");
-    assert.equal(this.$(".option .percentage:eq(1)").text(), "33%");
-    assert.equal(this.$(".option .percentage:eq(2)").text(), "16%");
-    assert.equal(this.$(".option .percentage:eq(3)").text(), "8%");
-    assert.equal(this.$(".option span:nth-child(2):eq(3)").text(), "a");
-    assert.equal(this.$(".option .percentage:eq(4)").text(), "8%");
-    assert.equal(this.$(".option span:nth-child(2):eq(4)").text(), "b");
+    assert.equal(find(".option .percentage:eq(0)").text(), "41%");
+    assert.equal(find(".option .percentage:eq(1)").text(), "33%");
+    assert.equal(find(".option .percentage:eq(2)").text(), "16%");
+    assert.equal(find(".option .percentage:eq(3)").text(), "8%");
+    assert.equal(find(".option span:nth-child(2):eq(3)").text(), "a");
+    assert.equal(find(".option .percentage:eq(4)").text(), "8%");
+    assert.equal(find(".option span:nth-child(2):eq(4)").text(), "b");
   }
 });
diff --git a/test/javascripts/admin/components/themes-list-item-test.js.es6 b/test/javascripts/admin/components/themes-list-item-test.js.es6
index 82190fb..d7506dc 100644
--- a/test/javascripts/admin/components/themes-list-item-test.js.es6
+++ b/test/javascripts/admin/components/themes-list-item-test.js.es6
@@ -11,7 +11,7 @@ componentTest("default theme", {
 
   test(assert) {
     assert.expect(1);
-    assert.equal(this.$(".d-icon-check").length, 1, "shows default theme icon");
+    assert.equal(find(".d-icon-check").length, 1, "shows default theme icon");
   }
 });
 
@@ -26,7 +26,7 @@ componentTest("pending updates", {
 
   test(assert) {
     assert.expect(1);
-    assert.equal(this.$(".d-icon-sync").length, 1, "shows pending update icon");
+    assert.equal(find(".d-icon-sync").length, 1, "shows pending update icon");
   }
 });
 
@@ -45,7 +45,7 @@ componentTest("broken theme", {
   test(assert) {
     assert.expect(1);
     assert.equal(
-      this.$(".d-icon-exclamation-circle").length,
+      find(".d-icon-exclamation-circle").length,
       1,
       "shows broken theme icon"
     );
diff --git a/test/javascripts/admin/components/themes-list-test.js.es6 b/test/javascripts/admin/components/themes-list-test.js.es6
index 3842e8d..4eaba9b 100644
--- a/test/javascripts/admin/components/themes-list-test.js.es6
+++ b/test/javascripts/admin/components/themes-list-test.js.es6
@@ -28,36 +28,36 @@ componentTest("current tab is themes", {
 
   test(assert) {
     assert.equal(
-      this.$(".themes-tab").hasClass("active"),
+      find(".themes-tab").hasClass("active"),
       true,
       "themes tab is active"
     );
     assert.equal(
-      this.$(".components-tab").hasClass("active"),
+      find(".components-tab").hasClass("active"),
       false,
       "components tab is not active"
     );
 
     assert.equal(
-      this.$(".inactive-indicator").index(),
+      find(".inactive-indicator").index(),
       -1,

[... diff too long, it was truncated ...]

GitHub sha: 5952a6c0

2 Likes