Skip to content

Commit

Permalink
Merge pull request #24 from brianium/build-styles-tests
Browse files Browse the repository at this point in the history
Adds tests for build-styles function
  • Loading branch information
roman01la authored Sep 25, 2017
2 parents cea186c + 9afca02 commit 90e5ade
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions test/cljss/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,68 @@
(:require [clojure.test :refer :all]
[cljss.core :refer :all]))

(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))
(def basic-styles {:color "red"})

(def dynamic-styles {:background-color #(:bg %)
:margin #(if (:large %) "10px" "5px")})

(def pseudo-styles (merge basic-styles {:&:hover {:color "blue"}}))

(def pseudo-dynamic-styles
(assoc
pseudo-styles
:&:active
{:color #(:active-color %)}))

(def complete-styles
(merge
basic-styles
dynamic-styles
pseudo-dynamic-styles))

;;; unique ids for style classes are generated
;;; from the hashed set of non pseudo styles
;;; this function replicates initialization
;;; of styles in cljss.core/build-styles
;;; and produces the same hashed value used in
;;; style ids
(defn remove-pseudo [styles]
(filterv
(comp not #'cljss.core/pseudo?)
styles))

(deftest test-build-styles
(testing "building basic styles"
(let [[static vals] (build-styles "test" basic-styles)]
(is (= (str ".test{color:red;}") static))
(is (empty? vals))))

(testing "building dynamic styles"
(let [[static vals] (build-styles "test" dynamic-styles)]
(is (= (str ".test{background-color:var(--var-test-0);margin:var(--var-test-1);}") static))
(is (= [["--var-test-0" (:background-color dynamic-styles)] ["--var-test-1" (:margin dynamic-styles)]] vals))))

(testing "building basic pseudo styles"
(let [[static vals] (build-styles "test" pseudo-styles)]
(is (= (str ".test{color:red;}.test:hover{color:blue;}") static))
(is (empty? vals))))

(testing "building dynamic psuedo styles"
(let [[static vals] (build-styles "test" pseudo-dynamic-styles)]
(is (= (-> ".test"
(str "{color:red;}")
(str ".test:hover{color:blue;}")
(str ".test:active{color:var(--var-test:active-0);}")) static))
(is (= [["--var-test:active-0" (get-in pseudo-dynamic-styles [:&:active :color])]] vals))))

(testing "building a complete set of styles"
(let [[static vals] (build-styles "test" complete-styles)]
(is (= (-> ".test"
(str "{color:red;")
(str "background-color:var(--var-test-0);")
(str "margin:var(--var-test-1);}")
(str ".test:hover{color:blue;}")
(str ".test:active{color:var(--var-test:active-2);}")) static))
(is (= [["--var-test-0" (:background-color complete-styles)]
["--var-test-1" (:margin complete-styles)]
["--var-test:active-2" (get-in complete-styles [:&:active :color])]] vals)))))

0 comments on commit 90e5ade

Please sign in to comment.