Skip to content
Permalink
b3a4236922
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
404 lines (351 sloc) 11.8 KB
(ns ulysses.utils-test
(:require [cljs.test :refer-macros [deftest testing are is]]
[ulysses.utils :as utils]))
(deftest noop
(are [x] (nil? (apply utils/noop x))
[] [1] [1 2]))
(deftest boolean?
(testing "yes"
(are [y] (utils/boolean? y)
true
false))
(testing "no"
(are [y] (not (utils/boolean? y))
nil "" 0 "true" "false")))
(deftest lazy-seq?
(testing "yes"
(are [y] (utils/lazy-seq? y)
(lazy-seq)
(map integer? [1 2 3])))
(testing "no"
(are [y] (not (utils/lazy-seq? y))
(list) (vector) (hash-map) nil "")))
(deftest objectifiable?
(testing "yes"
(are [y] (utils/objectifiable? y)
#js {}
#js {"foo" "bar"}
["clojure" "too"]))
(testing "no"
(are [y] (not (utils/objectifiable? y))
nil 3 4)))
; missing: ptr
; missing: ptr-first
(deftest 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 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 hyphenate-keys
(is (= {:a-b 1 :c-d 2} (utils/hyphenate-keys {:a_b 1 :c_d 2}))))
(deftest 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 str->int
(testing "successful cases"
(are [x y] (= x (utils/str->int y))
0 "0"
-5 "-5"
1 "1"
20003018 "20003018"
0 0
-5 -5
1 1
20003018 20003018))
(testing "unsuccessful cases"
(are [x] (nil? (utils/str->int x))
"" "0.0" "1.0" "123.123"
"garbage" "12moregarbage12"
123.123 nil)))
(deftest clj->json
(are [x y] (= x (utils/clj->json y))
"null" nil
"true" true
"-3" -3
"[1,2,3]" [1 2 3]
"[\"one\",\"three\",\"two\"]" (sorted-set :one :two :three)))
(deftest json->clj
(testing "basic values"
(are [x y] (= x (utils/json->clj y))
nil "null"
true "true"
-3 "-3"
[1 2 3] "[1,2,3]"
["one" "two" "three"] "[\"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"
(are [x] (thrown? js/SyntaxError (utils/json->clj x))
"asdf" "[1,2")))
(deftest url-encode-component
(are [x y] (= x (utils/url-encode-component y))
"foo" "foo"
"foo%20bar" "foo bar"
"%3F%26%3F%26" "?&?&"))
(deftest 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 into-maybe
(is (= (list 1) (utils/into-maybe (list) [1])))
(is (= (lazy-seq (list 1 2 3)) (utils/into-maybe (list) [1 2 3])))
(is (= #{1} (utils/into-maybe #{1} [1])))
(is (= #{1 2} (utils/into-maybe #{3} #{1 2}))))
(deftest by-id
(are [x y] (= x (utils/by-id y))
{} []
{1 {:id 1}} [{:id 1}]
{1 {:id 1} 2 {:id 2}} [{:id 1} {:id 2}]
{1 {:id 1} 2 {:id 2}} [{:id 1} {:id 2 :old true} {:id 2}]))
(deftest find-by-id
(let [ms [{:id 1} {:id 2} {:id 94}]]
(testing "found (type int)"
(are [pf y] (= (pf ms) (utils/find-by-id y ms))
first 1
second 2
last 94))
(testing "found (type int-string)"
(are [pf y] (= (pf ms) (utils/find-by-id y ms))
first "1"
second "2"
last "94"))
(testing "not found"
(are [x] (nil? (utils/find-by-id x ms))
33 "33" "1.0" nil))))
(deftest merge-by-id
;; ih test util, ex: (ih [1 2 3]) => [{:id 1} {:id 2} {:id 3}]
(let [ih (fn [coll]
((if (list? coll) identity (partial into (empty coll)))
(map #(if (map? %) % (hash-map :id %)) coll)))]
(testing "two seq args"
(are [x a b] (= x (utils/merge-by-id (ih a) (ih b)))
[] [] []
[] [] (list)
[{:id 0} {:id 1} {:id 2}] [0] [1 2]
#{{:id 0} {:id 1} {:id 2}} #{0} #{1 2}
#{{:id 0} {:id 1} {:id 2}} #{0} [1 2]
(list {:id 0} {:id 1 :a true} {:id 2} {:id 3}) (list 0 {:id 1 :a false} 2) [{:id 1 :a true} 2 3]))
(testing "many seq args"
(are [x many] (= x (apply utils/merge-by-id (map ih many)))
[] [[] [] []]
[] [[] (list) (list)]
[{:id 0}] [[] [0] [] [0]]
#{{:id 0} {:id 1} {:id 2}} [#{0} #{1 2} #{0 2 1}]
#{{:id 0} {:id 1} {:id 2 :a true}} [#{0} [1 2] #{{:id 2 :a true}}]
(list {:id 0} {:id 1} {:id 2 :a true} {:id 3}) [(list 0 1 2) [1 {:id 2 :a true} 3] []]))))
(deftest remove-by-id
(are [x id y] (= x (utils/remove-by-id id y))
[{:id 1} {:id 2} {:id 3}] 85 [{:id 1} {:id 2} {:id 3}]
#{{:id 1} {:id 2} {:id 3}} 85 #{{:id 1} {:id 2} {:id 3}}
[{:id 1} {:id 3}] 2 [{:id 1} {:id 2} {:id 3}]
[{:id 1} {:id 3}] "2" [{:id 1} {:id 2} {:id 3}]))
(deftest new-id
(are [x y] (= x (utils/new-id y))
1 (list)
2 (list {:id 1})
3 (list {:id 1} {:id 2})
40 (list {:id 1} {:id 39} {:id 4})))
(deftest map-lookup
(are [x m ids] (= x (utils/map-lookup m ids))
(list) {} (list)
(vector) {} (vector)
[] {2 {:id 2}} [1]
[{:id 1}] {1 {:id 1} 2 {:id 2}} [1]
[{:id 1} {:id 2}] {1 {:id 1} 2 {:id 2}} [1 2]
[{:id 1} {:id 2}] {1 {:id 1} 2 {:id 2}} [1 2 3]))
(deftest valid-request-id?
(testing "yes"
(are [x] (utils/valid-request-id? x)
1 5 "1" "5" "12345"))
(testing "no"
(are [x] (not (utils/valid-request-id? x))
0 -5 "0" "-5" nil [] "")))
; missing: throttle
; missing: debounce
; missing: debounce-standard
(deftest wop
(testing "is objectifiable"
(are [x y prop] (= x (utils/wop y prop))
1 #js {"ok" 1} "ok"
1 #js {"ok" 1} :ok
nil #js {"ok" 1} "missing"
nil #js {"ok" 1} :missing
nil {"ok" 1} :missing
nil :foo-bar :missing))
(testing "is not objectifiable"
(are [y prop] (nil? (utils/wop y prop))
3 :foo
nil :foo
true :foo)))
(deftest classes-vector
;; need to test using set since the order of maps is not guarenteed
(are [x args] (= (set x) (set (apply utils/classes-vector args)))
[] nil
[] []
[] [nil]
["foo"] ["foo"]
["foo"] [:foo]
["foo"] ['foo]
["100"] [100]
["foo"] [["foo"]]
["foo"] [#{"foo"}]
["foo"] [[:foo]]
["foo"] [[[[:foo]]]]
["foo"] [{:foo true}]
[] [{:foo false}]
["foo" "bar"] [:foo :bar]
["foo"] [{:foo true :bar false}]
["foo" "bar"] [{:foo true [:bar] true}]
["foo" "bar"] [{:foo true [{:bar true}] true}]
["foo" "bar"] [{:foo true [{["bar"] true}] true}]
["foo"] [{:foo true [{:bar false}] true}]
["btn" "btn-sm" "btn-primary"] [:btn [:btn-sm {:btn-primary true}]]
["btn" "btn-sm"] [:btn [:btn-sm {:btn-primary false}]]))
(deftest classes
;; most functionality is tested already in classes-vector
;; also, can only test using single classes when there are maps
;; since order of maps is not guarenteed
(are [x args] (= x (apply utils/classes args))
"" nil
"" []
"" [nil]
"foo" ["foo"]
"foo" [:foo]
"foo" ['foo]
"100" [100]
"foo" [["foo"]]
"foo" [#{"foo"}]
"foo" [[:foo]]
"foo" [[[[:foo]]]]
"foo" [{:foo true}]
"" [{:foo false}]
"foo bar" [:foo :bar]
"foo bar" [:foo {:bar true}]
"foo" [{:foo true :bar false}]
"foo" [{:foo true [{:bar false}] true}]))
(deftest classes-attr
;; most functionality is tested already in classes-vector and classes
;; also, can only test using single classes when there are maps
;; since order of maps is not guarenteed
(are [x args] (= {:class x} (apply utils/classes-attr args))
"" nil
"" []
"" [nil]
"foo" ["foo"]
"foo" [:foo]
"foo" ['foo]
"100" [100]
"foo" [["foo"]]
"foo" [#{"foo"}]
"foo" [[:foo]]
"foo" [[[[:foo]]]]
"foo" [{:foo true}]
"" [{:foo false}]
"foo bar" [:foo :bar]
"foo bar" [:foo {:bar true}]
"foo" [{:foo true :bar false}]
"foo" [{:foo true [{:bar false}] true}]))
(deftest btn-classes
;; most functionality is tested already in classes-vector and classes
;; also, can only test using single classes when there are maps
;; since order of maps is not guarenteed
(are [x args] (= x (apply utils/btn-classes args))
"btn" nil
"btn" []
"btn btn-sm" [:sm]
"btn btn-sm" [:sm {:primary false}]
"btn btn-sm btn-primary" [:sm {:primary true}]))
; missing: d-p
; missing: window-size
; missing: document-size
; missing: window-scroll-position
(deftest mechanism-to-contextual
(are [x y] (= x (utils/mechanism-to-contextual y))
:info :nsf
:success :nih
nil :not-found))
(deftest update-db-pagination
(are [x db response] (= x (utils/update-db-pagination db response))
;; test 1
{:page-current 2 :page-last 3}
{:page-current 1 :page-last 1}
{:current_page 2 :last_page 3}
;; test 2
{:foo :bar :page-current 1 :page-last 3}
{:foo :bar :page-current 1 :page-last 1}
{:foo :asdf :current_page 1 :last_page 3}))
; missing: faculties-pool-params
(deftest metric-maxes
(let [metrics {:publicationCount 0
:grantCount 0
:grantFunds 0
:recentGrantCount 0
:recentGrantFunds 0}
metrics-v1 (assoc metrics :grantCount 0.9)
metrics-v2 (assoc metrics :grantCount 1.2)
metrics-v3 (assoc metrics :grantFunds 43.9)]
(is (= metrics-v1
(utils/metric-maxes metrics [{:metrics metrics-v1}])))
(is (= metrics-v2
(utils/metric-maxes metrics [{:metrics metrics-v2}
{:metrics metrics-v1}])))
(is (= (assoc metrics-v2 :grantFunds (:grantFunds metrics-v3))
(utils/metric-maxes metrics [{:metrics metrics-v2}
{:metrics metrics-v3}
{:metrics metrics-v1}])))))
; missing: verify-user-poll-response
; missing: validate-builder-filters
(deftest parse-int-id
(are [x y] (= x (utils/parse-int-id y))
{:id 3} {:id "3"}
{:id 123} {:id "123"}
{:id 3} {:id 3}
{:id 123} {:id 123}
{:id nil} {:id "*"}))
; missing: clean-incoming-grant-op
; missing: clean-incoming-grant-ops
; missing: request
; missing: request-grant-ops-channel
; missing: debounce-request-grant-ops
; missing: request-faculties-pool-channel
; missing: debounce-request-faculties-pool
; missing: request-workspace-save-channel
; missing: debounce-request-workspace-save
; missing: scroll-to