diff --git a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs index 3869f62a03..87b65864a3 100644 --- a/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/text/viewport_texts_html.cljs @@ -97,12 +97,14 @@ (st/emit! (dwt/resize-text id width height))))) (st/emit! (dwt/clean-text-modifier id)))) - ;; Always clear the task and log measurement errors. - (p/catch (fn [cause] - (log/error :hint "Could not measure text shape" - :shape-id id - :cause cause) - nil)))) + ;; Log measurement errors and resolve to nil. The returned promise + ;; stays pending until the measurement settles, so callers keep the + ;; text mounted while it is measured. + (p/merr (fn [cause] + (log/error :hint "Could not measure text shape" + :shape-id id + :cause cause) + (p/resolved nil))))) (defn- update-text-modifier [{:keys [grow-type id] :as shape} node] diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 07f30d46ce..61de81d853 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -176,6 +176,7 @@ [frontend-tests.ui.subscription-test] [frontend-tests.ui.team-container-test] [frontend-tests.ui.text-attrs-multiple-test] + [frontend-tests.ui.viewport-texts-html-test] [frontend-tests.ui.viewport-top-bar-test] [frontend-tests.ui.workspace-libraries-test] [frontend-tests.util-clipboard-test] @@ -384,6 +385,7 @@ 'frontend-tests.ui.subscription-test 'frontend-tests.ui.team-container-test 'frontend-tests.ui.text-attrs-multiple-test + 'frontend-tests.ui.viewport-texts-html-test 'frontend-tests.ui.viewport-top-bar-test 'frontend-tests.ui.workspace-libraries-test 'frontend-tests.util-clipboard-test diff --git a/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs new file mode 100644 index 0000000000..d6369e5882 --- /dev/null +++ b/frontend/test/frontend_tests/ui/viewport_texts_html_test.cljs @@ -0,0 +1,71 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.ui.viewport-texts-html-test + "How the SVG renderer measures text shapes and commits the result." + (:require + [app.common.uuid :as uuid] + [app.main.data.workspace.texts :as dwt] + [app.main.store :as st] + [app.main.ui.workspace.shapes.text.viewport-texts-html :as vth] + [app.util.text-svg-position :as tsp] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] + [potok.v2.core :as ptk] + [promesa.core :as p])) + +(def ^:private fixed-text + {:id (uuid/next) :type :text :grow-type :fixed}) + +(defn- measure-text + "Measure `shape` as the SVG renderer does and return the measurement + promise. Kept out of the `^:async` test bodies: there, a call through the + var compiles to an awaited wrapper that would wait for the measurement." + [shape] + (#'vth/update-text-shape shape nil)) + +(defn- with-measurement + "Run `f` with the text measurement mocked to `measurement`, a promise the + test settles. Events emitted to the store are recorded in `emitted`." + [measurement emitted f] + (mock/with-mocks* + {tsp/calc-position-data (mock/stub (constantly measurement)) + st/emit! (mock/stub (fn [& events] (swap! emitted into events) nil))} + (await (f)))) + +(t/deftest ^:async measurement-promise-settles-with-the-measurement + (let [measurement (p/deferred) + emitted (atom []) + settled? (atom false)] + (await + (with-measurement + measurement + emitted + (^:async fn [] + (let [result (p/finally (measure-text fixed-text) + #(reset! settled? true))] + (await (async/settle)) + (t/is (false? @settled?) + "the promise stays pending while the text is measured") + (p/resolve measurement []) + (await result) + (t/is (true? @settled?) + "the promise settles once the measurement settles") + (t/is (some #(= ::dwt/clean-text-modifier (ptk/type %)) @emitted) + "the measurement completes and cleans the text modifier"))))))) + +(t/deftest ^:async failed-measurement-resolves-to-nil + (let [measurement (p/deferred)] + (await + (with-measurement + measurement + (atom []) + (^:async fn [] + (let [result (measure-text fixed-text)] + (p/reject measurement (ex-info "measure failed" {})) + (t/is (nil? (await result)) + "a failed measurement resolves to nil")))))))