diff --git a/src/cljs/ulysses/db.cljs b/src/cljs/ulysses/db.cljs index e90e894..21740d3 100644 --- a/src/cljs/ulysses/db.cljs +++ b/src/cljs/ulysses/db.cljs @@ -1,5 +1,4 @@ (ns ulysses.db - (:require-macros [cljs.test :refer [deftest testing are is]]) (:require [ulysses.utils :refer [document-size window-size window-scroll-position]])) (def default-db @@ -104,13 +103,3 @@ "given a db, set the given keys to their original state" [db keys] (merge db (select-keys default-db keys))) - -;; ---------------------------------------------------------------------------- -;; tests -;; ---------------------------------------------------------------------------- - -(deftest test-refresh-keys - (testing "refresh-keys" - (are [keys db] (= default-db (refresh-keys db keys)) - [:active-page] (assoc default-db :active-page :builder) - [:page-current] (assoc default-db :page-current 2)))) diff --git a/src/cljs/ulysses/utils.cljs b/src/cljs/ulysses/utils.cljs index 05faa7f..399e75a 100644 --- a/src/cljs/ulysses/utils.cljs +++ b/src/cljs/ulysses/utils.cljs @@ -1,6 +1,5 @@ (ns ulysses.utils - (:require-macros [cljs.test :refer [deftest testing are is]] - [cljs.core.async.macros :refer [go-loop]]) + (:require-macros [cljs.core.async.macros :refer [go-loop]]) (:require [clojure.string :as string] [clojure.walk :refer [keywordize-keys]] [clojure.set :refer [rename-keys]] @@ -531,136 +530,3 @@ (defn scroll-to [y] (.scrollTo js/window 0 y)) - -;; ---------------------------------------------------------------------------- -;; tests -;; ---------------------------------------------------------------------------- - -(deftest test-noop - (is (nil? (noop))) - (is (nil? (noop 1))) - (is (nil? (noop 1 2)))) - -(deftest test-boolean? - (is (= true (boolean? true))) - (is (= true (boolean? false))) - (is (not (boolean? nil))) - (is (not (boolean? ""))) - (is (not (boolean? 0)))) - -; missing: ptr - -; missing: ptr-first - -(deftest test-metas-equal? - (is (metas-equal? [(with-meta {:a 1} {:key 1})] [(with-meta {:a 2} {:key 1})])) - (is (not (metas-equal? [(with-meta {:a 1} {:key 1})] [(with-meta {:a 2} {:key 2})])))) - -(deftest test-remap-and-variations - (let [from {:a 1 :b 2 :c 3}] - (testing "remap" - (is (= {:a 2 :b 4 :c 6} (remap (partial * 2) from)))) - (testing "remap-k" - (is (= {:a1 1 :b2 2 :c3 3} - (remap-k - (fn [k v] (keyword (str (name k) v))) - from)))) - (testing "remap-v" - (is (= {:a :a1 :b :b2 :c :c3} - (remap-v - (fn [k v] (keyword (str (name k) v))) - from)))))) - -(deftest test-hyphenate-keys - (is (= {:a-b 1 :c-d 2} (hyphenate-keys {:a_b 1 :c_d 2})))) - -(deftest test-map-subels - (testing "when values have ids" - (let [in [{:id 23 :value 3} {:id 95 :value 4}] - out (map-subels :div in) - expected (list (with-meta [:div (first in)] {:key 23}) - (with-meta [:div (second in)] {:key 95}))] - (is (= out expected)) - (is (metas-equal? out expected)))) - (testing "when values don't have ids" - (let [in [3 4] - out (map-subels :div in) - expected (list (with-meta [:div (first in)] {:key "default-key-0"}) - (with-meta [:div (second in)] {:key "default-key-1"}))] - (is (= out expected)) - (is (metas-equal? out expected)))) - (testing "when values have ids sometimes" - (let [in [{:id 23 :value 3} 4] - out (map-subels :div in) - expected (list (with-meta [:div (first in)] {:key 23}) - (with-meta [:div (second in)] {:key "default-key-1"}))] - (is (= out expected)) - (is (metas-equal? out expected))))) - -(deftest test-str->int - (testing "successful cases" - (is (= 0 (str->int "0"))) - (is (= -5 (str->int "-5"))) - (is (= 1 (str->int "1"))) - (is (= 20003018 (str->int "20003018"))) - (is (= 0 (str->int 0))) - (is (= -5 (str->int -5))) - (is (= 1 (str->int 1))) - (is (= 20003018 (str->int 20003018)))) - (testing "unsuccessful cases" - (is (nil? (str->int ""))) - (is (nil? (str->int "0.0"))) - (is (nil? (str->int "1.0"))) - (is (nil? (str->int "123.123"))) - (is (nil? (str->int "garbage"))) - (is (nil? (str->int "12moregarbage12"))) - (is (nil? (str->int 123.123))) - (is (nil? (str->int nil))))) - -(deftest test-clj->json - (is (= "null" (clj->json nil))) - (is (= "true" (clj->json true))) - (is (= "-3" (clj->json -3))) - (is (= "[1,2,3]" (clj->json [1 2 3]))) - (is (= "[\"one\",\"three\",\"two\"]" (clj->json (sorted-set :one :two :three))))) - -(deftest test-json->clj - (testing "basic values" - (is (= nil (json->clj "null"))) - (is (= true (json->clj "true"))) - (is (= -3 (json->clj "-3"))) - (is (= [1 2 3] (json->clj "[1,2,3]"))) - (is (= ["one" "two" "three"] (json->clj "[\"one\",\"two\",\"three\"]")))) - (testing "maps: no keywordizing" - (is (= {"a" 1 "b" 2} (json->clj "{\"a\": 1, \"b\": 2}")))) - (testing "maps: keywordizing" - (is (= {:a 1 :b 2} (json->clj "{\"a\": 1, \"b\": 2}" :keywords)))) - (testing "exception throwing" - (is (thrown? js/SyntaxError (json->clj "asdf"))) - (is (thrown? js/SyntaxError (json->clj "[1,2"))))) - -(deftest test-url-encode-component - (is (= "foo" (url-encode-component "foo"))) - (is (= "foo%20bar" (url-encode-component "foo bar"))) - (is (= "%3F%26%3F%26" (url-encode-component "?&?&")))) - -(deftest test-truncate-with-ellipsis - (testing "normal" - (is (= "foo..." (truncate-with-ellipsis "foo bar" 3)))) - (testing "custom ellipsis" - (is (= "foo[..]" (truncate-with-ellipsis "foo bar" 3 "[..]"))))) - -(deftest test-find-by-id - (let [ms [{:id 1} {:id 2} {:id 94}]] - (testing "found (type int)" - (is (= (first ms) (find-by-id 1 ms))) - (is (= (second ms) (find-by-id 2 ms))) - (is (= (last ms) (find-by-id 94 ms)))) - (testing "found (type int-string)" - (is (= (first ms) (find-by-id "1" ms))) - (is (= (second ms) (find-by-id "2" ms))) - (is (= (last ms) (find-by-id "94" ms)))) - (testing "not found" - (is (nil? (find-by-id 33 ms))) - (is (nil? (find-by-id "33" ms))) - (is (nil? (find-by-id "1.0" ms)))))) diff --git a/src/js/extra.js b/src/js/extra.js index d14d8bc..d46b0bf 100644 --- a/src/js/extra.js +++ b/src/js/extra.js @@ -48,20 +48,20 @@ ulysses.extra = (function() { }, fit: function(blocks) { - var n, node, block, final; + var n, node, block, packed; - final = []; + packed = []; for (n = 0; n < blocks.length; n++) { block = blocks[n]; if (node = this.findNode(this.root, block.w, block.h)) { block.fit = this.splitNode(node, block.w, block.h); - final.push(block); + packed.push(block); } } - return final; + return packed; }, findNode: function(root, w, h) { diff --git a/test/cljs/ulysses/core_test.cljs b/test/cljs/ulysses/core_test.cljs index e7996a1..c419e03 100644 --- a/test/cljs/ulysses/core_test.cljs +++ b/test/cljs/ulysses/core_test.cljs @@ -1,11 +1,5 @@ (ns ulysses.core-test - (:require [cljs.test :refer-macros [deftest testing is]] - [ulysses.core :as core])) - -;; NOTE -;; runner.cljs is currently set up to run tests in -;; all namespaces that match ulysses.*, which mostly -;; voids the need for this auxillary test directory + (:require [cljs.test :refer-macros [deftest testing are is]])) (deftest fake-test (testing "description" diff --git a/test/cljs/ulysses/db_test.cljs b/test/cljs/ulysses/db_test.cljs new file mode 100644 index 0000000..53da4e7 --- /dev/null +++ b/test/cljs/ulysses/db_test.cljs @@ -0,0 +1,9 @@ +(ns ulysses.db-test + (:require [cljs.test :refer-macros [deftest testing are is]] + [ulysses.db :as db])) + +(deftest test-refresh-keys + (testing "refresh-keys" + (are [keys db-state] (= db/default-db (db/refresh-keys db-state keys)) + [:active-page] (assoc db/default-db :active-page :builder) + [:page-current] (assoc db/default-db :page-current 2)))) diff --git a/test/cljs/ulysses/runner.cljs b/test/cljs/ulysses/runner.cljs index 24bc36b..3385c49 100644 --- a/test/cljs/ulysses/runner.cljs +++ b/test/cljs/ulysses/runner.cljs @@ -1,6 +1,8 @@ (ns ulysses.runner - (:require [doo.runner :refer-macros [doo-tests doo-all-tests]] - [ulysses.core-test])) + (:require [doo.runner :refer-macros [doo-all-tests]] + [ulysses.core-test] + [ulysses.utils-test] + [ulysses.db-test])) ;; run all tests in all namespaces matching ulysses.* -(doo.runner/doo-all-tests #"ulysses.*") +(doo-all-tests #"ulysses.*") diff --git a/test/cljs/ulysses/utils_test.cljs b/test/cljs/ulysses/utils_test.cljs new file mode 100644 index 0000000..f2c7201 --- /dev/null +++ b/test/cljs/ulysses/utils_test.cljs @@ -0,0 +1,134 @@ +(ns ulysses.utils-test + (:require [cljs.test :refer-macros [deftest testing are is]] + [ulysses.utils :as utils])) + +(deftest test-noop + (is (nil? (utils/noop))) + (is (nil? (utils/noop 1))) + (is (nil? (utils/noop 1 2)))) + +(deftest test-boolean? + (is (= true (utils/boolean? true))) + (is (= true (utils/boolean? false))) + (is (not (utils/boolean? nil))) + (is (not (utils/boolean? ""))) + (is (not (utils/boolean? 0)))) + +; missing: ptr + +; missing: ptr-first + +(deftest test-metas-equal? + (is (utils/metas-equal? [(with-meta {:a 1} {:key 1})] + [(with-meta {:a 2} {:key 1})])) + (is (not (utils/metas-equal? [(with-meta {:a 1} {:key 1})] + [(with-meta {:a 2} {:key 2})])))) + +(deftest test-remap-and-variations + (let [from {:a 1 :b 2 :c 3}] + (testing "remap" + (is (= {:a 2 :b 4 :c 6} (utils/remap (partial * 2) from)))) + (testing "remap-k" + (is (= {:a1 1 :b2 2 :c3 3} + (utils/remap-k + (fn [k v] (keyword (str (name k) v))) + from)))) + (testing "remap-v" + (is (= {:a :a1 :b :b2 :c :c3} + (utils/remap-v + (fn [k v] (keyword (str (name k) v))) + from)))))) + +(deftest test-hyphenate-keys + (is (= {:a-b 1 :c-d 2} (utils/hyphenate-keys {:a_b 1 :c_d 2})))) + +(deftest test-map-subels + (testing "when values have ids" + (let [in [{:id 23 :value 3} {:id 95 :value 4}] + out (utils/map-subels :div in) + expected (list (with-meta [:div (first in)] {:key 23}) + (with-meta [:div (second in)] {:key 95}))] + (is (= out expected)) + (is (utils/metas-equal? out expected)))) + (testing "when values don't have ids" + (let [in [3 4] + out (utils/map-subels :div in) + expected (list (with-meta [:div (first in)] {:key "default-key-0"}) + (with-meta [:div (second in)] {:key "default-key-1"}))] + (is (= out expected)) + (is (utils/metas-equal? out expected)))) + (testing "when values have ids sometimes" + (let [in [{:id 23 :value 3} 4] + out (utils/map-subels :div in) + expected (list (with-meta [:div (first in)] {:key 23}) + (with-meta [:div (second in)] {:key "default-key-1"}))] + (is (= out expected)) + (is (utils/metas-equal? out expected))))) + +(deftest test-str->int + (testing "successful cases" + (is (= 0 (utils/str->int "0"))) + (is (= -5 (utils/str->int "-5"))) + (is (= 1 (utils/str->int "1"))) + (is (= 20003018 (utils/str->int "20003018"))) + (is (= 0 (utils/str->int 0))) + (is (= -5 (utils/str->int -5))) + (is (= 1 (utils/str->int 1))) + (is (= 20003018 (utils/str->int 20003018)))) + (testing "unsuccessful cases" + (is (nil? (utils/str->int ""))) + (is (nil? (utils/str->int "0.0"))) + (is (nil? (utils/str->int "1.0"))) + (is (nil? (utils/str->int "123.123"))) + (is (nil? (utils/str->int "garbage"))) + (is (nil? (utils/str->int "12moregarbage12"))) + (is (nil? (utils/str->int 123.123))) + (is (nil? (utils/str->int nil))))) + +(deftest test-clj->json + (is (= "null" (utils/clj->json nil))) + (is (= "true" (utils/clj->json true))) + (is (= "-3" (utils/clj->json -3))) + (is (= "[1,2,3]" (utils/clj->json [1 2 3]))) + (is (= "[\"one\",\"three\",\"two\"]" (utils/clj->json (sorted-set :one :two :three))))) + +(deftest test-json->clj + (testing "basic values" + (is (= nil (utils/json->clj "null"))) + (is (= true (utils/json->clj "true"))) + (is (= -3 (utils/json->clj "-3"))) + (is (= [1 2 3] (utils/json->clj "[1,2,3]"))) + (is (= ["one" "two" "three"] (utils/json->clj "[\"one\",\"two\",\"three\"]")))) + (testing "maps: no keywordizing" + (is (= {"a" 1 "b" 2} (utils/json->clj "{\"a\": 1, \"b\": 2}")))) + (testing "maps: keywordizing" + (is (= {:a 1 :b 2} (utils/json->clj "{\"a\": 1, \"b\": 2}" :keywords)))) + (testing "exception throwing" + (is (thrown? js/SyntaxError (utils/json->clj "asdf"))) + (is (thrown? js/SyntaxError (utils/json->clj "[1,2"))))) + +(deftest test-url-encode-component + (is (= "foo" (utils/url-encode-component "foo"))) + (is (= "foo%20bar" (utils/url-encode-component "foo bar"))) + (is (= "%3F%26%3F%26" (utils/url-encode-component "?&?&")))) + +(deftest test-truncate-with-ellipsis + (testing "normal" + (is (= "foo..." (utils/truncate-with-ellipsis "foo bar" 3)))) + (testing "custom ellipsis" + (is (= "foo[..]" (utils/truncate-with-ellipsis "foo bar" 3 "[..]"))))) + +(deftest test-find-by-id + (let [ms [{:id 1} {:id 2} {:id 94}]] + (testing "found (type int)" + (is (= (first ms) (utils/find-by-id 1 ms))) + (is (= (second ms) (utils/find-by-id 2 ms))) + (is (= (last ms) (utils/find-by-id 94 ms)))) + (testing "found (type int-string)" + (is (= (first ms) (utils/find-by-id "1" ms))) + (is (= (second ms) (utils/find-by-id "2" ms))) + (is (= (last ms) (utils/find-by-id "94" ms)))) + (testing "not found" + (is (nil? (utils/find-by-id 33 ms))) + (is (nil? (utils/find-by-id "33" ms))) + (is (nil? (utils/find-by-id "1.0" ms))))))