diff --git a/.gitignore b/.gitignore index a4ff1e8..818118a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target /classes +/out /checkouts pom.xml pom.xml.asc diff --git a/project.clj b/project.clj index 5b31590..5754277 100644 --- a/project.clj +++ b/project.clj @@ -12,6 +12,16 @@ :url "https://github.com/pupeno/free-form"} :dependencies [[org.clojure/clojurescript "1.9.225" :scope "provided"] - [org.clojure/clojure "1.8.0" :scope "provided"]] + [org.clojure/clojure "1.8.0" :scope "provided"] + [reagent "0.5.1" :scope "provided"] + [re-frame "0.7.0" :scope "provided"] + [doo "0.1.7" :scope "provided"]] + :plugins [[lein-cljsbuild "1.1.4"] + [lein-doo "0.1.7"]] + :doo {:build "test"} - :source-paths ["src/cljs"]) + :cljsbuild {:builds [{:id "test" + :source-paths ["src/cljs" "test/cljs"] + :compiler {:main free-form.runner + :output-to "out/free_form.js" + :optimizations :none}}]}) diff --git a/test/cljs/free_form/core_test.clj b/test/cljs/free_form/core_test.clj deleted file mode 100644 index 2e2ddc8..0000000 --- a/test/cljs/free_form/core_test.clj +++ /dev/null @@ -1,9 +0,0 @@ -;;;; Copyright © 2015, 2016 José Pablo Fernández Silva, All rights reserved. - -(ns free-form.core-test - (:require [clojure.test :refer :all] - [free-form.core :refer :all])) - -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1)))) diff --git a/test/cljs/free_form/core_test.cljs b/test/cljs/free_form/core_test.cljs new file mode 100644 index 0000000..f5fec56 --- /dev/null +++ b/test/cljs/free_form/core_test.cljs @@ -0,0 +1,261 @@ +;;;; Copyright © 2015 2016 José Pablo Fernández Silva All rights reserved. + +(ns free-form.core-test + (:require [clojure.test :refer [deftest testing is]] + [clojure.walk :refer [prewalk]] + [free-form.core :as free-form])) + +(defn- hide-on-change [form] + (prewalk (fn [node] (if (contains? node :on-change) + (assoc node :on-change :was-function) + node)) + form)) + +(deftest a-test + (let [plain-reagent-form-template [:form {:noValidate true} + [:div.errors {:free-form/error-message {:key :-general}} [:p.error]] + [:div.field {:free-form/error-class {:key :text :error "validation-errors"}} + [:label {:for :text} "Text"] + [:input.form-control {:free-form/input {:key :text} + :type :text + :id :text + :placeholder "placeholder"}] + [:div.errors {:free-form/error-message {:key :text}} [:p.error]]] + [:div.field {:free-form/error-class {:key :email :error "validation-errors"}} + [:label {:for :email} "Email"] + [:input.form-control {:free-form/input {:key :email} + :type :email + :id :email + :placeholder "placeholder@example.com"}] + [:div.errors {:free-form/error-message {:key :email}} [:p.error]]] + [:div.field {:free-form/error-class {:key :password :error "validation-errors"}} + [:label {:for :password} "Password"] + [:input.form-control {:free-form/input {:key :password} + :type :password + :id :password}] + [:div.errors {:free-form/error-message {:key :password}} [:p.error]]] + [:div.field {:free-form/error-class {:key :select :error "validation-errors"}} + [:label {:for :select} "Select"] + [:select.form-control {:free-form/input {:key :select} + :type :select + :id :select} + [:option] + [:option {:value :dog} "Dog"] + [:option {:value :cat} "Cat"] + [:option {:value :squirrel} "Squirrel"] + [:option {:value :giraffe} "Giraffe"]] + [:div.errors {:free-form/error-message {:key :select}} [:p.error]]] + [:div.field {:free-form/error-class {:key :select-with-group :error "validation-errors"}} + [:label {:for :select} "Select with groups"] + [:select.form-control {:free-form/input {:key :select-with-group} + :type :select + :id :select-with-group} + [:option] + [:optgroup {:label "Numbers"} + [:option {:value :one} "One"] + [:option {:value :two} "Two"] + [:option {:value :three} "Three"] + [:option {:value :four} "Four"]] + [:optgroup {:label "Leters"} + [:option {:value :a} "A"] + [:option {:value :b} "B"] + [:option {:value :c} "C"] + [:option {:value :d} "D"]]] + [:div.errors {:free-form/error-message {:key :select-with-group}} [:p.error]]] + [:div.field {:free-form/error-class {:key :textarea :error "validation-errors"}} + [:label {:for :text-area} "Text area"] + [:textarea.form-control {:free-form/input {:key :textarea} + :id :textarea}] + [:div.errors {:free-form/error-message {:key :textarea}} [:p.error]]] + [:div.field {:free-form/error-class {:key [:t :e :x :t] :error "validation-errors"}} + [:label {:for :text} "Text with deep keys"] + [:input.form-control {:free-form/input {:keys [:t :e :x :t]} + :type :text + :id :text + :placeholder "placeholder"}] + [:div.errors {:free-form/error-message {:keys [:t :e :x :t]}} [:p.error]]] + [:div.field {:free-form/error-class {:key :text-with-extra-validation-errors :error "validation-errors" + :extra-keys [[:text] [:-general]]}} + [:label {:for :text-with-extra-validation-errors} "Text with extra validation errors"] + [:input.form-control {:free-form/input {:key :text-with-extra-validation-errors} + :type :text + :id :text-with-extra-validation-errors + :placeholder "This will be marked as a validation error also when Text and General have validation errors."}] + [:div.errors {:free-form/error-message {:key :text-with-extra-validation-errors}} [:p.error]]] + [:button "Button"]]] + + (testing "simple generation" + (let [generated-input (hide-on-change + (free-form/form {} {} (fn [_keys _value]) + plain-reagent-form-template))] + (is (= generated-input + [:form {:noValidate true} + [:div.errors {:free-form/error-message {:key :-general}} [:p.error]] + [:div.field {} + [:label {:for :text} "Text"] + [:input.form-control {:type :text + :id :text + :placeholder "placeholder" + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :text}} [:p.error]]] + [:div.field {} + [:label {:for :email} "Email"] + [:input.form-control {:type :email + :id :email + :placeholder "placeholder@example.com" + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :email}} [:p.error]]] + [:div.field {} + [:label {:for :password} "Password"] + [:input.form-control {:type :password + :id :password + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :password}} [:p.error]]] + [:div.field {} + [:label {:for :select} "Select"] + [:select.form-control {:type :select + :id :select + :default-value nil + :on-change :was-function} + [:option] + [:option {:value :dog} "Dog"] + [:option {:value :cat} "Cat"] + [:option {:value :squirrel} "Squirrel"] + [:option {:value :giraffe} "Giraffe"]] + [:div.errors {:free-form/error-message {:key :select}} [:p.error]]] + [:div.field {} + [:label {:for :select} "Select with groups"] + [:select.form-control {:type :select + :id :select-with-group + :default-value nil + :on-change :was-function} + [:option] + [:optgroup {:label "Numbers"} + [:option {:value :one} "One"] + [:option {:value :two} "Two"] + [:option {:value :three} "Three"] + [:option {:value :four} "Four"]] + [:optgroup {:label "Leters"} + [:option {:value :a} "A"] + [:option {:value :b} "B"] + [:option {:value :c} "C"] + [:option {:value :d} "D"]]] + [:div.errors {:free-form/error-message {:key :select-with-group}} [:p.error]]] + [:div.field {} + [:label {:for :text-area} "Text area"] + [:textarea.form-control {:id :textarea + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :textarea}} [:p.error]]] + [:div.field {} + [:label {:for :text} "Text with deep keys"] + [:input.form-control {:type :text + :id :text + :placeholder "placeholder" + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:keys [:t :e :x :t]}} [:p.error]]] + [:div.field {} + [:label {:for :text-with-extra-validation-errors} "Text with extra validation errors"] + [:input.form-control {:type :text + :id :text-with-extra-validation-errors + :placeholder "This will be marked as a validation error also when Text and General have validation errors." + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :text-with-extra-validation-errors}} [:p.error]]] + [:button "Button"]])))) + + (testing "generation with initial data" + (let [generated-input (hide-on-change + (free-form/form {:text "Text value" + :email "Email value" + :password "Password value" + ;:select "cat" ; TODO: enable this and fix generation, as it's broken right now. + ;:select-with-group "two" ; TODO: enable this and fix generation, as it's broken right now. + :textarea "Textarea value" + :t {:e {:x {:t "Text with deep keys value"}}} + } {} (fn [_keys _value]) + plain-reagent-form-template))] + (is (= generated-input + [:form {:noValidate true} + [:div.errors {:free-form/error-message {:key :-general}} [:p.error]] + [:div.field {} + [:label {:for :text} "Text"] + [:input.form-control {:type :text + :id :text + :placeholder "placeholder" + :default-value "Text value" + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :text}} [:p.error]]] + [:div.field {} + [:label {:for :email} "Email"] + [:input.form-control {:type :email + :id :email + :placeholder "placeholder@example.com" + :default-value "Email value" + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :email}} [:p.error]]] + [:div.field {} [:label {:for :password} "Password"] + [:input.form-control {:type :password + :id :password + :default-value "Password value" + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :password}} [:p.error]]] + [:div.field {} + [:label {:for :select} "Select"] + [:select.form-control {:type :select + :id :select + :default-value nil + :on-change :was-function} + [:option] + [:option {:value :dog} "Dog"] + [:option {:value :cat} "Cat"] + [:option {:value :squirrel} "Squirrel"] + [:option {:value :giraffe} "Giraffe"]] + [:div.errors {:free-form/error-message {:key :select}} [:p.error]]] + [:div.field {} + [:label {:for :select} "Select with groups"] + [:select.form-control {:type :select + :id :select-with-group + :default-value nil + :on-change :was-function} + [:option] + [:optgroup {:label "Numbers"} + [:option {:value :one} "One"] + [:option {:value :two} "Two"] + [:option {:value :three} "Three"] + [:option {:value :four} "Four"]] + [:optgroup {:label "Leters"} + [:option {:value :a} "A"] + [:option {:value :b} "B"] + [:option {:value :c} "C"] + [:option {:value :d} "D"]]] + [:div.errors {:free-form/error-message {:key :select-with-group}} [:p.error]]] + [:div.field {} + [:label {:for :text-area} "Text area"] + [:textarea.form-control {:id :textarea + :default-value "Textarea value" + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :textarea}} [:p.error]]] + [:div.field {} + [:label {:for :text} "Text with deep keys"] + [:input.form-control {:type :text + :id :text + :placeholder "placeholder" + :default-value "Text with deep keys value" + :on-change :was-function}] + [:div.errors {:free-form/error-message {:keys [:t :e :x :t]}} [:p.error]]] + [:div.field {} + [:label {:for :text-with-extra-validation-errors} "Text with extra validation errors"] + [:input.form-control {:type :text + :id :text-with-extra-validation-errors + :placeholder "This will be marked as a validation error also when Text and General have validation errors." + :default-value nil + :on-change :was-function}] + [:div.errors {:free-form/error-message {:key :text-with-extra-validation-errors}} [:p.error]]] + [:button "Button"]])))))) + + diff --git a/test/cljs/free_form/runner.cljs b/test/cljs/free_form/runner.cljs new file mode 100644 index 0000000..60c54bd --- /dev/null +++ b/test/cljs/free_form/runner.cljs @@ -0,0 +1,7 @@ +;;;; Copyright © 2016 José Pablo Fernández Silva, All rights reserved. + +(ns free-form.runner + (:require [doo.runner :refer-macros [doo-tests]] + [free-form.core-test])) + +(doo-tests 'free-form.core-test) \ No newline at end of file