Skip to content

Commit

Permalink
Test flow/run
Browse files Browse the repository at this point in the history
  • Loading branch information
kimo-k committed Nov 21, 2023
1 parent f196eee commit ae48479
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 65 deletions.
56 changes: 0 additions & 56 deletions src/re_frame/flow/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -167,59 +167,3 @@
(let [all-flows (with-cleared @flows)]
(swap! flows vary-meta dissoc ::cleared)
(reduce run ctx ((memoize topsort) all-flows))))}))

#_(do
(def still-alive
{:coeffects {:db {:l? :alive}}
:effects {:db {:l? :alive}}})

(def still-dead
{:coeffects {:db {:l? :dead}}
:effects {:db {:l? :dead}}})

(def died
{:coeffects {:db {:l? :alive :sometimes-path :SOMETIMES}}
:effects {:db {:l? :dead}}})

(def born
{:coeffects {:db {:l? :dead}}
:effects {:db {:l? :alive}}})

(def flow-after (:after interceptor))

(reg-flow
:db
{:live? (constantly true)
:output (fn [data _] data)
:path []})

(reg-flow
:sometimes-flow
{:live? (comp #{:alive} :l?)
:inputs [:db]
:output (fn [data inputs] :SOMETIMES)
:path [:sometimes-path]})

(reg-flow
:always-flow
{:live? (constantly true)
:inputs [:sometimes-flow]
:output (fn [data inputs] inputs)
:path [:always-path]})

(assert (= (flow-after still-alive)
{:coeffects {:db {:l? :alive :sometimes-path :SOMETIMES}}
:effects {:db {:l? :alive :sometimes-path :SOMETIMES}}}))
(assert (= (flow-after still-dead)
{:coeffects {:db {:l? :dead}}
:effects {:db {:l? :dead}}}))
(assert (= (flow-after died)
{:coeffects {:db {:l? :alive :sometimes-path :SOMETIMES}}
:effects {:db {:l? :dead}}}))
(assert (= (flow-after born)
{:coeffects {:db {:l? :dead}}
:effects {:db {:l? :alive :sometimes-path :SOMETIMES}}}))

(swap! flows dissoc :sometimes-flow)

nil)
78 changes: 73 additions & 5 deletions test/re_frame/flow/alpha_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require
[cljs.test :refer [is deftest async use-fixtures testing]]
[re-frame.alpha :as rf]
[re-frame.flow.alpha :as f]))
[re-frame.flow.alpha :as f]
[re-frame.db :refer [app-db]]))

(use-fixtures :each (fn [f] (f) (reset! f/flows {})))

Expand Down Expand Up @@ -55,7 +56,74 @@
(is (= :y (rf/get-flow db :x)))))

(deftest run-flow
(rf/reg-flow {:id :x
:inputs {}
:output (fn [db] {})
:path [:a :b]}))
(rf/reg-event-db :go-live (fn [db _] (assoc db :l? true)))
(rf/reg-event-db :go-dead (fn [db _] (dissoc db :l?)))
(rf/reg-flow {:id :live-db
:live? :l?})
(rf/reg-flow {:id :live-path
:live? :l?
:live-inputs {:l? [:l?]}})
(rf/reg-flow {:id :live-input
:live? :l?
:live-inputs {:l? (rf/flow<- :live-path)}})
(testing "basic flow"
(rf/reg-event-fx :basic-event (fn [_ _] {}))
(rf/reg-flow {:id :basic-flow})
(is (not (rf/get-flow @app-db :basic-flow)))
(rf/dispatch-sync [:basic-event])
(is (rf/get-flow @app-db :basic-flow)))
(testing "live flow"
(is (nil? (rf/get-flow @app-db :live-db)))
(is (nil? (rf/get-flow @app-db :live-path)))
(is (nil? (rf/get-flow @app-db :live-input)))
(rf/dispatch-sync [:go-live])
(is (rf/get-flow @app-db :live-db))
(is (rf/get-flow @app-db :live-path))
(is (rf/get-flow @app-db :live-input))
(rf/dispatch-sync [:go-dead])
(is (nil? (rf/get-flow @app-db :live-db)))
(is (nil? (rf/get-flow @app-db :live-path)))
(is (nil? (rf/get-flow @app-db :live-input))))
(testing "flow effects"
(let [flow-a {:id :fx-flow
:output (constantly :a)}
flow-b {:id :fx-flow
:output (constantly :b)}]
(rf/reg-event-fx :reg-a (fn [_ _] {:fx [[:reg-flow flow-a]]}))
(rf/reg-event-fx :reg-b (fn [_ _] {:fx [[:reg-flow flow-b]]}))
(rf/reg-event-fx :clear-fx (fn [_ _] {:fx [[:clear-flow :fx-flow]]}))
(is (nil? (rf/get-flow @app-db :fx-flow)))
(rf/dispatch-sync [:reg-a])
(is (= :a (rf/get-flow @app-db :fx-flow)))
(rf/dispatch-sync [:reg-b])
(is (= :b (rf/get-flow @app-db :fx-flow)))
(rf/dispatch-sync [:clear-fx])
(is (nil? (rf/get-flow @app-db :fx-flow)))))
(testing "flow run count"
(let [ct (atom 0)
live-ct (atom 0)
ct-flow {:id :ct-flow
:live-inputs {:l? [:l?]}
:live? (fn [{:keys [l?]}] (swap! live-ct inc) l?)
:output (fn [_] (swap! ct inc))}]
(rf/reg-flow ct-flow)
(rf/dispatch-sync [:go-live])
(is (= 1 @ct))
(is (= 1 @live-ct))
(rf/dispatch-sync [:go-dead])
(is (= 1 @ct))
(is (= 3 @live-ct))
(rf/dispatch-sync [:go-live])
(is (= 2 @ct))
(is (= 5 @live-ct))
(rf/clear-flow :ct-flow)
(rf/dispatch-sync [:go-live])
(is (= 2 @ct))
(is (= 6 @live-ct))
(rf/dispatch-sync [:go-live])
(is (= 2 @ct))
(is (= 6 @live-ct))
(rf/reg-flow ct-flow)
(rf/dispatch-sync [:go-live])
(is (= 3 @ct))
(is (= 7 @live-ct)))))
8 changes: 4 additions & 4 deletions test/re_frame/subs/alpha_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
(q/clear!)
(reset! side-effect-atom 0)
(let [test-sub (sub q)]
(reset! db/app-db :test)
(is (= :test @test-sub))
(reset! db/app-db {:test true})
(is (= {:test true} @test-sub))
(is (= @side-effect-atom 1))
;; no caching is done
(sub q)
Expand Down Expand Up @@ -133,8 +133,8 @@
(testing "Reactive subscription lifecycle"
(let [q {::rf/q :side-effecting-handler
::rf/lifecycle :reactive}]
(reset! db/app-db :test)
(is (= :test @(sub q)))
(reset! db/app-db {:test true})
(is (= {:test true} @(sub q)))
(is (= @side-effect-atom 1))
;; sub is cached, even outside a reactive context
(sub q)
Expand Down

0 comments on commit ae48479

Please # to comment.