core_test.cljs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. (ns frontend.modules.outliner.core-test
  2. (:require [cljs.test :refer [deftest is are testing use-fixtures run-tests] :as test]
  3. [frontend.modules.outliner.tree :as tree]
  4. [datascript.core :as d]
  5. [frontend.react :as r]
  6. [frontend.modules.outliner.utils :as outliner-u]
  7. [frontend.modules.outliner.core :as outliner-core]
  8. [frontend.modules.outliner.datascript :as outliner-ds]
  9. [frontend.fixtures :as fixtures]
  10. [cljs-run-test :refer [run-test]]
  11. [frontend.core-test :as core-test]
  12. [frontend.handler.block :as block]))
  13. (use-fixtures :each
  14. fixtures/load-test-env
  15. fixtures/react-impl
  16. fixtures/react-components
  17. fixtures/reset-db)
  18. (defn build-block
  19. ([id]
  20. (build-block id nil nil))
  21. ([id parent-id left-id & [m]]
  22. (let [m (->> (merge m {:block/uuid id
  23. :block/parent
  24. (outliner-u/->block-lookup-ref parent-id)
  25. :block/left
  26. (outliner-u/->block-lookup-ref left-id)
  27. :block/content (str id)})
  28. (remove #(nil? (val %)))
  29. (into {}))]
  30. (outliner-core/block m))))
  31. (defrecord TreeNode [id children])
  32. (defn build-node-tree
  33. [[id children :as _tree]]
  34. (let [children (mapv build-node-tree children)]
  35. (->TreeNode id children)))
  36. (defn build-db-records
  37. "build RDS record from memory node struct."
  38. [tree-record]
  39. (outliner-ds/auto-transact!
  40. [state (outliner-ds/new-outliner-txs-state)] nil
  41. (letfn [(build [node queue]
  42. (let [{:keys [id left parent]} node
  43. block (build-block id parent left)
  44. left (atom (:id node))
  45. children (map (fn [c]
  46. (let [node (assoc c :left @left :parent (:id node))]
  47. (swap! left (constantly (:id c)))
  48. node))
  49. (:children node))
  50. queue (concat queue children)]
  51. (tree/-save block state)
  52. (when (seq queue)
  53. (build (first queue) (rest queue)))))]
  54. (let [root (assoc tree-record :left "1" :parent "1")]
  55. (tree/-save (build-block "1") state)
  56. (build root '())))))
  57. (def tree [1 [[2 [[3 [[4]
  58. [5]]]
  59. [6 [[7 [[8]]]]]
  60. [9 [[10]
  61. [11]]]]]
  62. [12 [[13]
  63. [14]
  64. [15]]]
  65. [16 [[17]]]]])
  66. (def node-tree (build-node-tree tree))
  67. (comment
  68. (build-db-records node-tree)
  69. (dotimes [i 18]
  70. (when-not (= i 0)
  71. (prn (d/pull @(core-test/get-current-conn) '[*] [:block/uuid i])))))
  72. (deftest test-insert-node-as-first-child
  73. "
  74. Insert a node between 6 and 9.
  75. [1 [[2 [[18] ;; add
  76. [3 [[4]
  77. [5]]]
  78. [6 [[7 [[8]]]]]
  79. [9 [[10]
  80. [11]]]]]
  81. [12 [[13]
  82. [14]
  83. [15]]]
  84. [16 [[17]]]]]
  85. "
  86. (build-db-records node-tree)
  87. (let [new-node (build-block 18 nil nil)
  88. parent-node (build-block 2 1 1)]
  89. (outliner-ds/auto-transact!
  90. [state (outliner-ds/new-outliner-txs-state)] nil
  91. (outliner-core/insert-node-as-first-child state new-node parent-node))
  92. (let [children-of-2 (->> (build-block 2 1 1)
  93. (tree/-get-children)
  94. (mapv #(-> % :data :block/uuid)))]
  95. (is (= [18 3 6 9] children-of-2)))))
  96. (deftest test-insert-node-as-sibling
  97. "
  98. Insert a node between 6 and 9.
  99. [1 [[2 [[3 [[4]
  100. [5]]]
  101. [6 [[7 [[8]]]]]
  102. [18] ;; add
  103. [9 [[10]
  104. [11]]]]]
  105. [12 [[13]
  106. [14]
  107. [15]]]
  108. [16 [[17]]]]]
  109. "
  110. (build-db-records node-tree)
  111. (let [new-node (build-block 18 nil nil)
  112. left-node (build-block 6 2 3)]
  113. (outliner-ds/auto-transact!
  114. [state (outliner-ds/new-outliner-txs-state)] nil
  115. (outliner-core/insert-node-as-sibling state new-node left-node))
  116. (let [children-of-2 (->> (build-block 2 1 1)
  117. (tree/-get-children)
  118. (mapv #(-> % :data :block/uuid)))]
  119. (is (= [3 6 18 9] children-of-2)))))
  120. (deftest test-delete-node
  121. "
  122. Inert a node between 6 and 9.
  123. [1 [[2 [[3 [[4]
  124. [5]]]
  125. [6 [[7 [[8]]]]] ;; delete 6
  126. [9 [[10]
  127. [11]]]]]
  128. [12 [[13]
  129. [14]
  130. [15]]]
  131. [16 [[17]]]]]
  132. "
  133. (build-db-records node-tree)
  134. (let [node (build-block 6 2 3)]
  135. (outliner-core/delete-node node true)
  136. (let [children-of-2 (->> (build-block 2 1 1)
  137. (tree/-get-children)
  138. (mapv #(-> % :data :block/uuid)))]
  139. (is (= [3 9] children-of-2)))))
  140. (deftest test-move-subtree-as-sibling
  141. "
  142. Move 3 between 14 and 15.
  143. [1 [[2 [[6 [[7 [[8]]]]]
  144. [9 [[10]
  145. [11]]]]]
  146. [12 [[13]
  147. [14]
  148. [3 [[4] ;; moved 3
  149. [5]]]
  150. [15]]]
  151. [16 [[17]]]]]
  152. "
  153. (build-db-records node-tree)
  154. (let [node (build-block 3 2 2)
  155. target-node (build-block 14 12 13)]
  156. (outliner-core/move-subtree node target-node true)
  157. (let [old-parent's-children (->> (build-block 2 1 1)
  158. (tree/-get-children)
  159. (mapv #(-> % :data :block/uuid)))
  160. new-parent's-children (->> (build-block 12 1 2)
  161. (tree/-get-children)
  162. (mapv #(-> % :data :block/uuid)))]
  163. (is (= [6 9] old-parent's-children))
  164. (is (= [13 14 3 15] new-parent's-children)))))
  165. (deftest test-move-subtree-as-first-child
  166. "
  167. Move 3 as first child of 12.
  168. [1 [[2 [[6 [[7 [[8]]]]]
  169. [9 [[10]
  170. [11]]]]]
  171. [12 [[3 [[4] ;; moved 3
  172. [5]]]
  173. [13]
  174. [14]
  175. [15]]]
  176. [16 [[17]]]]]
  177. "
  178. (build-db-records node-tree)
  179. (let [node (build-block 3 2 2)
  180. target-node (build-block 12 1 2)]
  181. (outliner-core/move-subtree node target-node false)
  182. (let [old-parent's-children (->> (build-block 2 1 1)
  183. (tree/-get-children)
  184. (mapv #(-> % :data :block/uuid)))
  185. new-parent's-children (->> (build-block 12 1 2)
  186. (tree/-get-children)
  187. (mapv #(-> % :data :block/uuid)))]
  188. (is (= [6 9] old-parent's-children))
  189. (is (= [3 13 14 15] new-parent's-children)))))
  190. (deftest test-indent-nodes
  191. "
  192. [1 [[2 [[3
  193. [[4]
  194. [5]
  195. [6 [[7 [[8]]]]] ;; indent 6, 9
  196. [9 [[10]
  197. [11]]]]]]]
  198. [12 [[13]
  199. [14]
  200. [15]]]
  201. [16 [[17]]]]]
  202. "
  203. (build-db-records node-tree)
  204. (let [nodes [(build-block 6 2 3)
  205. (build-block 9 2 6)]]
  206. (outliner-core/indent-outdent-nodes nodes true)
  207. (let [children-of-3 (->> (build-block 3)
  208. (tree/-get-children)
  209. (mapv #(-> % :data :block/uuid)))]
  210. (is (= [4 5 6 9] children-of-3)))))
  211. (deftest test-outdent-nodes
  212. "
  213. [1 [[2 [[3]
  214. [4] ;; outdent 6, 9
  215. [5]
  216. [6 [[7 [[8]]]]]
  217. [9 [[10]
  218. [11]]]]]
  219. [12 [[13]
  220. [14]
  221. [15]]]
  222. [16 [[17]]]]]
  223. "
  224. (build-db-records node-tree)
  225. (let [nodes [(build-block 4 3 3)
  226. (build-block 5 3 4)]]
  227. (outliner-core/indent-outdent-nodes nodes false)
  228. (let [children-of-2 (->> (build-block 2)
  229. (tree/-get-children)
  230. (mapv #(-> % :data :block/uuid)))]
  231. (is (= [3 4 5 6 9] children-of-2)))))
  232. (comment
  233. (run-test test-outdent-nodes))
  234. (deftest test-delete-nodes
  235. "
  236. [1 [[2 [[3 [[4]
  237. [5]]]
  238. ;[6 [[7 [[8]]]]] delete 6, 9
  239. ;[9 [[10]
  240. ; [11]]]
  241. ]]
  242. [12 [[13]
  243. [14]
  244. [15]]]
  245. [16 [[17]]]]]
  246. "
  247. (build-db-records node-tree)
  248. (let [start-node (build-block 6 2 3)
  249. end-node (build-block 11 9 10)
  250. block-ids [7 8 9 10]]
  251. (outliner-core/delete-nodes start-node end-node block-ids)
  252. (let [children-of-2 (->> (build-block 2)
  253. (tree/-get-children)
  254. (mapv #(-> % :data :block/uuid)))]
  255. (is (= [3] children-of-2)))))
  256. (comment
  257. (run-test test-delete-nodes))
  258. (deftest test-move-node
  259. "
  260. [1 [[2 [[3 [[4]
  261. [5]]]
  262. [9 [[10] ;; swap 6 and 9
  263. [11]]]
  264. [6 [[7 [[8]]]]]]]
  265. [12 [[13]
  266. [14]
  267. [15]]]
  268. [16 [[17]]]]]
  269. "
  270. (build-db-records node-tree)
  271. (let [node (build-block 9 2 6)]
  272. (outliner-core/move-node node true)
  273. (let [children-of-2 (->> (build-block 2)
  274. (tree/-get-children)
  275. (mapv #(-> % :data :block/uuid)))]
  276. (is (= [3 9 6] children-of-2)))))
  277. (comment
  278. (run-test test-move-node))
  279. (deftest test-insert-nodes
  280. "
  281. add [18 [19 20] 21] after 6
  282. [1 [[2 [[3 [[4]
  283. [5]]]
  284. [6 [[7 [[8]]]]]
  285. [9 [[10]
  286. [11]]]]]
  287. [12 [[13]
  288. [14]
  289. [15]]]
  290. [16 [[17]]]]]
  291. "
  292. (build-db-records node-tree)
  293. (let [new-nodes-tree [(build-block 18)
  294. [(build-block 19)
  295. (build-block 20)]
  296. (build-block 21)]
  297. target-node (build-block 6 2 3)]
  298. (outliner-core/insert-nodes
  299. new-nodes-tree target-node true)
  300. (let [children-of-2 (->> (build-block 2)
  301. (tree/-get-children)
  302. (mapv #(-> % :data :block/uuid)))]
  303. (is (= [3 6 18 21 9] children-of-2)))
  304. (let [children-of-18 (->> (build-block 18)
  305. (tree/-get-children)
  306. (mapv #(-> % :data :block/uuid)))]
  307. (is (= [19 20] children-of-18)))))
  308. (comment
  309. (run-test test-insert-nodes))
  310. (comment
  311. (run-tests))