Initial Commit
This commit is contained in:
13
src/cljs/emptyhead/test/fixtures.cljs
Normal file
13
src/cljs/emptyhead/test/fixtures.cljs
Normal file
@@ -0,0 +1,13 @@
|
||||
(ns emptyhead.test.fixtures
|
||||
(:require [cljs.test :as t :include-macros true]
|
||||
[emptyhead.idea.state :as s]))
|
||||
|
||||
(defn temporary-state [tests]
|
||||
(let [orig-state (swap! s/state identity)]
|
||||
(s/reset-state!)
|
||||
(tests)
|
||||
(reset! s/state orig-state)))
|
||||
|
||||
(defn pre-reset [tests]
|
||||
(s/reset-state!)
|
||||
(tests))
|
||||
30
src/cljs/emptyhead/test/idea/crud.cljs
Normal file
30
src/cljs/emptyhead/test/idea/crud.cljs
Normal file
@@ -0,0 +1,30 @@
|
||||
(ns emptyhead.test.idea.crud
|
||||
(:require [cljs.test :as t :include-macros true]
|
||||
[emptyhead.idea.crud :as crud]
|
||||
[emptyhead.idea.state :as s :refer [state]]
|
||||
[emptyhead.idea.protocol :as prtc]
|
||||
[emptyhead.test.fixtures :as fx]))
|
||||
|
||||
(t/use-fixtures :once fx/temporary-state)
|
||||
(t/use-fixtures :each fx/pre-reset)
|
||||
|
||||
(t/deftest have-idea!
|
||||
(t/testing "Idea creation"
|
||||
(let [idea (crud/have-idea!)]
|
||||
(t/is (get @state idea)))))
|
||||
|
||||
(t/deftest idea-updating
|
||||
(t/testing "Idea updating"
|
||||
(let [idea (crud/have-idea!)]
|
||||
(crud/extend-idea! idea {:foo :bar})
|
||||
(t/is (= :bar (prtc/val-fn :foo idea)))
|
||||
(crud/swap-idea! idea {:baz :quux})
|
||||
(t/is (not (prtc/val-fn :foo idea)))
|
||||
(t/is (= :quux (prtc/val-fn :baz idea)))
|
||||
)))
|
||||
|
||||
(t/deftest forget!
|
||||
(t/testing "Idea deletion"
|
||||
(let [idea (crud/have-idea!)]
|
||||
(crud/forget-idea! idea)
|
||||
(t/is (= nil (get @state idea))))))
|
||||
25
src/cljs/emptyhead/test/idea/property.cljs
Normal file
25
src/cljs/emptyhead/test/idea/property.cljs
Normal file
@@ -0,0 +1,25 @@
|
||||
(ns emptyhead.test.idea.property
|
||||
(:require [cljs.test :as t :include-macros true]
|
||||
[emptyhead.idea.crud :as crud]
|
||||
[emptyhead.idea.property :as prop]
|
||||
[emptyhead.test.fixtures :as fx]))
|
||||
|
||||
(t/use-fixtures :once fx/temporary-state)
|
||||
(t/use-fixtures :each fx/pre-reset)
|
||||
|
||||
(t/deftest properties
|
||||
(t/testing "Property addition and removal"
|
||||
(let [idea (crud/have-idea!)]
|
||||
(prop/register-property! idea [:a :b :c] [:a :d :e])
|
||||
|
||||
(t/is (= #{[:a :b :c] [:a :d :e] [:a] [:a :b] [:a :d]}
|
||||
(prop/properties idea)))
|
||||
|
||||
(t/is (prop/has-property? idea [:a :b]))
|
||||
(t/is (not (prop/has-property? idea [:invalid])))
|
||||
|
||||
(t/is (contains? (prop/with-property [:a :d]) idea))
|
||||
(t/is (not (contains? (prop/with-property [:invalid]) idea)))
|
||||
|
||||
(prop/remove-property! idea [:a :b])
|
||||
(t/is (not (prop/has-property? idea [:a :b :c]))))))
|
||||
22
src/cljs/emptyhead/test/idea/protocol.cljs
Normal file
22
src/cljs/emptyhead/test/idea/protocol.cljs
Normal file
@@ -0,0 +1,22 @@
|
||||
(ns emptyhead.test.idea.protocol
|
||||
(:require [cljs.test :as t :include-macros true]
|
||||
[emptyhead.idea.crud :as crud]
|
||||
[emptyhead.idea.protocol :as prtc]
|
||||
[emptyhead.test.utils :refer [expect-error]]
|
||||
[emptyhead.test.fixtures :as fx]))
|
||||
|
||||
(t/use-fixtures :once fx/temporary-state)
|
||||
(t/use-fixtures :each fx/pre-reset)
|
||||
|
||||
(t/deftest protocol
|
||||
(t/testing "Value/reference semantics"
|
||||
(let [idea (crud/have-idea!)]
|
||||
;; Repeatedly going between value and reference shouldn't mangle idea
|
||||
(t/is (= idea (-> idea prtc/value prtc/reference)))
|
||||
(t/is (= (prtc/value idea) (-> idea prtc/reference prtc/value)))
|
||||
|
||||
;; Attempting to get a reference to a copy should be an error
|
||||
(expect-error :stale-reference #(prtc/reference (prtc/copy idea)))
|
||||
|
||||
;; Attempting to get a reference to an invalid idea should be an error
|
||||
(expect-error :invalid-reference #(prtc/reference {})))))
|
||||
8
src/cljs/emptyhead/test/main.cljs
Normal file
8
src/cljs/emptyhead/test/main.cljs
Normal file
@@ -0,0 +1,8 @@
|
||||
(ns emptyhead.test.main
|
||||
(:require [cljs.test :as t :include-macros true]
|
||||
[emptyhead.test.idea.crud]
|
||||
[emptyhead.test.idea.property]
|
||||
[emptyhead.test.idea.protocol]))
|
||||
|
||||
(defn run []
|
||||
(t/run-all-tests #"emptyhead\.test\..*"))
|
||||
10
src/cljs/emptyhead/test/utils.cljs
Normal file
10
src/cljs/emptyhead/test/utils.cljs
Normal file
@@ -0,0 +1,10 @@
|
||||
(ns emptyhead.test.utils
|
||||
(:require [cljs.test :as t :include-macros true]))
|
||||
|
||||
(defn expect-error [type fun]
|
||||
(t/is
|
||||
(= type
|
||||
(try (fun)
|
||||
nil
|
||||
(catch :default e
|
||||
(-> e ex-data :data first :type))))))
|
||||
Reference in New Issue
Block a user