DEV: Improve test suite.

DEV: Improve test suite.

diff --git a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6 b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
index a70ffc1..1de2e88 100644
--- a/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/composer-action-after/encrypt.js.es6
@@ -1,10 +1,16 @@
 import { getOwner } from "discourse-common/lib/get-owner";
+import { registerHelper } from "discourse-common/lib/helpers";
 import {
   ENCRYPT_ACTIVE,
   ENCRYPT_DISABLED,
   getEncryptionStatus
 } from "discourse/plugins/discourse-encrypt/lib/discourse";
 
+// TODO: I believe this should get into core.
+// Handlebars offers `if` but no other helpers for conditions, which eventually
+// lead to a lot of JavaScript bloat.
+registerHelper("or", ([a, b]) => a || b);
+
 export default {
   setupComponent(args, component) {
     const currentUser = getOwner(component).lookup("current-user:main");
@@ -26,12 +32,12 @@ export default {
 
       didInsertElement() {
         this._super(...arguments);
-        this.appEvents.on("encrypt:status-changed", this.listener);
+        this.appEvents.on("encrypt:status-changed", this, this.listener);
       },
 
       willDestroyElement() {
         this._super(...arguments);
-        this.appEvents.off("encrypt:status-changed", this.listener);
+        this.appEvents.off("encrypt:status-changed", this, this.listener);
       },
 
       clicked() {
diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
index ff0337b..33bb69c 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
@@ -31,7 +31,7 @@
             {{{i18n 'encrypt.preferences.notice_import'}}}
             {{textarea class="imported-keypair" value=identity}}
           {{/if}}
-          {{d-button class="btn-primary" icon="lock" action=(action "enableEncrypt") disabled=(or passphraseStatus inProgress) label="encrypt.preferences.enable"}}
+          {{d-button class="btn-primary" icon="lock" action=(action "enableEncrypt") disabled=inProgress label="encrypt.preferences.enable"}}
           {{#if importIdentity}}
             <span><a {{action (mut importIdentity) false}}>{{i18n 'cancel'}}</a></span>
           {{else}}
diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
index 52f181d..85b65ba 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -1,4 +1,3 @@
-import { registerHelper } from "discourse-common/lib/helpers";
 import { ajax } from "discourse/lib/ajax";
 import showModal from "discourse/lib/show-modal";
 import {
@@ -20,11 +19,6 @@ import {
   importIdentity
 } from "discourse/plugins/discourse-encrypt/lib/protocol";
 
-// TODO: I believe this should get into core.
-// Handlebars offers `if` but no other helpers for conditions, which eventually
-// lead to a lot of JavaScript bloat.
-registerHelper("or", ([a, b]) => a || b);
-
 export default {
   setupComponent(args, component) {
     const currentUser = Discourse.User.current();
@@ -62,11 +56,11 @@ export default {
         },
         didInsertElement() {
           this._super(...arguments);
-          this.appEvents.on("encrypt:status-changed", this.listener);
+          this.appEvents.on("encrypt:status-changed", this, this.listener);
         },
         willDestroyElement() {
           this._super(...arguments);
-          this.appEvents.off("encrypt:status-changed", this.listener);
+          this.appEvents.off("encrypt:status-changed", this, this.listener);
         }
       });
     } else {
diff --git a/assets/javascripts/discourse/initializers/hook-composer.js.es6 b/assets/javascripts/discourse/initializers/hook-composer.js.es6
index 19f4ecb..ba6277a 100644
--- a/assets/javascripts/discourse/initializers/hook-composer.js.es6
+++ b/assets/javascripts/discourse/initializers/hook-composer.js.es6
@@ -99,58 +99,56 @@ export default {
     // Decode composer on reply reload. This usually occurs when a post is
     // edited or a draft is loaded.
     const appEvents = container.lookup("app-events:main");
-    appEvents.on("composer:reply-reloaded", model => {
-      const draftKey = model.draftKey;
-
-      let encrypted, decTitle, decReply;
-      if (draftKey === Composer.NEW_PRIVATE_MESSAGE_KEY) {
-        encrypted = true;
-      } else if (draftKey.indexOf("topic_") === 0) {
-        const topicId = draftKey.substr("topic_".length);
-        encrypted = !!hasTopicKey(topicId);
-      }
+    appEvents.on("composer:reply-reloaded", this, "composerReplyReloaded");
+  },
+
+  composerReplyReloaded(model) {
+    const draftKey = model.draftKey;
+
+    let encrypted, decTitle, decReply;
+    if (draftKey === Composer.NEW_PRIVATE_MESSAGE_KEY) {
+      encrypted = true;
+    } else if (draftKey.indexOf("topic_") === 0) {
+      const topicId = draftKey.substr("topic_".length);
+      encrypted = !!hasTopicKey(topicId);
+    }
 
-      if (encrypted) {
-        if (model.action === "edit" && model.originalText) {
-          const topicId = model.get("topic.id");
-          decTitle = getTopicTitle(topicId);
-          decReply = getTopicKey(topicId)
-            .then(key => decrypt(key, model.reply))
-            .then(decrypted => decrypted.raw);
-        } else {
-          const pos = model.reply ? model.reply.indexOf("\n") : -1;
-          if (pos !== -1) {
-            const topicKey = model.reply.substr(0, pos).trim();
-            const reply = model.reply.substr(pos + 1).trim();
-
-            const decKey = getIdentity().then(identity =>
-              importKey(topicKey, identity.encryptPrivate)
-            );
-
-            decTitle = model.title
-              ? decKey.then(key => decrypt(key, model.title))
-              : "";
-
-            decReply = reply
-              ? decKey
-                  .then(key => decrypt(key, reply))
-                  .then(decrypted => decrypted.raw)
-              : "";
-          }
+    if (encrypted) {
+      if (model.action === "edit" && model.originalText) {
+        const topicId = model.get("topic.id");
+        decTitle = getTopicTitle(topicId);
+        decReply = getTopicKey(topicId)
+          .then(key => decrypt(key, model.reply))
+          .then(decrypted => decrypted.raw);
+      } else {
+        const pos = model.reply ? model.reply.indexOf("\n") : -1;
+        if (pos !== -1) {
+          const topicKey = model.reply.substr(0, pos).trim();
+          const reply = model.reply.substr(pos + 1).trim();
+
+          const decKey = getIdentity().then(identity =>
+            importKey(topicKey, identity.encryptPrivate)
+          );
+
+          decTitle = model.title
+            ? decKey.then(key => decrypt(key, model.title))
+            : "";
+
+          decReply = reply
+            ? decKey
+                .then(key => decrypt(key, reply))
+                .then(decrypted => decrypted.raw)
+            : "";
         }
       }
+    }
 
-      if (decTitle) {
-        decTitle.then(title =>
-          model.setProperties({ title, isEncrypted: true })
-        );
-      }
+    if (decTitle) {
+      decTitle.then(title => model.setProperties({ title, isEncrypted: true }));
+    }
 
-      if (decReply) {
-        decReply.then(reply =>
-          model.setProperties({ reply, isEncrypted: true })
-        );
-      }
-    });
+    if (decReply) {
+      decReply.then(reply => model.setProperties({ reply, isEncrypted: true }));
+    }
   }
 };

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

GitHub sha: 3a0958eb

It could not hurt! It’s similar to:

1 Like

I’ve not reviewed this in a while, but sleep is a bad sign in tests. It means we have not enclosed something in a promise properly. It should be a goal to remove these.

1 Like

I went over and over this and could never figure out if there is a promise that is not enclosed properly.

However, I got rid of sleep by using the waiters and followed up in:

DEV: Use waiters instead of sleep. · discourse/discourse-encrypt@326f4b7 · GitHub.

2 Likes