DEV: uses focus helper for d-editor tests (#7610)

DEV: uses focus helper for d-editor tests (#7610)

diff --git a/.eslintrc b/.eslintrc
index 96a41ba..75fcbf0 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -20,6 +20,8 @@
     "Blob": true,
     "bootbox": true,
     "click": true,
+    "waitUntil": true,
+    "getSettledState": true,
     "collapseSelectKit": true,
     "controllerFor": true,
     "count": true,
@@ -58,6 +60,7 @@
     "selectKitSelectRowByName": true,
     "selectKitSelectRowByValue": true,
     "setTextareaSelection": true,
+    "getTextareaSelection": true,
     "sinon": true,
     "test": true,
     "triggerEvent": true,
diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6
index 196942e..2e30539 100644
--- a/app/assets/javascripts/discourse/components/d-editor.js.es6
+++ b/app/assets/javascripts/discourse/components/d-editor.js.es6
@@ -544,7 +544,8 @@ export default Ember.Component.extend({
         $textarea.focus();
       }
       textarea.selectionStart = from;
-      textarea.selectionEnd = textarea.selectionStart + length;
+      textarea.selectionEnd = from + length;
+
       $textarea.scrollTop(oldScrollPos);
     });
   },
diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6
index 2da16a6..612d67d 100644
--- a/test/javascripts/components/d-editor-test.js.es6
+++ b/test/javascripts/components/d-editor-test.js.es6
@@ -777,12 +777,6 @@ composerTestCase("replace-text event for composer", async function(assert) {
     }
   ];
 
-  function getSelection(textarea) {
-    const start = textarea.selectionStart;
-    const end = textarea.selectionEnd;
-    return [start, end - start];
-  }
-
   for (let i = 0; i < CASES.length; i++) {
     const CASE = CASES[i];
     // prettier-ignore
@@ -792,26 +786,21 @@ composerTestCase("replace-text event for composer", async function(assert) {
     ) {
       this.set("value", BEFORE);
 
-      await click(textarea);
+      await focus(textarea);
 
-      assert.ok(document.activeElement === textarea);
       assert.ok(textarea.value === BEFORE);
 
       const [start, len] = CASE.before;
       setTextareaSelection(textarea, start, start + len);
 
-      assert.ok(document.activeElement === textarea);
-
       this.container
         .lookup("app-events:main")
         .trigger("composer:replace-text", "green", "yellow", { forceFocus: true });
 
-      assert.ok(document.activeElement === textarea);
-
       let expect = await formatTextWithSelection(AFTER, CASE.after); // eslint-disable-line no-undef
       let actual = await formatTextWithSelection( // eslint-disable-line no-undef
         this.value,
-        getSelection(textarea)
+        getTextareaSelection(textarea)
       );
       assert.equal(actual, expect);
     });
diff --git a/test/javascripts/helpers/set-textarea-selection.js b/test/javascripts/helpers/set-textarea-selection.js
deleted file mode 100644
index b6f219e..0000000
--- a/test/javascripts/helpers/set-textarea-selection.js
+++ /dev/null
@@ -1,9 +0,0 @@
-Ember.Test.registerHelper("setTextareaSelection", function(
-  app,
-  textarea,
-  selectionStart,
-  selectionEnd
-) {
-  textarea.selectionStart = selectionStart;
-  textarea.selectionEnd = selectionEnd;
-});
diff --git a/test/javascripts/helpers/textarea-selection.js b/test/javascripts/helpers/textarea-selection.js
new file mode 100644
index 0000000..c406928
--- /dev/null
+++ b/test/javascripts/helpers/textarea-selection.js
@@ -0,0 +1,15 @@
+Ember.Test.registerHelper("setTextareaSelection", function(
+  app,
+  textarea,
+  selectionStart,
+  selectionEnd
+) {
+  textarea.selectionStart = selectionStart;
+  textarea.selectionEnd = selectionEnd;
+});
+
+Ember.Test.registerHelper("getTextareaSelection", function(app, textarea) {
+  var start = textarea.selectionStart;
+  var end = textarea.selectionEnd;
+  return [start, end - start];
+});
diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js
index bf920e7..d94a88f 100644
--- a/test/javascripts/test_helper.js
+++ b/test/javascripts/test_helper.js
@@ -30,7 +30,7 @@
 //= require sinon/pkg/sinon
 
 //= require helpers/assertions
-//= require helpers/set-textarea-selection
+//= require helpers/textarea-selection
 //= require helpers/select-kit-helper
 //= require helpers/d-editor-helper

GitHub sha: 19256274

1 Like