Bläddra i källkod

Add tests for :current-block and :parent-block

Remove before-ms and after-ms if we don't have a good use case for it
Gabriel Horner 2 år sedan
förälder
incheckning
506faa89c7

+ 7 - 19
src/main/frontend/db/query_react.cljs

@@ -21,8 +21,8 @@
   ([input current-block-uuid]
    (cond
      (= :right-now-ms input) (util/time-ms)
-     (= :start-of-today-ms input) (util/date-at-local-ms 0 0 0 0)
-     (= :end-of-today-ms input) (util/date-at-local-ms 24 0 0 0)
+     (= :start-of-today-ms input) (util/today-at-local-ms 0 0 0 0)
+     (= :end-of-today-ms input) (util/today-at-local-ms 24 0 0 0)
 
      (= :today input)
      (date->int (t/today))
@@ -38,34 +38,22 @@
      (:db/id (db-utils/entity [:block/uuid current-block-uuid]))
      (and current-block-uuid (= :parent-block input))
      (:db/id (model/get-block-parent current-block-uuid))
-     ;; :3d-before-ms
-     (and (keyword? input)
-          (re-find #"^\d+d(-before-ms)?$" (name input)))
-     (let [input (name input)
-           days (parse-long (re-find #"^\d+" input))]
-       (util/date-at-local-ms (t/minus (t/today) (t/days days)) 0 0 0 0))
-     ;; :3d-after-ms
-     (and (keyword? input)
-          (re-find #"^\d+d(-after-ms)?$" (name input)))
-     (let [input (name input)
-           days (parse-long (re-find #"^\d+" input))]
-       (util/date-at-local-ms (t/plus (t/today) (t/days days)) 24 0 0 0))
-     ;; :3d-before
+     ;; e.g. :3d-before
      (and (keyword? input)
           (re-find #"^\d+d(-before)?$" (name input)))
      (let [input (name input)
            days (parse-long (re-find #"^\d+" input))]
        (date->int (t/minus (t/today) (t/days days))))
-     ;; :3d-after
+     ;; e.g. :3d-after
      (and (keyword? input)
           (re-find #"^\d+d(-after)?$" (name input)))
      (let [input (name input)
            days (parse-long (re-find #"^\d+" input))]
        (date->int (t/plus (t/today) (t/days days))))
 
-    (and (string? input) (page-ref/page-ref? input))
-    (-> (page-ref/get-page-name input)
-        (string/lower-case))
+     (and (string? input) (page-ref/page-ref? input))
+     (-> (page-ref/get-page-name input)
+         (string/lower-case))
 
      :else
      input)))

+ 2 - 5
src/main/frontend/util.cljc

@@ -766,11 +766,8 @@
 ;; For example, if you run this function at 10pm EDT in the EDT timezone on May 31st,
 ;; it will return 1622433600000, which is equivalent to Mon May 31 2021 00 :00:00.
 #?(:cljs
-   (defn date-at-local-ms
-     ([hours mins secs millisecs]
-      (date-at-local-ms (.now js/Date) hours mins secs millisecs))
-     ([date hours mins secs millisecs]
-      (.setHours (js/Date. date) hours mins secs millisecs))))
+   (defn today-at-local-ms [hours mins secs millisecs]
+     (.setHours (js/Date. (.now js/Date)) hours mins secs millisecs)))
 
 (defn d
   [k f]

+ 61 - 0
src/test/frontend/db/query_react_test.cljs

@@ -0,0 +1,61 @@
+(ns frontend.db.query-react-test
+  (:require [cljs.test :refer [deftest is use-fixtures]]
+            [clojure.pprint]
+            [frontend.state :as state]
+            [frontend.test.helper :as test-helper :refer [load-test-files]]
+            [frontend.db.query-custom :as query-custom]
+            [frontend.db.utils :as db-utils]
+            [frontend.db.react :as react]))
+
+(use-fixtures :each {:before test-helper/start-test-db!
+                     :after test-helper/destroy-test-db!})
+
+(defn- custom-query
+  "Use custom-query over react-query for testing since it handles rules the way users
+expect"
+  [query & [opts]]
+  (react/clear-query-state!)
+  (when-let [result (query-custom/custom-query test-helper/test-db query opts)]
+    (map first (deref result))))
+
+(deftest resolve-input
+  (load-test-files [{:file/path "pages/page1.md"
+                     :file/content
+                     "- parent
+   - child 1
+   - child 2"}])
+
+  (is (= ["child 2" "child 1" "parent"]
+         (with-redefs [state/get-current-page (constantly "page1")]
+           (map :block/content
+                (custom-query {:inputs [:current-page]
+                               :query '[:find (pull ?b [*])
+                                        :in $ ?current-page
+                                        :where [?b :block/page ?bp]
+                                        [?bp :block/name ?current-page]]}))))
+      ":current-page input resolves to current page name")
+
+  (is (= ["child 1" "child 2"]
+         (let [block-uuid (-> (db-utils/q '[:find (pull ?b [:block/uuid])
+                                            :where [?b :block/content "parent"]])
+                              ffirst
+                              :block/uuid)]
+           (map :block/content
+                (custom-query {:inputs [:current-block]
+                               :query '[:find (pull ?b [*])
+                                        :in $ ?current-block
+                                        :where [?b :block/parent ?current-block]]}
+                              {:current-block-uuid block-uuid}))))
+      ":current-block input resolves to current block's :db/id")
+  (is (= ["parent"]
+         (let [block-uuid (-> (db-utils/q '[:find (pull ?b [:block/uuid])
+                                            :where [?b :block/content "child 1"]])
+                              ffirst
+                              :block/uuid)]
+           (map :block/content
+                (custom-query {:inputs [:parent-block]
+                               :query '[:find (pull ?parent-block [*])
+                                        :in $ ?parent-block
+                                        :where [?parent-block :block/parent]]}
+                              {:current-block-uuid block-uuid}))))
+      ":parent-block input resolves to parent of current blocks's :db/id"))