Test state management on client

This commit is contained in:
Nicholas Kariniemi 2013-08-17 19:29:29 +03:00
parent ad02da6c88
commit f0c352b598
5 changed files with 73 additions and 17 deletions

View file

@ -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)))

58
spec/cljs/state_spec.cljs Normal file
View file

@ -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)))))

View file

@ -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 (<! events)))))
events (a/fan-in [local-events' remote-events])]
(a/do-chan! ws/send-to-server local-events'')
(a/copy-chan state/incoming-events events)))
(defn init []
(view/render-body)

View file

@ -1,5 +1,9 @@
(ns grub.state
(:require-macros [grub.macros :refer [log logs]]))
(:require [cljs.core.async :refer [chan <!]])
(:require-macros [grub.macros :refer [log logs go-loop]]
[cljs.core.async.macros :refer [go]]))
(def incoming-events (chan))
(def grubs (atom []))
@ -21,6 +25,7 @@
(defmethod handle-event :create [event]
(let [grub (-> 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 (<! incoming-events))))
(handle-incoming-events)

View file

@ -67,7 +67,7 @@
(get-grubs-from-enter)])]
(->> 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)