Browse Source

roam test with cljs

Adam Schmideg 4 years ago
parent
commit
d1e595ff76

+ 2 - 1
deps.edn

@@ -40,7 +40,8 @@
                   :main-opts ["-m" "shadow.cljs.devtools.cli"]}
            :test
            {:extra-paths ["src/test/"]
-            :extra-deps  {org.clojure/clojurescript {:mvn/version "1.10.764"}
+            :extra-deps  {cheshire/cheshire {:mvn/version "5.10.0"}
+                          org.clojure/clojurescript {:mvn/version "1.10.764"}
                           org.clojure/test.check {:mvn/version "RELEASE"}}
             :main-opts   ["-m" "shadow.cljs.devtools.cli"]}
            :runner

+ 0 - 0
src/main/frontend/external.cljs → src/main/frontend/external.cljc


+ 2 - 2
src/main/frontend/external/protocol.cljs → src/main/frontend/external/protocol.cljc

@@ -2,9 +2,9 @@
 
 (defprotocol External
   (toMarkdownFiles [this content config]
-    "Should return a map of markdown's file name to contents.")
+    "Should return a map of markdown's file name to contents."))
 
   ;; Long-term goal:
   ;; (toMldocAst [this content])
   ;; (fromMldocAst [this ast])
-  )
+

+ 6 - 4
src/main/frontend/external/roam.cljs → src/main/frontend/external/roam.cljc

@@ -1,6 +1,7 @@
 (ns frontend.external.roam
-  (:require [frontend.external.protocol :as protocol]
-            [cljs-bean.core :as bean]
+  (:require #?(:cljs [cljs-bean.core :as bean]
+               :clj [cheshire.core :as json])
+            [frontend.external.protocol :as protocol]
             [medley.core :as medley]
             [clojure.walk :as walk]
             [clojure.string :as string]
@@ -122,8 +123,9 @@
 (defrecord Roam []
   protocol/External
   (toMarkdownFiles [this content _config]
-    (let [data (bean/->clj (js/JSON.parse content))]
-      (->files data))))
+    #?(:cljs (let [data (bean/->clj (js/JSON.parse content))]
+               (->files data))
+       :clj (-> content json/parse-string ->files))))
 
 (comment
   (defonce test-roam-json (frontend.db/get-file "same.json"))

+ 33 - 0
src/test/frontend/external/roam_test.cljc

@@ -0,0 +1,33 @@
+(ns frontend.external.roam-test
+  (:require #?(:clj  [clojure.test :refer :all]
+               :cljs [cljs.test :refer [is deftest]])
+            ;[frontend.external.roam :as roam]
+            [frontend.external :refer [to-markdown-files]]))
+
+(def minimal-json "
+[
+ {
+  \"create-email\": \"[email protected]\",
+  \"create-time\": 1610708403162,
+  \"title\": \"Export JSON\",
+  \"children\": [
+                  {
+                    \"string\": \"Hello, world!\",
+                    \"create-email\": \"[email protected]\",
+                    \"create-time\": 1610708405787,
+                    \"uid\": \"7c5um7hSz\",
+                    \"edit-time\": 1610708415484,
+                    \"edit-email\": \"[email protected]\"}
+                ],
+  \"edit-time\": 1610708403169,
+  \"edit-email\": \"[email protected]\"}]
+")
+
+(deftest roam-import-test
+  (let [got (to-markdown-files :roam minimal-json {})
+        md (first got)]
+    (is (= 1 (count got)))
+    (is (= "Export JSON" (:title md)))
+    (is (:created-at md))
+    (is (:last-modified-at md))
+    (is (= "---\ntitle: Export JSON\n---\n\n## Hello, world!\n" (:text md)))))