DEV: adds test for image-short-url (#6642)

DEV: adds test for image-short-url (#6642)

From 58c795ef3012389230472f5672d51e3773f7da37 Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Wed, 21 Nov 2018 21:48:52 +0100
Subject: [PATCH] DEV: adds test for image-short-url (#6642)


diff --git a/app/assets/javascripts/pretty-text/image-short-url.js.es6 b/app/assets/javascripts/pretty-text/image-short-url.js.es6
index d2e573b..e457210 100644
--- a/app/assets/javascripts/pretty-text/image-short-url.js.es6
+++ b/app/assets/javascripts/pretty-text/image-short-url.js.es6
@@ -9,8 +9,12 @@ export function lookupUncachedUploadUrls(urls, ajax) {
     method: "POST",
     data: { short_urls: urls }
   }).then(uploads => {
-    uploads.forEach(upload => (_cache[upload.short_url] = upload.url));
-    urls.forEach(url => (_cache[url] = _cache[url] || "missing"));
+    uploads.forEach(upload =>
+      cacheShortUploadUrl(upload.short_url, upload.url)
+    );
+    urls.forEach(url =>
+      cacheShortUploadUrl(url, lookupCachedUploadUrl(url) || "missing")
+    );
     return uploads;
   });
 }
@@ -19,10 +23,15 @@ export function cacheShortUploadUrl(shortUrl, url) {
   _cache[shortUrl] = url;
 }
 
+export function resetCache() {
+  _cache = {};
+}
+
 function _loadCachedShortUrls($images) {
   $images.each((idx, image) => {
-    let $image = $(image);
-    let url = lookupCachedUploadUrl($image.data("orig-src"));
+    const $image = $(image);
+    const url = lookupCachedUploadUrl($image.data("orig-src"));
+
     if (url) {
       $image.removeAttr("data-orig-src");
       if (url !== "missing") {
@@ -34,7 +43,7 @@ function _loadCachedShortUrls($images) {
 
 function _loadShortUrls($images, ajax) {
   const urls = $images.toArray().map(img => $(img).data("orig-src"));
-  lookupUncachedUploadUrls(urls, ajax).then(() =>
+  return lookupUncachedUploadUrls(urls, ajax).then(() =>
     _loadCachedShortUrls($images)
   );
 }
@@ -48,11 +57,9 @@ export function resolveAllShortUrls(ajax) {
     $shortUploadUrls = $("img[data-orig-src]");
     if ($shortUploadUrls.length > 0) {
       // this is carefully batched so we can do a leading debounce (trigger right away)
-      Ember.run.debounce(
+      return Ember.run.debounce(
         null,
-        () => {
-          _loadShortUrls($shortUploadUrls, ajax);
-        },
+        () => _loadShortUrls($shortUploadUrls, ajax),
         450,
         true
       );
diff --git a/test/javascripts/lib/image-short-url-test.js.es6 b/test/javascripts/lib/image-short-url-test.js.es6
new file mode 100644
index 0000000..d7b2c53
--- /dev/null
+++ b/test/javascripts/lib/image-short-url-test.js.es6
@@ -0,0 +1,56 @@
+import {
+  lookupCachedUploadUrl,
+  resolveAllShortUrls,
+  resetCache
+} from "pretty-text/image-short-url";
+import { ajax } from "discourse/lib/ajax";
+
+QUnit.module("lib:pretty-text/image-short-url", {
+  beforeEach() {
+    const response = object => {
+      return [200, { "Content-Type": "application/json" }, object];
+    };
+
+    const srcs = [
+      {
+        short_url: "upload://a.jpeg",
+        url: "/uploads/default/original/3X/c/b/1.jpeg"
+      },
+      {
+        short_url: "upload://b.jpeg",
+        url: "/uploads/default/original/3X/c/b/2.jpeg"
+      }
+    ];
+
+    // prettier-ignore
+    server.post("/uploads/lookup-urls", () => { //eslint-disable-line
+      return response(srcs);
+    });
+
+    fixture().html(
+      srcs.map(src => `<img data-orig-src="${src.url}">`).join("")
+    );
+  },
+
+  afterEach() {
+    resetCache();
+  }
+});
+
+QUnit.test("resolveAllShortUrls", async assert => {
+  let lookup;
+
+  lookup = lookupCachedUploadUrl("upload://a.jpeg");
+  assert.notOk(lookup);
+
+  await resolveAllShortUrls(ajax);
+
+  lookup = lookupCachedUploadUrl("upload://a.jpeg");
+  assert.equal(lookup, "/uploads/default/original/3X/c/b/1.jpeg");
+
+  lookup = lookupCachedUploadUrl("upload://b.jpeg");
+  assert.equal(lookup, "/uploads/default/original/3X/c/b/2.jpeg");
+
+  lookup = lookupCachedUploadUrl("upload://c.jpeg");
+  assert.notOk(lookup);
+});

GitHub