diff --git a/.gitignore b/.gitignore index 1dbeeb3..7ebe1b0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,13 +5,16 @@ public/build_no_opt public/test_*.js public/domina.js domina-tests +.lein-repl-history lib +out +dev-resources/public/js/*.js *.jar .repl pom.xml .lein-plugins .lein-failures -/target +target *.*~ \#*# .#* diff --git a/profiles.clj b/profiles.clj new file mode 100644 index 0000000..8d0b4e8 --- /dev/null +++ b/profiles.clj @@ -0,0 +1,35 @@ +{:dev {:test-paths ["test/cljs"] + :dependencies [[hiccups "0.2.0"]] + :plugins [[com.cemerick/clojurescript.test "0.2.0"]] + :cljsbuild {:builds {#_:none + #_{:source-paths ["src/cljs" "test/cljs"] + :compiler {:libs ["goog/dom/query.js"] + :pretty-print true + :optimizations :none + :output-to "dev-resources/public/js/none.js"}} + :whitespace + {:source-paths ["src/cljs" "test/cljs"] + :compiler {:libs ["goog/dom/query.js"] + :optimizations :whitespace + :pretty-print true + :output-to "dev-resources/public/js/whitespace.js"}} + #_:simple + #_{:source-paths ["src/cljs" "test/cljs"] + :compiler {:libs ["goog/dom/query.js"] + :optimizations :simple + :pretty-print true + :output-to "dev-resources/public/js/simple.js"}} + #_:advanced + #_{:source-paths ["src/cljs" "test/cljs"] + :compiler {:libs ["goog/dom/query.js"] + :optimizations :advanced + :pretty-print true + :output-to "dev-resources/public/js/advanced.js"}}} + :test-commands {"whitespace" + ["phantomjs" :runner "dev-resources/public/js/whitespace.js"] + + #_"simple" + #_["phantomjs" :runner "dev-resources/public/js/simple.js"] + + #_"advanced" + #_["phantomjs" :runner "dev-resources/public/js/advanced.js"]}}}} diff --git a/project.clj b/project.clj index a2d954c..74df297 100644 --- a/project.clj +++ b/project.clj @@ -1,47 +1,13 @@ (defproject domina "1.0.3-SNAPSHOT" :description "A DOM manipulation library for ClojureScript inspired by JQuery" - :source-paths ["src/cljs"] - :dependencies [[org.clojure/clojure "1.4.0"]] - :plugins [[lein-cljsbuild "0.3.0"] - [lein-clojars "0.9.1"]] + :source-paths ["src/clj" "src/cljs"] + :dependencies [[org.clojure/clojure "1.5.1"] + [org.clojure/clojurescript "0.0-2030"]] + :plugins [[lein-cljsbuild "1.0.0-alpha1"]] :hooks [leiningen.cljsbuild] - :cljsbuild {:builds [{:jar true + :cljsbuild {:builds {:deploy + {:source-paths ["src/cljs"] :compiler {:libs ["goog/dom/query.js"] :pretty-print true :output-dir ".cljsbuild/domina" - :output-to "public/domina.js"}} - {:source-paths ["test/cljs"] - :compiler {:libs ["goog/dom/query.js"] - :pretty-print true - :optimizations :none - :output-dir "public/build_no_opt" - :output-to "public/test_no_opt.js"}} - {:source-paths ["test/cljs"] - :compiler {:libs ["goog/dom/query.js"] - :optimizations :whitespace - :pretty-print true - :output-dir ".cljsbuild/whitespace" - :output-to "public/test_whitespace.js"}} - {:source-paths ["test/cljs"] - :compiler {:libs ["goog/dom/query.js"] - :optimizations :simple - :pretty-print true - :output-dir ".cljsbuild/simple" - :output-to "public/test_simple.js"}} - {:source-paths ["test/cljs"] - :compiler {:libs ["goog/dom/query.js"] - :optimizations :advanced - :pretty-print true - :output-dir ".cljsbuild/advanced" - :output-to "public/test_advanced.js"}}]} - :repositories {"sonatype-staging" - "https://oss.sonatype.org/content/groups/staging/"}) - -(comment - (do - (require '[cljs.repl :as repl]) - (require '[cljs.repl.browser :as browser]) - (def env (browser/repl-env)) - (repl/repl env)) - - ) + :output-to "public/domina.js"}}}}) diff --git a/src/cljs/domina/macros.clj b/src/clj/domina/macros.clj similarity index 100% rename from src/cljs/domina/macros.clj rename to src/clj/domina/macros.clj diff --git a/src/cljs/domina/events.cljs b/src/cljs/domina/events.cljs index 1db48f3..9dee1ef 100644 --- a/src/cljs/domina/events.cljs +++ b/src/cljs/domina/events.cljs @@ -1,4 +1,3 @@ - (ns domina.events (:require [domina :as domina] [goog.object :as gobj] diff --git a/test/cljs/domina/test.cljs b/temp/test/cljs/domina/test.cljs similarity index 95% rename from test/cljs/domina/test.cljs rename to temp/test/cljs/domina/test.cljs index 0ebe5e1..f5bc46c 100644 --- a/test/cljs/domina/test.cljs +++ b/temp/test/cljs/domina/test.cljs @@ -12,7 +12,7 @@ [domina.xpath :only [xpath]] [domina.css :only [sel]] [domina.events :only [listen! capture! listen-once! capture-once! - unlisten! dispatch-event! dispatch! unlisten-by-key! + unlisten! dispatch! unlisten-by-key! get-listeners prevent-default stop-propagation target current-target event-type raw-event]]) (:require [goog.events :as events] @@ -69,28 +69,28 @@ ;;;;;; CSS selection tests -(add-test "basic CSS selection" +#_(add-test "basic CSS selection" #(do (reset) (standard-fixture) (assert (= 3 (count (nodes (sel "p"))))))) -(add-test "basic CSS selection (single node)" +#_(add-test "basic CSS selection (single node)" #(do (reset) (standard-fixture) (assert (not (nil? (single-node (sel "p"))))))) -(add-test "CSS selection with class specification" +#_(add-test "CSS selection with class specification" #(do (reset) (standard-fixture) (assert (= 1 (count (nodes (sel ".d1"))))))) -(add-test "a relative CSS selector" +#_(add-test "a relative CSS selector" #(do (reset) (standard-fixture) (assert (= 3 (count (nodes (-> (sel ".d1") (sel "p")))))))) -(add-test "extended CSS chaining" +#_(add-test "extended CSS chaining" #(do (reset) (append! (sel "body") "

some text

more text

") @@ -101,28 +101,28 @@ ;;;;;; DOM Manipulation Tests -(add-test "basic xpath selection" +#_(add-test "basic xpath selection" #(do (reset) (standard-fixture) (assert (= 3 (count (nodes (xpath "//p"))))))) -(add-test "basic xpath selection (single node)" +#_(add-test "basic xpath selection (single node)" #(do (reset) (standard-fixture) (assert (not (nil? (single-node (xpath "//p"))))))) -(add-test "xpath selection with class specification" +#_(add-test "xpath selection with class specification" #(do (reset) (standard-fixture) (assert (= 1 (count (nodes (xpath "//div[@class='d1']"))))))) -(add-test "a relative xpath expression" +#_(add-test "a relative xpath expression" #(do (reset) (standard-fixture) (assert (= 3 (count (nodes (-> (xpath "//body/div[@class='d1']") (xpath "p")))))))) -(add-test "extended selection chaining" +#_(add-test "extended selection chaining" #(do (reset) (append! (xpath "//body") "

some text

more text

") @@ -130,17 +130,17 @@ (xpath "./div") (xpath "./p") (xpath "./span")))))))) -(add-test "advanced xpath" +#_(add-test "advanced xpath" #(do (reset) (standard-fixture) (assert (= 2 (count (nodes (xpath "//p[following-sibling::p[@class='p3']]"))))))) -(add-test "look up node by id" +#_(add-test "look up node by id" #(do (reset) (standard-fixture) (assert (= 1 (count (nodes (by-id "id1"))))))) -(add-test "look up node by id with context" +#_(add-test "look up node by id with context" #(do (reset) (append! (sel "body") "

some text

more text

") @@ -149,58 +149,58 @@ (sel "p") (sel "span")))))))) -(add-test "look up nodes by class" +#_(add-test "look up nodes by class" #(do (reset) (standard-fixture) (assert (= 1 (count (nodes (by-class "p3"))))))) -(add-test "look up multiple nodes by class" +#_(add-test "look up multiple nodes by class" #(do (reset) (append! (xpath "//body") "

test1

test2

") (assert (= 2 (count (nodes (by-class "tc"))))))) -(add-test "child selection" +#_(add-test "child selection" #(do (reset) (standard-fixture) (assert (= 3 (count (children (xpath "//div[@class='d1']"))))))) -(add-test "clone a single node" +#_(add-test "clone a single node" #(do (reset) (standard-fixture) (assert (= 1 (count (clone (single-node (xpath "//p")))))))) -(add-test "clone multiple nodes" +#_(add-test "clone multiple nodes" #(do (reset) (standard-fixture) (assert (= 3 (count (clone (nodes (xpath "//p")))))))) -(add-test "append a single child to a single parent" +#_(add-test "append a single child to a single parent" #(do (reset) (append! (xpath "//body") "

test

") (assert (= 1 (count (nodes (xpath "//body/p[@class='appended1']"))))))) -(add-test "append multiple children to a single parent" +#_(add-test "append multiple children to a single parent" #(do (reset) (append! (xpath "//body") "

test2-1

test2-2

") (assert (= 2 (count (nodes (xpath "//body/p[@class='appended2']"))))))) -(add-test "append a single child to multiple parents" +#_(add-test "append a single child to multiple parents" #(do (reset) (standard-fixture) (append! (xpath "//body/div/p") "!!") (assert (= 3 (count (nodes (xpath "//div/p/span"))))))) -(add-test "append multiple children to multiple parents" +#_(add-test "append multiple children to multiple parents" #(do (reset) (standard-fixture) (append! (xpath "//body/div/p") "some more text") (assert (= 3 (count (nodes (xpath "//div/p/span[@class='foo']"))))))) -(add-test "prepend a single child to a single parent" +#_(add-test "prepend a single child to a single parent" #(do (reset) (append! (xpath "//body") "
2
3
") (prepend! (xpath "//body") "
1
") @@ -208,13 +208,13 @@ (assert (= "2" (text (xpath "//body/div[2]")))) (assert (= "3" (text (xpath "//body/div[3]")))))) -(add-test "prepend a single child to multiple parents" +#_(add-test "prepend a single child to multiple parents" #(do (reset) (append! (xpath "//body") "

2

2

") (prepend! (xpath "//body/div") "

1

") (assert (= 2 (count (nodes (xpath "//body/div/p[text()='2']"))))))) -(add-test "Insert a single child to a single parent" +#_(add-test "Insert a single child to a single parent" #(do (reset) (append! (xpath "//body") "
") @@ -248,14 +248,14 @@ (assert (= 1 (count (nodes (xpath children "p[@class='i2']/following-sibling::*")))))))) -(add-test "destroy a single node" +#_(add-test "destroy a single node" #(do (reset) (append! (xpath "//body") "

app1

") (assert (= 1 (count (nodes (xpath "//body/p[@class='appended1']"))))) (destroy! (xpath "//body/p[@class='appended1']")) (assert (= 0 (count (nodes (xpath "//body/p[@class='appended1']"))))))) -(add-test "destroy multiple nodes" +#_(add-test "destroy multiple nodes" #(do (reset) (append! (xpath "//body") "

app1

app2

") @@ -263,7 +263,7 @@ (destroy! (xpath "//body/p[@class='appended2']")) (assert (= 0 (count (nodes (xpath "//body/p[@class='appended2']"))))))) -(add-test "detach and reattach a single node" +#_(add-test "detach and reattach a single node" #(do (reset) (standard-fixture) (let [n (detach! (xpath "//p[@class='p3']"))] @@ -271,7 +271,7 @@ (append! (xpath "//div[@class='d1']") n) (assert (= 1 (count (nodes (xpath "//p[@class='p3']")))))))) -(add-test "detach and reattach multiple nodes" +#_(add-test "detach and reattach multiple nodes" #(do (reset) (standard-fixture) (let [n (detach! (xpath "//div[@class='d1']/p"))] @@ -279,7 +279,7 @@ (append! (xpath "//div[@class='d1']") n) (assert (= 3 (count (nodes (xpath "//div[@class='d1']/p")))))))) -(add-test "detach child nodes" +#_(add-test "detach child nodes" #(do (reset) (standard-fixture) (let [parent (xpath "//div[@class='d1']") @@ -287,27 +287,27 @@ (assert (= 0 (count (nodes (xpath "//div[@class='d1']/p"))))) (assert (= 3 (count detached-children)))))) -(add-test "clear a node's contents" +#_(add-test "clear a node's contents" #(do (reset) (standard-fixture) (destroy-children! (xpath "//div[@class='d1']")) (assert (= 1 (count (nodes (xpath "//div[@class='d1']"))))) (assert (= 0 (count (nodes (xpath "//div[@class='d1']/*"))))))) -(add-test "insert-before! with a single reference and single new node" +#_(add-test "insert-before! with a single reference and single new node" #(do (reset) (append! (xpath "//body") "
Some content
") (insert-before! (nodes (by-id "ref")) "

before

") (assert (= 1 (count (nodes (xpath "//div[@id='ref']/preceding-sibling::*[text()='before']"))))))) -(add-test "insert-before! with a single reference and multiple new nodes" +#_(add-test "insert-before! with a single reference and multiple new nodes" #(do (reset) (append! (xpath "//body") "
Some content
") (insert-before! (nodes (by-id "ref")) "

before1

before2

") (assert (= 1 (count (nodes (xpath "//div[@id='ref']/preceding-sibling::*[text()='before2' and position()=1]"))))) (assert (= 1 (count (nodes (xpath "//div[@id='ref']/preceding-sibling::*[text()='before1' and position()=2]"))))))) -(add-test "insert-before! with multiple reference nodes and a single new node" +#_(add-test "insert-before! with multiple reference nodes and a single new node" #(do (reset) (append! (xpath "//body") "
content1
") (append! (xpath "//body") "
content2
") @@ -316,20 +316,20 @@ (assert (= 1 (count (nodes (xpath "//div[@id='ref1']/preceding-sibling::p"))))) (assert (= 2 (count (nodes (xpath "//div[@id='ref2']/preceding-sibling::p"))))))) -(add-test "insert-after! with a single reference and single new node" +#_(add-test "insert-after! with a single reference and single new node" #(do (reset) (append! (xpath "//body") "
Some content
") (insert-after! (nodes (by-id "ref")) "

after

") (assert (= 1 (count (nodes (xpath "//div[@id='ref']/following-sibling::*[text()='after']"))))))) -(add-test "insert-after! with a single reference and multiple new nodes" +#_(add-test "insert-after! with a single reference and multiple new nodes" #(do (reset) (append! (xpath "//body") "
Some content
") (insert-after! (nodes (by-id "ref")) "

after1

after2

") (assert (= 1 (count (nodes (xpath "//div[@id='ref']/following-sibling::*[text()='after1' and position()=1]"))))) (assert (= 1 (count (nodes (xpath "//div[@id='ref']/following-sibling::*[text()='after2' and position()=2]"))))))) -(add-test "insert-after! with multiple reference nodes and a single new node" +#_(add-test "insert-after! with multiple reference nodes and a single new node" #(do (reset) (append! (xpath "//body") "
content1
") (append! (xpath "//body") "
content2
") @@ -338,21 +338,21 @@ (assert (= 2 (count (nodes (xpath "//div[@id='ref1']/following-sibling::p"))))) (assert (= 1 (count (nodes (xpath "//div[@id='ref2']/following-sibling::p"))))))) -(add-test "swap-content! with a single reference node and a single new node" +#_(add-test "swap-content! with a single reference node and a single new node" #(do (reset) (append! (xpath "//body") "

TYPO

") (swap-content! (xpath "//p[@id='before']") "

fixed

") (assert (= 0 (count (nodes (xpath "//p[@id='before']"))))) (assert (= 1 (count (nodes (xpath "//p[@id='after']"))))))) -(add-test "swap-content! with a single reference node and multiple new nodes" +#_(add-test "swap-content! with a single reference node and multiple new nodes" #(do (reset) (append! (xpath "//body") "

TYPO

") (swap-content! (xpath "//p[@id='before']") "

fixed1

fixed2

") (assert (= 0 (count (nodes (xpath "//p[@id='before']"))))) (assert (= 2 (count (nodes (xpath "//p[@class='after']"))))))) -(add-test "swap-content! with multiple reference nodes and multiple new nodes" +#_(add-test "swap-content! with multiple reference nodes and multiple new nodes" #(do (reset) (append! (xpath "//body") "

TYPO-1

") (append! (xpath "//body") "

TYPO-2

") diff --git a/test/cljs/domina/css_edge_test.cljs b/test/cljs/domina/css_edge_test.cljs new file mode 100644 index 0000000..b3c7e13 --- /dev/null +++ b/test/cljs/domina/css_edge_test.cljs @@ -0,0 +1,20 @@ +(ns domina.css-edge-test + (:require-macros [cemerick.cljs.test :as m :refer (deftest testing are use-fixtures)] + [domina.macros :as dm]) + (:require [cemerick.cljs.test :as t] + [domina :as dom :refer (nodes)] + [domina.css :as css :refer (sel)] + [domina.fixtures :as fix :refer (base-fixture)])) + +(use-fixtures :each base-fixture) + +;;; sel +(deftest sel-test + (testing "Unit Testing for (sel css-expr)\n" + (testing "Edge Cases\n" + (are [expected actual] (= expected actual) + false (nil? (sel nil)) + false (nil? (sel "")) + false (nil? (sel " ")) + 0 (count (nodes (sel ".not-existent-css-class"))) + 0 (count (nodes (sel "#not-existent-css-id"))))))) diff --git a/test/cljs/domina/css_standard_test.cljs b/test/cljs/domina/css_standard_test.cljs new file mode 100644 index 0000000..acec944 --- /dev/null +++ b/test/cljs/domina/css_standard_test.cljs @@ -0,0 +1,31 @@ +(ns domina.css-standard-test + (:require-macros [cemerick.cljs.test :as m :refer (deftest testing are use-fixtures)] + [domina.macros :as dm] + [hiccups.core :as hm]) + (:require [cemerick.cljs.test :as t] + [domina :as dom :refer (nodes append!)] + [domina.css :as css :refer (sel)] + [domina.fixtures :as fix :refer (base-fixture)] + [hiccups.runtime :as hrt])) + +(use-fixtures :each base-fixture) + +;;; sel +(deftest sel-test + (testing "Unit Testing for (sel css-expr)\n" + (testing "Standard Cases\n" + (are [expected actual] (= expected actual) + 3 (count (nodes (sel "p"))) + 1 (count (nodes (sel ".d1"))) + 1 (count (nodes (sel "#id1"))) + 3 (count (nodes (-> (sel ".d1") + (sel "p")))) + 3 (count (nodes (sel ".d1 > p"))) + 2 (do (append! (sel "body") + (hm/html [:div + [:p [:span "some text"]] + [:p [:span "more text"]]])) + (count (nodes (-> (sel "body") + (sel "div") + (sel "p") + (sel "span"))))))))) diff --git a/test/cljs/domina/fixtures.cljs b/test/cljs/domina/fixtures.cljs new file mode 100644 index 0000000..e420691 --- /dev/null +++ b/test/cljs/domina/fixtures.cljs @@ -0,0 +1,22 @@ +(ns domina.fixtures + (:require-macros [domina.macros :as dm] + [hiccups.core :as hm]) + (:require [domina :as dom :refer (append! destroy!)] + [hiccups.runtime :as hrt] + [domina.xpath :as css :refer (xpath)] + [domina.events :as de :refer (unlisten!)])) + +(defn- initialize [] + (append! (xpath "//body") + (hm/html [:div.d1 + [:p.p1 "P1"] + [:p.p2 "P2"] + [:p#id1.p3 "P3"]]))) +(defn- reset[] + (destroy! (xpath "//body/*")) + (unlisten! (xpath "//*"))) + +(defn base-fixture [f] + (initialize) + (f) + (reset)) diff --git a/test/cljs/domina/xpath_standard_test.cljs b/test/cljs/domina/xpath_standard_test.cljs new file mode 100644 index 0000000..8dc64cd --- /dev/null +++ b/test/cljs/domina/xpath_standard_test.cljs @@ -0,0 +1,32 @@ +(ns domina.xpath-standard-test + (:require-macros [cemerick.cljs.test :as m :refer (deftest testing are use-fixtures)] + [domina.macros :as dm] + [hiccups.core :as hm]) + (:require [cemerick.cljs.test :as t] + [domina :as dom :refer (nodes single-node append!)] + [domina.xpath :as css :refer (xpath)] + [domina.fixtures :as fix :refer (base-fixture)] + [hiccups.runtime :as hrt])) + +(use-fixtures :each base-fixture) + +;;; xpath +(deftest xpath-test + (testing "Unit Testing for (xpath xpath-expr)\n" + (testing "Standard Cases\n" + (are [expected actual] (= expected actual) + 3 (count (nodes (xpath "//p"))) + false (nil? (single-node (xpath "//p"))) + 1 (count (nodes (xpath "//div[@class='d1']"))) + 3 (count (nodes (-> (xpath "//body/div[@class='d1']") + (xpath "p")))) + 2 (do + (append! (xpath "//body") + (hm/html [:div + [:p [:span "some text"]] + [:p [:span "more text"]]])) + (count (nodes (-> (xpath "//body") + (xpath "./div") + (xpath "./p") + (xpath "./span"))))) + 2 (count (nodes (xpath "//p[following-sibling::p[@class='p3']]"))))))) diff --git a/test/cljs/domina_edge_test.cljs b/test/cljs/domina_edge_test.cljs new file mode 100644 index 0000000..c866a14 --- /dev/null +++ b/test/cljs/domina_edge_test.cljs @@ -0,0 +1,247 @@ +(ns domina-edge-test + (:require-macros [cemerick.cljs.test :as m :refer (deftest testing are use-fixtures)] + [domina.macros :as dm]) + (:require [cemerick.cljs.test :as t] + [domina :as dom :refer (nodes + single-node + by-class + by-id + append! + prepend! + clone + ancestor? + common-ancestor + children + detach! + destroy! + destroy-children! + has-class? + value)] + [domina.xpath :as xp :refer (xpath)] + [domina.fixtures :as fix :refer (base-fixture)])) + +(use-fixtures :each base-fixture) + +;;; nodes +(deftest nodes-test + (testing "Unit Testing for (nodes content)\n" + (testing "Edge Cases\n" + (testing "(nodes content)" + (are [expected actual] (= expected actual) + () (nodes nil) + false (nil? (nodes "")) + false (nil? (nodes " ")) + false (nil? (nodes (by-id "not-an-existent-id"))) + nil (nodes ()) + nil (nodes []) + nil (nodes {}) + nil (nodes #{})))))) + +;;; single-node +(deftest single-node-test + (testing "Unit Testing for (single-node node)\n" + (testing "Edge Cases\n" + (testing "(single-node node)" + (are [expected actual] (= expected actual) + nil (single-node nil) + false (nil? (single-node "")) + false (nil? (single-node " ")) + true (nil? (single-node (by-class "not-a-class"))) + true (nil? (single-node (by-id "not-an-id"))) + nil (single-node ()) + nil (single-node []) + nil (single-node {}) + nil (single-node #{})))))) + +;;; by-id +(deftest by-id-test + (testing "Unit Testing for (by-id id)\n" + (testing "Edge Cases\n" + (testing "(by-id s)" + (are [expected actual] (= expected actual) + "Error" (try + (by-id nil) + (catch js/Error e + "Error")) + nil (by-id "") + nil (by-id "not-existent-id") + nil (by-id " ")))))) + +;;; by-class +(deftest by-class-test + (testing "Unit Testing for (by-class css-class)\n" + (testing "Edge Cases\n" + (testing "(by-class css-class)" + (are [expected actual] (= expected actual) + "Error" (try + (by-class nil) + (catch js/Error e + "Error")) + ;nil (by-class "") + ;nil (by-class " ") + nil (by-class "not-existent-class")))))) + +;;; append! +(deftest append!-test + (testing "Unit Testing for (append! parent-content child-content)\n" + (testing "Edge Cases\n" + (testing "(append! single-parent single-child)" + (are [expected actual] (= expected actual) + nil (append! nil nil) + () (children (append! (by-id "id1") nil)) + () (children (append! (by-id "id1") "")) + () (children (append! (by-id "id1") " ")) + nil (append! nil "
"))) + (testing "(append! multiple-parent child-content)" + (are [expected actual] (= expected actual) + () (children (append! (by-class "p1") nil)) + () (children (append! (by-class "p1") "")) + () (children (append! (by-class "p1") " ")) + () (children (append! (by-class "p1") ())) + () (children (append! (by-class "p1") [])) + () (children (append! (by-class "p1") {})) + () (children (append! (by-class "p1") #{}))))))) + +;;; detach +(deftest detach!-test + (testing "Unit Testing for (detach! content)\n" + (testing "Edge Cases\n" + (testing "(detach! content)" + (are [expected actual] (= expected actual) + () (detach! nil) + '(nil) (detach! "") + '(nil) (detach! " ") + () (detach! ()) + () (detach! []) + () (detach! {}) + () (detach! #{}) + () (detach! (by-class "not-a-class")) + () (detach! (by-id "not-an-id"))))))) + +;;; children +(deftest children-test + (testing "Unit Testing for (children content)\n" + (testing "Edge Cases\n" + (testing "(children content)" + (are [expected actual] (= expected actual) + () (children nil) + () (children "") + () (children " ") + () (children ()) + () (children []) + () (children {}) + () (children #{}) + () (children "not-existent-parent") + () (children "not existent parent")))))) + +;;; destroy +(deftest destroy!-test + (testing "Unit Testing for (destroy! content)\n" + (testing "Edge Cases\n" + (testing "(destroy! content)" + (are [expected actual] (= expected actual) + nil (destroy! nil) + nil (destroy! "") + nil (destroy! " ") + nil (destroy! "not-a-content") + nil (destroy! "not a content") + nil (destroy! ()) + nil (destroy! []) + nil (destroy! {}) + nil (destroy! #{})))))) + +;;; common-ancestor +(deftest common-ancestor-test + (testing "Unit Testing for (common-ancestor &contents)\n" + (testing "Edge Cases\n" + (testing "(common-ancestor node-a)" + (are [expected actual] (= expected actual) + nil (common-ancestor nil))) + (testing "(common-ancestor node-a node-b)" + (are [expected actual] (= expected actual) + nil (common-ancestor nil nil) + nil (common-ancestor nil (by-id "id1")) + nil (common-ancestor (by-id "id1") nil))) + (testing "(common-ancestor node-a node-b node-c)" + (are [expected actual] (= expected actual) + nil (common-ancestor nil nil nil) + nil (common-ancestor nil (by-id "id1") nil) + nil (common-ancestor nil nil (by-id "id1")) + nil (common-ancestor (by-id "id1") nil nil)))))) + +;;; ancestor? +(deftest ancestor?-test + (testing "Unit Testing for (ancestor? ancestor-node descendant-node)\n" + (testing "Edge Cases\n" + (testing "(ancestor? ancestor descendant)" + (are [expected actual] (= expected actual) + true (ancestor? nil nil) + true (ancestor? nil (by-id "id1")) + false (ancestor? (by-id "id1") nil) + + true (ancestor? () ()) + true (ancestor? nil ()) + true (ancestor? () nil) + + true (ancestor? [] []) + true (ancestor? nil []) + true (ancestor? [] nil) + + true (ancestor? {} {}) + true (ancestor? nil {}) + true (ancestor? {} nil) + + true (ancestor? #{} #{}) + true (ancestor? nil #{}) + true (ancestor? #{} nil)))))) + +;;; clone +(deftest clone-test + (testing "Unit Testing for (clone node)\n" + (testing "Edge Cases\n" + (testing "(clone node)" + (are [expected actual] (= expected actual) + () (clone nil) + () (clone ()) + () (clone []) + () (clone {}) + () (clone #{})))))) + +;;; has-class? +(deftest has-class?-test + (testing "Unit Testing for (has-class? content class)\n" + (testing "Edge Cases\n" + (testing "(has-class? content class)" + (are [expected actual] (= expected actual) + "Error" (try + (has-class? nil nil) + (catch js/Error e + "Error")) + "Error" (try + (has-class? nil "p1") + (catch js/Error e + "Error")) + false (has-class? (by-id "id1") nil)))))) + +;;; value +(deftest value-test + (testing "Unit Testing for (value content)\n" + (testing "Edge Cases\n" + (testing "(value single-node)" + (are [expected actual] (= expected actual) + "Error" (try + (value (by-id nil)) + (catch js/Error e + "Error")) + "Error" (try + (value (by-id "")) + (catch js/Error e + "Error")) + "Error" (try + (value (by-id " ")) + (catch js/Error e + "Error")) + "Error" (try + (value (by-id "not-existent-id")) + (catch js/Error e + "Error"))))))) diff --git a/test/cljs/domina_standard_test.cljs b/test/cljs/domina_standard_test.cljs new file mode 100644 index 0000000..cae50d0 --- /dev/null +++ b/test/cljs/domina_standard_test.cljs @@ -0,0 +1,310 @@ +(ns domina-standard-test + (:require-macros [cemerick.cljs.test :as m :refer (deftest testing are use-fixtures)] + [domina.macros :as dm] + [hiccups.core :as hm]) + (:require [cemerick.cljs.test :as t] + [domina :as dom :refer (nodes + single-node + by-id + by-class + append! + clone + detach! + destroy! + destroy-children! + children + insert-before! + insert-after! + prepend! + insert! + swap-content! + text)] + [domina.fixtures :as fix :refer (base-fixture)] + [domina.xpath :as xp :refer (xpath)] + [hiccups.runtime :as hrt])) + +(use-fixtures :each base-fixture) + +;;; nodes +(deftest nodes-test + (testing "Unit Testing for (nodes content)\n" + (testing "Standard Cases\n" + (testing "(nodes content)" + (are [expected actual] (= expected actual) + 1 (count (nodes (xpath "//body"))) + 1 (count (nodes (xpath "//body/div"))) + 1 (count (nodes (xpath "//div[@class='d1']")))))))) + +;;; single-node +(deftest single-node-test + (testing "Unit Testing for (single-node node)\n" + (testing "Standard Cases\n" + (testing "(single-node node)" + (are [expected actual] (= expected actual) + false (nil? (single-node (xpath "//p"))) + false (nil? (single-node (by-class "p1"))) + false (nil? (single-node (by-id "id1")))))))) + +;;; by-id +(deftest by-id-test + (testing "Unit Testing for (by-id id)\n" + (testing "Standard Cases\n" + (testing "(by-id id)" + (are [expected actual] (= expected actual) + false (nil? (by-id "id1"))))))) + +;;; by-class +(deftest by-class-test + (testing "Unit Testing for (by-class css-class)\n" + (testing "Standard Cases\n" + (testing "(by-class css-class)" + (are [expected actual] (= expected actual) + 1 (count (by-class "p3")) + 2 (do + (append! (xpath "//body") + (hm/html [:p.tc "test1"] + [:p.tc "test2"])) + (count (nodes (by-class "tc"))))))))) + +;;; append! +(deftest append!-test + (testing "Unit Testing for (append! parent-content child-content)\n" + (testing "Standard Cases\n" + (testing "(append! single-parent single-child)" + (are [expected actual] (= expected actual) + 1 (do + (children (append! (xpath "//body") + (hm/html [:p.appended1 "test"]))) + (count (nodes (xpath "//body/p[@class='appended1']")))))) + (testing "Standard Cases\n" + (testing "(append! single-parent children)" + (are [expected actual] (= expected actual) + 2 (do + (append! (xpath "//body") + (hm/html [:p.appended2 "test2-1"] + [:p.appended2 "test2-2"])) + (count (nodes (xpath "//body/p[@class='appended2']")))))) + (testing "(append! multiple-parent single-child)" + (are [expected actual] (= expected actual) + 3 (do + (append! (xpath "//body/div/p") + (hm/html [:span "!!"])) + (count (nodes (xpath "//div/p/span")))))) + (testing "(append! multiple-parent children)" + (are [expected actual] (= expected actual) + 3 (do + (append! (xpath "//body/div/p") + (hm/html ["some " [:span.foo "more"] "text"])) + (count (nodes (xpath "//div/p/span[@class='foo']")))))))))) + +;;; detach! +(deftest detach!-test + (testing "Unit Testing for (detach! content)\n" + (testing "Standard Cases\n" + (testing "(detach! single-node)" + (are [expected actual] (= expected actual) + 0 (do + (detach! (xpath "//p[@class='p3']")) + (count (nodes (xpath "//p[@class='p3']")))))) + (testing "(detach! multiple-nodes)" + (are [expected actual] (= expected actual) + 0 (do + (detach! (xpath "//div[@class='d1']/p")) + (count (nodes (xpath "//div[@class='d1']/p")))) + 0 (do + (detach! (children "//div[@class='d1']")) + (count (children "//div[@class='d1']")))))))) + +;;; children +(deftest children-test + (testing "Unit Testing for (children content)\n" + (testing "Standard Cases\n" + (testing "(children content)" + (are [expected actual] (= expected actual) + 3 (count (children (xpath "//div[@class='d1']")))))))) + +;;; clone +(deftest clone-test + (testing "Unit Testing for (clone single-node)\n" + (testing "Standard Cases\n" + (testing "(clone single-node)" + (are [expected actual] (= expected actual) + 1 (count (clone (single-node (xpath "//p")))))) + (testing "(clone multiple-nodes)" + (are [expected actual] (= expected actual) + 3 (count (clone (nodes (xpath "//p"))))))))) + +;;; destroy! +(deftest destroy!-test + (testing "Unit Testing for (destroy! content)\n" + (testing "Standard Cases\n" + (testing "(destroy! single-node)" + (are [expected actual] (= expected actual) + 0 (do (append! (xpath "//body") + (hm/html [:p.appended1 "app1"])) + (destroy! (xpath "//body/p[@class='appended1']")) + (count (nodes (xpath "//body/p[@class='appended1']")))))) + (testing "(destroy! multiple-nodes)" + (are [expected actual] (= expected actual) + 0 (do (append! (xpath "//body") + (hm/html [:p.appended2 "app1"] + [:p.appended2 "app2"])) + (destroy! (xpath "//body/p[@class='appended2']")) + (count (nodes (xpath "//body/p[@class='appended2']"))))))))) + +;;; destroy-children! +(deftest destroy-children!-test + (testing "Unit Testing for (destroy-children! content)\n" + (testing "Standard Cases\n" + (testing "(destroy-children! single-node)" + (are [expected actual] (= expected actual) + 0 (do + (destroy-children! (xpath "//div[@class='d1']")) + (count (nodes (xpath "//div[@class='d1']/*"))))))))) + +;;; insert-before! +(deftest insert-before!-test + (testing "Unit Testing for (insert-before! ref-nodes new-nodes)\n" + (testing "Standard Cases\n" + (testing "(insert-before! single-ref-node single-node)" + (are [expected actual] (= expected actual) + 1 (do + (append! (xpath "//body") + (hm/html [:div#ref "Some Content"])) + (insert-before! (nodes (by-id "ref")) "

before

") + (count (nodes (xpath "//div[@id='ref']/preceding-sibling::*[text()='before']")))))) + (testing "(insert-before! single-ref-node multiple-nodes)" + (are [expected actual] (= expected actual) + 2 (do (append! (xpath "//body") + (hm/html [:div#ref "some content"])) + (insert-before! (nodes (by-id "ref")) + (hm/html [:p "before1"] + [:p "before2"])) + (+ (count (nodes (xpath "//div[@id='ref']/preceding-sibling::*[text()='before2' and position()=1]"))) + (count (nodes (xpath "//div[@id='ref']/preceding-sibling::*[text()='before1' and position()=2]")))))))))) + +;;; insert-before! +(deftest insert-before!!-test + (testing "Unit Testing for (insert-before! ref-nodes new-nodes)\n" + (testing "Standard Cases\n" + (testing "(insert-before! multiple-ref-nodes single-node)" + (are [expected actual] (= expected actual) + 5 (do + (append! (xpath "//body") (hm/html [:div#ref1.ref "content1"])) + (append! (xpath "//body") (hm/html [:div#ref2.ref "content2"])) + (insert-before! (nodes (by-class "ref")) (hm/html [:p "before"])) + (+ (count (nodes (xpath "//div[@class='ref']"))) + (count (nodes (xpath "//div[@id='ref1']/preceding-sibling::p"))) + (count (nodes (xpath "//div[@id='ref2']/preceding-sibling::p")))))))))) + +;;; prepend! +(deftest prepend!-test + (testing "Unit Testing for (prepend! parents children)\n" + (testing "Standard Cases\n" + (testing "(prepend! single-parent single-child)" + (are [expected actual] (= expected actual) + "1" (do + (append! (xpath "//body") (hm/html [:div "2"] [:div "3"])) + (prepend! (xpath "//body") (hm/html [:div "1"])) + (text (xpath "//body/div[1]"))))) + (testing "(prepend! parents single-child)" + (are [expected actual] (= expected actual) + 2 (do (append! (xpath "//body") + (hm/html [:div [:p "2"]] + [:div [:p "2"]]) + #_"

2

2

") + (prepend! (xpath "//body/div") + (hm/html [:p "1"]) + #_"

1

") + (count (nodes (xpath "//body/div/p[text()='2']"))))))))) + +(deftest insert!-test + (testing "Unit Testing for (insert! parents children)\n" + (testing "Standard Cases\n" + (testing "(insert! parent child)" + (are [expected actual] (= expected actual) + 5 (do + (append! (xpath "//body") + (hm/html [:div.testInserts])) + (append! (xpath "//div[@class='testInserts']") + (hm/html [:p.i1])) + (append! (xpath "//div[@class='testInserts']") + (hm/html [:p.i3])) + (insert! (xpath "//div[@class='testInserts']") + (hm/html [:p.i2]) 1) + (+ (count (nodes (xpath "//div[@class='testInserts']/p"))) + (count (nodes (xpath "//p[@class='i2']/preceding-sibling::*"))) + (count (nodes (xpath "//p[@class='i2']/following-sibling::*"))))))) + (testing "(insert! parents single-child)" + (are [expected actual] (= expected actual) + true false))))) + +;;; insert-after! +(deftest insert-after!-test + (testing "Unit Testing for (insert-after! ref-nodes new-nodes)\n" + (testing "Standard Cases\n" + (testing "(insert-after! single-ref-node single-node)" + (are [expected actual] (= expected actual) + 1 (do + (append! (xpath "//body") + (hm/html [:div#ref "Some content"])) + (insert-after! (nodes (by-id "ref")) + (hm/html [:p "after"])) + (count (nodes (xpath "//div[@id='ref']/following-sibling::*[text()='after']")))))) + (testing "(insert-after! single-ref-node multiple-nodes)" + (are [expected actual] (= expected actual) + 2 (do + (append! (xpath "//body") + (hm/html [:div#ref "Some content"])) + (insert-after! (nodes (by-id "ref")) + (hm/html [:p "after1"] + [:p "after2"])) + (+ (count (nodes (xpath "//div[@id='ref']/following-sibling::*[text()='after1' and position()=1]"))) + (count (nodes (xpath "//div[@id='ref']/following-sibling::*[text()='after2' and position()=2]"))))))) + (testing "(insert-after! multiple-ref-node multiple-nodes)" + (are [expected actual] (= expected actual) + 3 (do + (append! (xpath "//body") + (hm/html [:div#ref1.ref "content1"])) + (append! (xpath "//body") + (hm/html [:div#ref2.ref "content2"])) + (insert-after! (nodes (by-class "ref")) + (hm/html [:p "after"])) + (+ (count (nodes (xpath "//div[@id='ref1']/following-sibling::p"))) + (count (nodes (xpath "//div[@id='ref2']/following-sibling::p")))))))))) + +;;; swap-content! +(deftest swap-content!-test + (testing "Unit Testing for (swap-content! old-content new-content)\n" + (testing "Standard Cases\n" + (testing "(swap-content! single-old-node single-new-node)" + (are [expected actual] (= expected actual) + 1 (do + (append! (xpath "//body") + (hm/html [:div [:p#before "TYPO"]])) + (swap-content! (xpath "//p[@id='before']") + (hm/html [:p#after "fixed"])) + (+ (count (nodes (xpath "//p[@id='before']"))) + (count (nodes (xpath "//p[@id='after']"))))))) + (testing "(swap-content! single-ref-node multiple-new-node)" + (are [expected actual] (= expected actual) + 2 (do + (append! (xpath "//body") + (hm/html [:div [:p#before "TYPE"]])) + (swap-content! (xpath "//p[@id='before']") + (hm/html [:p.after "fixed1"] + [:p.after "fixed2"])) + (+ (count (nodes (xpath "//p[@id='before']"))) + (count (nodes (xpath "//p[@class='after']"))))))) + (testing "(swap-content! multiple-ref-nodes multiple-new-node)" + (are [expected actual] (= expected actual) + 4 (do + (append! (xpath "//body") + (hm/html [:div [:p.before1 "TYPO-1"]])) + (append! (xpath "//body") + (hm/html [:div [:p.before1"TYPO-2"]])) + (swap-content! (xpath "//p[@class='before1']") + (hm/html [:p.after1 "fixed1"] + [:p.after1 "fixed2"])) + (+ (count (nodes (xpath "//p[@class='before1']"))) + (count (nodes (xpath "//p[@class='after1']"))))))))))