Fork me on GitHub
#clojurescript
<
2018-08-21
>
kwladyka14:08:49

how to write tests simulated event on-blur or change on input?

kwladyka14:08:37

*without webbrowser clicking simulation

kwladyka14:08:40

Is it possible?

kwladyka14:08:56

I want to have value event.target.name event.target.value event.type. Just thinking if instead make this fake “map” with static values for tests purpose

kwladyka14:08:47

or I should simulate webbrowser without no excuse hmm… not sure what is right choice

thheller14:08:07

you mean like calling (on-blur (clj->js {:type "foo" :target {:name "foo" :value "foo"}})?

kwladyka14:08:44

I mean fn which I want to test will look something like that:

(defn fn-name->value [form]
  "assoc input value to atom {}
  The best with on-change."
  (let [{:keys [names->value names->msg names->validations specs->msg]} form]
    (fn [event]
      (let [name (keyword event.target.name)
            value event.target.value
            spec (get names->spec name)]
        (validate->atom names->msg specs->msg spec name value)
        (swap! names->value #(assoc % name value))))))

kwladyka14:08:37

fn use event.target.name or that way or another. I will rewrite it later probably to (-> …)

kwladyka14:08:29

All after all my goal is to make library which could be use with leiningen and shadow-cljs to help work with form validation and spec

kwladyka14:08:17

At that moment I am doing everything in 1 shadow-cljs project to test my ideas

thheller14:08:03

what I wrote above should work

thheller14:08:25

(let [on-blur (fn-name->value form)] (on-blur ...))

kwladyka14:08:41

Do you think test it in that way instead of simulation webbrowser is ok?

thheller14:08:59

depends on how much you rely on DOM stuff

kwladyka14:08:49

Hmm I guess only on on-blur and change events and event.target.name event.target.value event.type. At least at that moment.

kwladyka14:08:05

I think it should be fine

thheller14:08:26

well the above doesn't require any DOM. you don't need actual event object you just need something with the correct properties

thheller14:08:31

which you get with the code I posted

kwladyka14:08:48

(defn test-fn [form event]
  (println "event.type" event.type)
  (when-not (re-matches #".*!.*" event.target.value)
    {:msg "Password should have '!' character"
     :level :warn}))

(def init (-> {:names->value {:email ""
                              :password ""}
               :names->validations {:email [::spec-email]
                                    :password [::spec-password test-fn]}
               :specs->msg {::spec-email "Typo? It doesn't look valid."
                            ::spec-password "Password is required."}}
              (form/init-form)))
BTW what do you think about this TL; DR; module? It will let use specs, fns and different levels of :error :warn :info etc. Such opensource make sense? I didn’t find any which satisfy me. Does it look ok (I know that is too primitive description)?

kwladyka14:08:33

[mui/text-field
            {:on-change value->form
             :on-blur fn-name->show
             :error (form/show-error? form :email)
             :helper-text (if email-error?
                            (form/get-error form :email)
                            " ")
             :name "email"}]
^inputs

jjttjj15:08:08

Is anyone using or has ever used hipo? https://github.com/jeluard/hipo

dottedmag16:08:59

Is there a trick to wait for core.async channel in REPL?

thheller16:08:46

you can't block so not really 😉

dottedmag16:08:01

Well, (go (print (<! ...))) works, but I'd like to capture the output somewhere for interrogation.

dottedmag16:08:44

(go (def captured (<! ...))) works too, but ugh.

jjttjj16:08:36

you could use an atom and just reset! it in the go block which might feel a little cleaner

jjttjj16:08:14

(def captured (atom nil))
(go (reset! captured (<! ...))))
@captured

aaelony17:08:10

Snowflake uses Javascript for UDF’s. Has anyone that uses Snowflake thought about using Clojurescript for such UDFs? https://docs.snowflake.net/manuals/sql-reference/udf-overview.html

dottedmag17:08:39

@jjttjj Thanks, that works too.

dottedmag17:08:00

Is there a built-in helper to convert data from/to Uint8Array (and typed arrays in general)?

dottedmag17:08:04

They are not even iterable.

thheller17:08:12

array-seq should work (assuming you mean converting to CLJS seqs)