Forked from cdbkr/react-testing-library-clojurescript.cljs
Created
February 16, 2020 06:14
-
-
Save jaimesangcap/6e88f5fb1b5abf058b798dd1d0a2252c to your computer and use it in GitHub Desktop.
React-testing-library and Re-frame clojurescript app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns demo.core-test | |
(:require [cljs.test :refer [deftest | |
testing | |
is | |
use-fixtures]] | |
[clojure.string :refer [lower-case]] | |
[demo.components :refer [title-component | |
counter-component]] | |
[reagent.core :as r] | |
["react-testing-library" :as rtl])) | |
(use-fixtures :each | |
{:after rtl/cleanup}) | |
;; Idea from https://github.com/reagent-project/reagent/blob/master/test/reagenttest/utils.cljs | |
(defn with-mounted-component [comp f] | |
(let [mounted-component (rtl/render (r/as-element comp))] | |
(try | |
(f mounted-component) | |
(finally | |
(.unmount mounted-component) | |
(r/flush))))) | |
(defn click-element [el] | |
(.click rtl/fireEvent el) | |
(r/flush)) | |
(deftest test-title | |
(testing "A provided title should be enclosed in a h1" | |
(with-mounted-component | |
[title-component "hello"] | |
(fn [component] | |
(is | |
(= "h1" | |
(-> component | |
(.getByText "hello") | |
(.-tagName) | |
lower-case)))))) | |
(testing "A title saying 'hello' is shown to the user" | |
(with-mounted-component | |
[title-component "hello"] | |
(fn [component] | |
(is | |
(= "hello" | |
(-> component | |
(.getByText "hello") | |
(.-innerHTML)))))))) | |
(deftest test-stateful-component | |
(testing "A counter should track clicks" | |
(with-mounted-component | |
[counter-component {:text "increase"}] | |
(fn [component] | |
(is | |
(= "Counter: 0" | |
(->> (.getByText component #"Counter:") | |
(.-innerHTML)))) | |
(click-element (.getByText component "increase")) | |
(is | |
(= "Counter: 1" | |
(->> (.getByText component #"Counter:") | |
(.-innerHTML)))))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment