From b66ec9cd9fba1e831760bd781fdc461dff218d2a Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Wed, 3 Aug 2022 14:49:37 -0400 Subject: [PATCH] Add tests to confirm property completion fix Move current-line to specific clause for performance as fn is run on every keystroke --- src/main/frontend/handler/editor.cljs | 8 +++--- src/test/frontend/handler/editor_test.cljs | 32 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 776f418e2..9192babaf 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1827,8 +1827,7 @@ pos (cursor/pos input) last-input-char (util/nth-safe (.-value input) (dec pos)) last-prev-input-char (util/nth-safe (.-value input) (dec (dec pos))) - prev-prev-input-char (util/nth-safe (.-value input) (- pos 3)) - current-line (text-util/get-current-line-by-pos (.-value input) (dec pos))] + prev-prev-input-char (util/nth-safe (.-value input) (- pos 3))] ;; TODO: is it cross-browser compatible? ;; (not= (gobj/get native-e "inputType") "insertFromPaste") @@ -1855,8 +1854,9 @@ (and (not= :property-search (state/get-editor-action)) - (or (text-util/wrapped-by? current-line (dec pos) "" "::") - (text-util/wrapped-by? current-line (dec pos) "\n" "::"))) + (let [current-line (text-util/get-current-line-by-pos (.-value input) (dec pos))] + (or (text-util/wrapped-by? current-line (dec pos) "" "::") + (text-util/wrapped-by? current-line (dec pos) "\n" "::")))) (do (state/set-editor-action-data! {:pos (cursor/get-caret-pos input)}) (state/set-editor-action! :property-search)) diff --git a/src/test/frontend/handler/editor_test.cljs b/src/test/frontend/handler/editor_test.cljs index c39ac187d..6e06574c8 100644 --- a/src/test/frontend/handler/editor_test.cljs +++ b/src/test/frontend/handler/editor_test.cljs @@ -116,3 +116,35 @@ "TODO" "TODO content" "DOING content" "TODO" "## TODO content" "## DOING content" "DONE" "DONE content" "content")) + +(defn- handle-last-input-handler + "Spied version of editor/keydown-not-matched-handler" + [{:keys [value cursor-pos]}] + ;; Reset editor action in order to test result + (state/set-editor-action! nil) + ;; Default cursor pos to end of line + (let [pos (or cursor-pos (count value))] + (with-redefs [state/get-input (constantly #js {:value value}) + cursor/pos (constantly pos) + cursor/move-cursor-backward (constantly nil) ;; ignore if called + cursor/get-caret-pos (constantly {})] + (editor/handle-last-input)))) + +(deftest handle-last-input-handler-test + (testing "Property autocompletion" + (handle-last-input-handler {:value "::"}) + (is (= :property-search (state/get-editor-action)) + "Autocomplete properties if only colons have been typed") + + (handle-last-input-handler {:value "foo::bar\n::"}) + (is (= :property-search (state/get-editor-action)) + "Autocomplete properties if typing colons on a second line") + + (handle-last-input-handler {:value "middle of line::"}) + (is (= nil (state/get-editor-action)) + "Don't autocomplete properties if typing colons in the middle of a line") + + (handle-last-input-handler {:value "first \nfoo::bar" + :cursor-pos (dec (count "first "))}) + (is (= nil (state/get-editor-action)) + "Don't autocomplete properties if typing in a block where properties already exist")))