DEV: Teardown leaky page:changed `appEvent`s between tests

DEV: Teardown leaky page:changed appEvents between tests

diff --git a/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js b/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js
index 66ab52a..1d8eba4 100644
--- a/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js
+++ b/app/assets/javascripts/discourse/app/initializers/opengraph-tag-updater.js
@@ -6,15 +6,21 @@ export default {
   initialize(container) {
     // workaround for Safari on iOS 14.3
     // seems it has started using opengraph tags when sharing
-    let appEvents = container.lookup("service:app-events");
-    const ogTitle = document.querySelector("meta[property='og:title']"),
-      ogUrl = document.querySelector("meta[property='og:url']");
+    this.appEvents = container.lookup("service:app-events");
+    this.ogTitle = document.querySelector("meta[property='og:title']");
+    this.ogUrl = document.querySelector("meta[property='og:url']");
 
-    if (ogTitle && ogUrl) {
-      appEvents.on("page:changed", (data) => {
-        ogTitle.setAttribute("content", data.title);
-        ogUrl.setAttribute("content", getAbsoluteURL(data.url));
-      });
+    if (this.ogTitle && this.ogUrl) {
+      this.appEvents.on("page:changed", this, this.updateOgAttributes);
     }
   },
+
+  updateOgAttributes(data) {
+    this.ogTitle.setAttribute("content", data.title);
+    this.ogUrl.setAttribute("content", getAbsoluteURL(data.url));
+  },
+
+  teardown() {
+    this.appEvents.off("page:changed", this, this.updateOgAttributes);
+  },
 };

GitHub sha: 0bec323204ab8c2d7dd38c0469c7c39da72d0ab8

This commit appears in #14745 which was approved by CvX and jjaffeux. It was merged by davidtaylorhq.