Skip to content

Commit

Permalink
add unit-tests for export
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci authored and tiensonqin committed Mar 20, 2023
1 parent 6fae1a8 commit 3cda885
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/main/frontend/handler/export/text.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@
(let [ast (gp-mldoc/->edn content (gp-mldoc/default-config format))
ast (mapv common/remove-block-ast-pos ast)
ast (removev common/Properties-block-ast? ast)
keep-level<=n (get-in *state* [:export-options :keep-only-level<=N])
ast (if (pos? keep-level<=n)
(common/keep-only-level<=n ast keep-level<=n)
ast)
ast* (common/replace-block&page-reference&embed ast)
keep-level<=n (get-in *state* [:export-options :keep-only-level<=N])
ast* (if (pos? keep-level<=n)
(common/keep-only-level<=n ast* keep-level<=n)
ast*)
ast** (if (= "no-indent" (get-in *state* [:export-options :indent-style]))
(mapv common/replace-Heading-with-Paragraph ast*)
ast*)
Expand Down
129 changes: 107 additions & 22 deletions src/test/frontend/handler/export_test.cljs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
(ns frontend.handler.export-test
(:require [cljs.test :refer [async use-fixtures are is]]
[frontend.test.helper :as test-helper :include-macros true :refer [deftest-async]]
(:require [cljs.test :refer [are async deftest is use-fixtures]]
[clojure.edn :as edn]
[clojure.string :as string]
[frontend.handler.export :as export]
[frontend.handler.export.text :as export-text]
[frontend.state :as state]
[frontend.test.helper :as test-helper :include-macros true :refer [deftest-async]]
[promesa.core :as p]))

(def test-files
[{:file/path "pages/page1.md"
:file/content
"- 1
(string/trim "
- 1
id:: 61506710-484c-46d5-9983-3d1651ec02c8
- 2
id:: 61506711-5638-4899-ad78-187bdc2eaffc
- 3
id:: 61506712-3007-407e-b6d3-d008a8dfa88b
- ((61506712-3007-407e-b6d3-d008a8dfa88b))
- 4
id:: 61506712-b8a7-491d-ad84-b71651c3fdab"}
id:: 61506712-b8a7-491d-ad84-b71651c3fdab")}
{:file/path "pages/page2.md"
:file/content
"- 3
(string/trim "
- 3
id:: 97a00e55-48c3-48d8-b9ca-417b16e3a616
- {{embed [[page1]]}}"}])
- {{embed [[page1]]}}")}])

(use-fixtures :once
{:before (fn []
Expand All @@ -33,28 +36,110 @@
(done))))
:after test-helper/destroy-test-db!})

(deftest-async export-blocks-as-markdown
(p/do!
(are [expect block-uuid-s]
(= expect
(export-text/export-blocks-as-markdown (state/get-current-repo) [(uuid block-uuid-s)]
{:remove-options #{:property}}))
"- 1\n\t- 2\n\t\t- 3\n\t\t- 3\n"
"61506710-484c-46d5-9983-3d1651ec02c8"
(deftest export-blocks-as-markdown-without-properties
(are [expect block-uuid-s]
(= expect
(string/trim (export-text/export-blocks-as-markdown (state/get-current-repo) [(uuid block-uuid-s)]
{:remove-options #{:property}})))
(string/trim "
- 1
- 2
- 3
- 3")
"61506710-484c-46d5-9983-3d1651ec02c8"

(string/trim "
- 3
- 1
- 2
- 3
- 3
- 4")
"97a00e55-48c3-48d8-b9ca-417b16e3a616"))


(deftest export-blocks-as-markdown-with-properties
(are [expect block-uuid-s]
(= expect (string/trim (export-text/export-blocks-as-markdown (state/get-current-repo) [(uuid block-uuid-s)] {})))
(string/trim "
- 1
id:: 61506710-484c-46d5-9983-3d1651ec02c8
- 2
id:: 61506711-5638-4899-ad78-187bdc2eaffc
- 3
id:: 61506712-3007-407e-b6d3-d008a8dfa88b
- 3")
"61506710-484c-46d5-9983-3d1651ec02c8"

(string/trim "
- 3
id:: 97a00e55-48c3-48d8-b9ca-417b16e3a616
- 1
id:: 61506710-484c-46d5-9983-3d1651ec02c8
- 2
id:: 61506711-5638-4899-ad78-187bdc2eaffc
- 3
id:: 61506712-3007-407e-b6d3-d008a8dfa88b
- 3
- 4
id:: 61506712-b8a7-491d-ad84-b71651c3fdab")
"97a00e55-48c3-48d8-b9ca-417b16e3a616"))

(deftest export-blocks-as-markdown-level<N
(are [expect block-uuid-s]
(= expect (string/trim (export-text/export-blocks-as-markdown (state/get-current-repo) [(uuid block-uuid-s)]
{:remove-options #{:property}
:other-options {:keep-only-level<=N 2}})))
(string/trim "
- 1
- 2")
"61506710-484c-46d5-9983-3d1651ec02c8"

(string/trim "
- 3
- 1
- 4")
"97a00e55-48c3-48d8-b9ca-417b16e3a616"))

(deftest export-blocks-as-markdown-newline-after-block
(are [expect block-uuid-s]
(= expect (string/trim (export-text/export-blocks-as-markdown (state/get-current-repo) [(uuid block-uuid-s)]
{:remove-options #{:property}
:other-options {:newline-after-block true}})))
(string/trim "
- 1
- 2
- 3
- 3")
"61506710-484c-46d5-9983-3d1651ec02c8"
(string/trim "
- 3
- 1
- 2
- 3
- 3
- 4")
"97a00e55-48c3-48d8-b9ca-417b16e3a616"))

"- 3\n\t- 1\n\t\t- 2\n\t\t\t- 3\n\t\t\t- 3\n\t- 4\n"
"97a00e55-48c3-48d8-b9ca-417b16e3a616")))

(deftest-async export-files-as-markdown
(p/do!
(are [expect files]
(= expect
(@#'export-text/export-files-as-markdown files {:remove-options #{:property}}))
[["pages/page1.md" "- 1\n\t- 2\n\t\t- 3\n\t\t- 3\n- 4\n"]]
[{:path "pages/page1.md" :content (:file/content (nth test-files 0)) :names ["page1"] :format :markdown}]
(= expect
(@#'export-text/export-files-as-markdown files {:remove-options #{:property}}))
[["pages/page1.md" "- 1\n\t- 2\n\t\t- 3\n\t\t- 3\n- 4\n"]]
[{:path "pages/page1.md" :content (:file/content (nth test-files 0)) :names ["page1"] :format :markdown}]

[["pages/page2.md" "- 3\n\t- 1\n\t\t- 2\n\t\t\t- 3\n\t\t\t- 3\n\t- 4\n"]]
[{:path "pages/page2.md" :content (:file/content (nth test-files 1)) :names ["page2"] :format :markdown}])))
[["pages/page2.md" "- 3\n\t- 1\n\t\t- 2\n\t\t\t- 3\n\t\t\t- 3\n\t- 4\n"]]
[{:path "pages/page2.md" :content (:file/content (nth test-files 1)) :names ["page2"] :format :markdown}])))

(deftest-async export-repo-as-edn-str
(p/do!
Expand Down

0 comments on commit 3cda885

Please sign in to comment.