Sfoglia il codice sorgente

Merge pull request #4432 from logseq/dev/improve-tests-for-wrong-logbook-creation

Dev: Rewrite tests from #4382 to use with-redefs
Gabriel Horner 3 anni fa
parent
commit
93c4a21ba9

+ 2 - 0
shadow-cljs.edn

@@ -62,6 +62,8 @@
          :output-to       "static/tests.js"
          :closure-defines {frontend.util/NODETEST true}
          :devtools        {:enabled false}
+         ;; disable :static-fns to allow for with-redefs and repl development
+         :compiler-options {:static-fns false}
          :main            frontend.node-test-runner/main}
 
   :publishing {:target        :browser

+ 22 - 1
src/test/frontend/handler/editor_test.cljs

@@ -1,6 +1,6 @@
 (ns frontend.handler.editor-test
   (:require [frontend.handler.editor :as editor]
-            [clojure.test :refer [deftest is testing]]))
+            [clojure.test :refer [deftest is testing are]]))
 
 (deftest extract-nearest-link-from-text-test
   (testing "Page, block and tag links"
@@ -41,3 +41,24 @@
          (editor/extract-nearest-link-from-text
           "[[https://github.com/logseq/logseq][logseq]] is #awesome :)" 0 editor/url-regex))
       "Finds url in org link correctly"))
+
+(defn- set-marker
+  [marker content format new-marker]
+  (let [actual-content (atom nil)]
+    (with-redefs [editor/save-block-if-changed! (fn [_ content]
+                                                  (reset! actual-content content))]
+      (editor/set-marker {:block/marker marker :block/content content :block/format format}
+                         new-marker)
+      @actual-content)))
+
+(deftest set-marker-org
+  (are [marker content new-marker expect] (= expect (set-marker marker content :org new-marker))
+    "TODO" "TODO content" "DOING" "DOING content"
+    "TODO" "## TODO content" "DOING" "## TODO content"
+    "DONE" "DONE content" "" "content"))
+
+(deftest set-marker-markdown
+  (are [marker content new-marker expect] (= expect (set-marker marker content :markdown new-marker))
+    "TODO" "TODO content" "DOING" "DOING content"
+    "TODO" "## TODO content" "DOING" "## DOING content"
+    "DONE" "DONE content" "" "content"))

+ 1 - 29
src/test/frontend/util/marker_test.cljs

@@ -1,7 +1,6 @@
 (ns frontend.util.marker-test
   (:require [cljs.test :refer [are deftest]]
-            [frontend.util.marker :as marker]
-            [clojure.string :as string]))
+            [frontend.util.marker :as marker]))
 
 (deftest add-or-update-marker-markdown
   (are [content marker expect] (= expect (marker/add-or-update-marker content :markdown marker))
@@ -24,31 +23,4 @@
     "TODO xxx" "DONE" "DONE xxx"
     "TODO" "DONE" "DONE "))
 
-(defn set-marker
-  [marker content format new-marker]
-  (let [old-header-marker (when (not= format :org)
-                            (re-find (marker/header-marker-pattern true marker) content))
-        new-header-marker (when old-header-marker
-                            (string/replace old-header-marker marker new-marker))
-        marker (or old-header-marker marker)
-        new-marker (or new-header-marker new-marker)
-        new-content (->
-                     (if marker
-                       (string/replace-first content (re-pattern (str "^" marker)) new-marker)
-                       (str new-marker " " content))
-                     (string/triml))]
-    new-content))
-
-(deftest set-marker-org
-  (are [marker content new-marker expect] (= expect (set-marker marker content :org new-marker))
-    "TODO" "TODO content" "DOING" "DOING content"
-    "TODO" "## TODO content" "DOING" "## TODO content"
-    "DONE" "DONE content" "" "content"))
-
-(deftest set-marker-markdown
-  (are [marker content new-marker expect] (= expect (set-marker marker content :markdown new-marker))
-    "TODO" "TODO content" "DOING" "DOING content"
-    "TODO" "## TODO content" "DOING" "## DOING content"
-    "DONE" "DONE content" "" "content"))
-
 #_(cljs.test/run-tests)