pipeline_test.cljs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (ns frontend.worker.pipeline-test
  2. (:require [cljs.test :refer [deftest is use-fixtures testing]]
  3. [datascript.core :as d]
  4. [frontend.db :as db]
  5. [frontend.worker.pipeline :as worker-pipeline]
  6. [frontend.test.helper :as test-helper :refer [load-test-files]]))
  7. (use-fixtures :each test-helper/start-and-destroy-db)
  8. (defn- get-blocks [db]
  9. (->> (d/q '[:find (pull ?b [* {:block/path-refs [:block/name :db/id]}])
  10. :in $
  11. :where [?b :block/content] [(missing? $ ?b :block/pre-block?)]]
  12. db)
  13. (map first)))
  14. ;; TODO: Move this test to outliner dep when there is a load-test-files helper for deps
  15. (deftest compute-block-path-refs-tx
  16. (load-test-files [{:file/path "pages/page1.md"
  17. :file/content "prop:: #bar
  18. - parent #foo
  19. - child #baz
  20. - grandchild #bing"}])
  21. (testing "when a block's :refs change, descendants of block have correct :block/path-refs"
  22. (let [conn (db/get-db test-helper/test-db false)
  23. blocks (get-blocks @conn)
  24. ;; Update parent block to replace #foo with #bar
  25. new-tag-id (:db/id (db/get-page "bar"))
  26. modified-blocks (map #(if (= "parent #foo" (:block/content %))
  27. (assoc %
  28. :block/refs [{:db/id new-tag-id}]
  29. :block/path-refs [{:db/id new-tag-id}])
  30. %)
  31. blocks)
  32. refs-tx (worker-pipeline/compute-block-path-refs-tx {:tx-meta {:outliner-op :save-block} :db-after @conn} modified-blocks)
  33. _ (d/transact! conn refs-tx)
  34. updated-blocks (->> (get-blocks @conn)
  35. (map #(hash-map :block/content (:block/content %)
  36. :path-ref-names (mapv :block/name (:block/path-refs %)))))]
  37. (is (= [;; still have old parent content b/c we're only testing :block/path-refs updates
  38. {:block/content "parent #foo"
  39. :path-ref-names ["page1" "bar"]}
  40. {:block/content "child #baz"
  41. :path-ref-names ["page1" "bar" "baz"]}
  42. {:block/content "grandchild #bing"
  43. :path-ref-names ["page1" "bar" "baz" "bing"]}]
  44. updated-blocks)))))