Skip to content

Commit

Permalink
Setup testing and implement two sort-of integration tests. For #6.
Browse files Browse the repository at this point in the history
  • Loading branch information
pupeno committed Aug 23, 2016
1 parent 2d6214e commit 6f0c61b
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
/classes
/out
/checkouts
pom.xml
pom.xml.asc
Expand Down
14 changes: 12 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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}}]})
9 changes: 0 additions & 9 deletions test/cljs/free_form/core_test.clj

This file was deleted.

261 changes: 261 additions & 0 deletions test/cljs/free_form/core_test.cljs
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"}]
[: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 "[email protected]"
: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 "[email protected]"
: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"]]))))))


7 changes: 7 additions & 0 deletions test/cljs/free_form/runner.cljs
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6f0c61b

Please sign in to comment.