2016-07-27 05:43:47 +00:00
|
|
|
(ns re-frame.fx-test
|
|
|
|
(:require
|
|
|
|
[cljs.test :refer-macros [is deftest async use-fixtures]]
|
|
|
|
[re-frame.core :as re-frame]
|
|
|
|
[re-frame.fx]
|
2016-10-15 00:40:34 +00:00
|
|
|
[re-frame.interop :refer [set-timeout!]]
|
2016-10-16 21:31:00 +00:00
|
|
|
[re-frame.loggers :as log]
|
|
|
|
[clojure.string :as str]))
|
2016-07-27 05:43:47 +00:00
|
|
|
|
|
|
|
;; ---- FIXTURES ---------------------------------------------------------------
|
|
|
|
|
2016-08-24 00:44:21 +00:00
|
|
|
;; This fixture uses the re-frame.core/make-restore-fn to checkpoint and reset
|
|
|
|
;; to cleanup any dynamically registered handlers from our tests.
|
|
|
|
(defn fixture-re-frame
|
|
|
|
[]
|
|
|
|
(let [restore-re-frame (atom nil)]
|
|
|
|
{:before #(reset! restore-re-frame (re-frame.core/make-restore-fn))
|
|
|
|
:after #(@restore-re-frame)}))
|
2016-07-27 05:43:47 +00:00
|
|
|
|
2016-10-16 21:31:00 +00:00
|
|
|
(use-fixtures :each (fixture-re-frame))
|
2016-07-27 05:43:47 +00:00
|
|
|
|
|
|
|
;; ---- TESTS ------------------------------------------------------------------
|
|
|
|
|
|
|
|
(deftest dispatch-later
|
|
|
|
(let [seen-events (atom [])]
|
|
|
|
;; Setup and excercise effects handler with :dispatch-later.
|
|
|
|
(re-frame/reg-event-fx
|
|
|
|
::later-test
|
|
|
|
(fn [_world _event-v]
|
2016-08-03 02:53:01 +00:00
|
|
|
(re-frame/reg-event-db
|
2016-07-27 05:43:47 +00:00
|
|
|
::watcher
|
|
|
|
(fn [db [_ token]]
|
|
|
|
(is (#{:event1 :event2 :event3} token) "unexpected: token passed through")
|
|
|
|
(swap! seen-events #(conj % token))
|
|
|
|
db))
|
|
|
|
{:dispatch-later [{:ms 100 :dispatch [::watcher :event1]}
|
2016-07-27 06:03:29 +00:00
|
|
|
{:ms 200 :dispatch [::watcher :event2]}
|
|
|
|
{:ms 200 :dispatch [::watcher :event3]}]}))
|
2016-07-27 05:43:47 +00:00
|
|
|
|
|
|
|
(async done
|
|
|
|
(set-timeout!
|
|
|
|
(fn []
|
|
|
|
(is (= @seen-events [:event1 :event2 :event3]) "All 3 events should have fired in order")
|
|
|
|
(done))
|
|
|
|
1000)
|
|
|
|
;; kick off main handler
|
|
|
|
(re-frame/dispatch [::later-test]))))
|
2016-10-15 00:40:34 +00:00
|
|
|
|
2016-10-16 21:31:00 +00:00
|
|
|
(re-frame/reg-event-fx
|
|
|
|
::missing-handler-test
|
|
|
|
(fn [_world _event-v]
|
|
|
|
{:fx-not-exist [:nothing :here]}))
|
2016-10-15 00:40:34 +00:00
|
|
|
|
2016-10-16 21:31:00 +00:00
|
|
|
(deftest report-missing-handler
|
|
|
|
(let [logs (atom [])
|
|
|
|
log-fn (fn [& args] (swap! logs conj (str/join args)))
|
|
|
|
original-loggers (log/get-loggers)]
|
|
|
|
(try
|
|
|
|
(log/set-loggers! {:error log-fn})
|
|
|
|
(re-frame/dispatch-sync [::missing-handler-test])
|
2018-01-17 02:36:13 +00:00
|
|
|
(is (re-matches #"re-frame: no handler registered for effect::fx-not-exist. Ignoring." (first @logs)))
|
2016-10-16 21:31:00 +00:00
|
|
|
(is (= (count @logs) 1))
|
|
|
|
(finally
|
|
|
|
(log/set-loggers! original-loggers)))))
|