|
1 | 1 | (ns cljss.core-test |
2 | 2 | (:require [clojure.test :refer :all] |
3 | 3 | [cljss.core :refer :all] |
| 4 | + [cljss.rum :refer :all] |
4 | 5 | [cljss.builder :refer :all] |
5 | 6 | [cljss.font-face :as ff] |
6 | 7 | [cljss.inject-global :as ig])) |
7 | 8 |
|
8 | | -(def basic-styles {:color "red"}) |
| 9 | +(deftest test-styles-builder |
| 10 | + (testing "static" |
| 11 | + (is (= (build-styles "cls" {:color "#fff"}) |
| 12 | + ["cls" [".cls{color:#fff;}"] []]))) |
9 | 13 |
|
10 | | -(def dynamic-styles {:background-color #(:bg %) |
11 | | - :margin #(if (:large %) "10px" "5px")}) |
| 14 | + (testing "pseudos" |
| 15 | + (is (= (build-styles "cls" {:&:hover {:color "red"}}) |
| 16 | + ["cls" [".cls{}" ".cls:hover{color:red;}"] []]))) |
12 | 17 |
|
13 | | -(def pseudo-styles (merge basic-styles {:&:hover {:color "blue"}})) |
| 18 | + (testing "string selector" |
| 19 | + (is (= (build-styles "cls" {"a" {:color "blue"}}) |
| 20 | + ["cls" [".cls{}" ".cls a{color:blue;}"] []]))) |
14 | 21 |
|
15 | | -(def pseudo-dynamic-styles |
16 | | - (assoc |
17 | | - pseudo-styles |
18 | | - :&:active |
19 | | - {:color #(:active-color %)})) |
| 22 | + (testing "dynamic" |
| 23 | + (is (= (build-styles "cls" {:color 'x}) |
| 24 | + '["cls" [".cls{color:var(--var-cls-0);}"] [["--var-cls-0" x]]]))) |
20 | 25 |
|
21 | | -(def complete-styles |
22 | | - (merge |
23 | | - basic-styles |
24 | | - dynamic-styles |
25 | | - pseudo-dynamic-styles)) |
26 | | - |
27 | | -;;; unique ids for style classes are generated |
28 | | -;;; from the hashed set of non pseudo styles |
29 | | -;;; this function replicates initialization |
30 | | -;;; of styles in cljss.builder/build-styles |
31 | | -;;; and produces the same hashed value used in |
32 | | -;;; style ids |
33 | | -(defn remove-pseudo [styles] |
34 | | - (filterv |
35 | | - (comp not #'cljss.builder/pseudo?) |
36 | | - styles)) |
37 | | - |
38 | | -(deftest test-build-styles |
39 | | - (testing "building basic styles" |
40 | | - (let [[[id static vals]] (build-styles "test" basic-styles)] |
41 | | - (is (= "test" id)) |
42 | | - (is (= ".test{color:red;}" static)) |
43 | | - (is (empty? vals)))) |
44 | | - |
45 | | - (testing "building dynamic styles" |
46 | | - (let [[[id static vals]] (build-styles "test" dynamic-styles)] |
47 | | - (is (= "test" id)) |
48 | | - (is (= ".test{background-color:var(--var-test-0);margin:var(--var-test-1);}" static)) |
49 | | - (is (= [["--var-test-0" (:background-color dynamic-styles)] ["--var-test-1" (:margin dynamic-styles)]] vals)))) |
50 | | - |
51 | | - (testing "building basic pseudo styles" |
52 | | - (let [result (build-styles "test" pseudo-styles)] |
53 | | - (is (= result |
54 | | - [["test" ".test{color:red;}" []] |
55 | | - [".test:hover" ".test:hover{color:blue;}" []]])))) |
56 | | - |
57 | | - (testing "building dynamic pseudo styles" |
58 | | - (let [result (build-styles "test" pseudo-dynamic-styles)] |
59 | | - (is (= result |
60 | | - [["test" |
61 | | - ".test{color:red;}" |
62 | | - [["--var-test-1" (get-in pseudo-dynamic-styles [:&:active :color])]]] |
63 | | - [".test:hover" ".test:hover{color:blue;}" []] |
64 | | - [".test:active" ".test:active{color:var(--var-test-1);}" []]])))) |
65 | | - |
66 | | - (testing "building a complete set of styles" |
67 | | - (let [result (build-styles "test" complete-styles)] |
68 | | - (is (= result |
69 | | - [["test" |
70 | | - ".test{color:red;background-color:var(--var-test-0);margin:var(--var-test-1);}" |
71 | | - [["--var-test-0" (:background-color complete-styles)] |
72 | | - ["--var-test-1" (:margin complete-styles)] |
73 | | - ["--var-test-3" (get-in complete-styles [:&:active :color])]]] |
74 | | - [".test:hover" ".test:hover{color:blue;}" []] |
75 | | - [".test:active" ".test:active{color:var(--var-test-3);}" []]]))))) |
| 26 | + (testing "dynamic pseudos" |
| 27 | + (is (= (build-styles "cls" {:&:hover {:color 'x}}) |
| 28 | + '["cls" [".cls{}" ".cls:hover{color:var(--var-cls-0);}"] [["--var-cls-0" x]]])))) |
76 | 29 |
|
77 | 30 | (deftest test-font-face |
78 | 31 | (testing "build @font-face" |
|
0 commit comments