diff --git a/spec/cljs/core_spec.cljs b/spec/cljs/core_spec.cljs deleted file mode 100644 index 03796df..0000000 --- a/spec/cljs/core_spec.cljs +++ /dev/null @@ -1,9 +0,0 @@ -(ns grub.core-spec - (:require-macros [specljs.core :refer [describe it should=]]) - (:require [specljs.core] - [grub.core])) - -(describe "A ClojureScript test" - (it "fails. Fix it!" - (should= 0 1))) - diff --git a/spec/cljs/state_spec.cljs b/spec/cljs/state_spec.cljs new file mode 100644 index 0000000..7b5bdbc --- /dev/null +++ b/spec/cljs/state_spec.cljs @@ -0,0 +1,58 @@ +(ns grub.state-spec + (:require [specljs.core] + [grub.state :as state]) + (:require-macros [specljs.core :refer [describe it before + should= should-contain + should-not-be-nil]] + [grub.macros :refer [log logs]])) + +(describe + "grub state" + (before (reset! state/grubs [])) + (describe "Add grub" + (it "should add a grub to the state when an add event comes" + (let [test-grub {:_id 12345 :grub "testgrub" :completed true} + add-event (assoc test-grub :event :add)] + (state/handle-event add-event) + (should-contain test-grub @state/grubs)))) + (describe "Create grub" + (it "should add a new grub to the state when a create event comes" + (let [test-grub {:grub "testgrub"} + expected-grub (assoc test-grub :completed false) + create-event (assoc test-grub :event :create)] + (state/handle-event create-event) + (let [created-grub (first @state/grubs)] + (should= (:grub created-grub) (:grub test-grub))))) + (it "should generate an _id for the new grub" + (let [test-grub {:grub "testgrub"} + create-event (assoc test-grub :event :create)] + (state/handle-event create-event) + (log @state/grubs) + (let [added-grub (first (filter #(= (:grub %) (:grub test-grub)) + @state/grubs))] + (should-not-be-nil (:_id added-grub)))))) + (describe "Complete grub" + (it "should complete a grub in the state when a complete event comes" + (let [test-grub {:_id 234243 :grub "testgrub" :completed false} + expected-grub (assoc test-grub :completed true) + complete-event (-> (select-keys [:_id]) + (assoc :event :complete))] + (reset! state/grubs [test-grub]) + (state/handle-event complete-event) + (should-contain expected-grub @state/grubs)))) + (describe "Uncomplete grub" + (it "should uncomplete a grub in the state when an uncomplete event comes" + (let [test-grub {:_id 234243 :grub "testgrub" :completed true} + expected-grub (assoc test-grub :completed false) + complete-event (-> (select-keys [:_id]) + (assoc :event :uncomplete))] + (reset! state/grubs [test-grub]) + (state/handle-event complete-event) + (should-contain expected-grub @state/grubs)))) + (describe "Delete grub" + (it "should delete a grub from the state when a delete event comes" + (let [test-grub {:_id 234243 :grub "testgrub" :completed true} + delete-event {:_id (:_id test-grub) :event :delete}] + (reset! state/grubs [test-grub]) + (state/handle-event delete-event) + (should= [] @state/grubs))))) diff --git a/src/cljs/grub/core.cljs b/src/cljs/grub/core.cljs index a569cf4..f840ea0 100644 --- a/src/cljs/grub/core.cljs +++ b/src/cljs/grub/core.cljs @@ -1,6 +1,5 @@ (ns grub.core - (:require [grub.async-utils - :refer [fan-in fan-out event-chan filter-chan do-chan do-chan! map-chan]] + (:require [grub.async-utils :as a] [grub.view :as view] [grub.websocket :as ws] [grub.state :as state] @@ -11,11 +10,11 @@ (defn handle-grub-events [] (let [local-events (view/get-local-events) - [local-events' local-events''] (fan-out local-events 2) + [local-events' local-events''] (a/fan-out local-events 2) remote-events (ws/get-remote-events) - events (fan-in [local-events' remote-events])] - (do-chan! ws/send-to-server local-events'') - (go-loop (state/handle-event ( event (select-keys [:_id :grub]) + (assoc :_id (str "grub-" (.now js/Date))) (assoc :completed false))] (swap! grubs (fn [current] (conj current grub))))) @@ -46,4 +51,7 @@ (defmethod handle-event :unknown-event [event] (logs "Cannot handle unknown event:" event)) +(defn handle-incoming-events [] + (go-loop (handle-event (> grubs (filter-chan #(not (empty? %))) - (map-chan (fn [g] {:event :create :grub g :_id (str "grub-" (.now js/Date))}))))) + (map-chan (fn [g] {:event :create :grub g}))))) (defn get-completed-event [event] (let [target (.-target event)