Skip to content

Instantly share code, notes, and snippets.

@wilkerlucio
Created November 2, 2018 02:12
Show Gist options
  • Save wilkerlucio/3d0846d106f992bff7c7e49ea6f580c2 to your computer and use it in GitHub Desktop.
Save wilkerlucio/3d0846d106f992bff7c7e49ea6f580c2 to your computer and use it in GitHub Desktop.
Generative testing for fulcro components
(ns your-ns
(:require [clojure.test.check.properties :as prop]
[com.wsscode.pathom.gen :as sgen]
[cljs.test :refer [is]]
[clojure.test.check.generators :as gen :include-macros true]
[fulcro.client.primitives :as fp]
[clojure.test.check :as tc]
[fulcro.client.mutations :as fm]
[clojure.data :as data]))
(defn normalize-gen-value [g]
(gen/one-of [g (gen/return nil) (gen/return :com.wsscode.pathom.core/reader-error)]))
(defn test-widget-prop [{::keys [component factory gen-env]}]
(let [factory (or factory (fp/factory component))
gen-env (assoc gen-env ::sgen/transform-generator normalize-gen-value)]
(prop/for-all [props (sgen/comp-props-generator gen-env component)]
(let [frame (js/document.createElement "iframe")]
(js/ReactDOM.render (factory props) frame
#(js/ReactDOM.unmountComponentAtNode frame))
;; ReactDOM.render returns nil, so we need to return true
true))))
(defn check-props-result [{::keys [form]} {:keys [result] :as res}]
(let [{:nubank.workspaces.card-types.test/keys [test]} (cljs.test/get-current-env)]
(if (true? result)
(cljs.test/do-report
{:type :pass
:message "Props checked with success"
:expected form
:actual res})
(do
(js/console.error "Props check for" test "failed\n" result)
(cljs.test/do-report
{:type :fail
:message "FAILED"
:expected form
:actual res})))))
(defn check-component [options]
(let [form `(check-props-result
options
(tc/quick-check 10
(test-widget-prop options)))]
(try
(check-props-result
(assoc options ::form form)
(tc/quick-check 10
(test-widget-prop options)))
(catch :default e
(cljs.test/do-report
{:type :error
:expected form
:actual e})))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment