-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add devcards and react testing library
- Loading branch information
1 parent
015cf3c
commit dc74704
Showing
16 changed files
with
179 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
node_modules | ||
dist | ||
out | ||
test* | ||
test*/ | ||
.shadow-cljs | ||
.nrepl-port | ||
yarn-error.log | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
{:lint-as {reagent.core/with-let clojure.core/let}} | ||
{:lint-as {devcards.core/defcard cljs.core/def | ||
devcards.core/deftest cljs.core/def}} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(ns app.cards.devcards-runner | ||
(:require [cljsjs.react] | ||
[cljsjs.react.dom] | ||
; devcards needs cljsjs.react and cljsjs.react.dom to be imported | ||
; separately for shadow-cljs to add shims. | ||
[devcards.core :refer [start-devcard-ui!]] | ||
; Import all namespaces with cards here to load them. | ||
[app.hello-cards])) | ||
|
||
(defn ^:export main | ||
"Start the devcards UI." | ||
[] | ||
; Add a special class to the body to signal we're in devcards mode. | ||
; We want to mostly use the same styles as the app, but might need to make | ||
; some exceptions. | ||
(js/document.body.classList.add "using-devcards") | ||
; Start the devcards UI. | ||
(start-devcard-ui!)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(ns app.cards.helpers) | ||
|
||
(defn testing-container | ||
"The container that should be used to render testing-library react components. | ||
We want to provide our own container so that the rendered devcards aren't used." | ||
[] | ||
(let [app-div (js/document.createElement "div")] | ||
(.setAttribute app-div "id" "testing-lib") | ||
(js/document.body.appendChild app-div))) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns app.cards.test-runner | ||
{:dev/always true} | ||
(:require [shadow.test.env :as env] | ||
[cljs.test :as ct] | ||
[shadow.test :as st] | ||
["jsdom-global" :as jsdom-global] | ||
; Import the devcards-runner ns to get all the tests imported by it. | ||
[app.cards.devcards-runner])) | ||
|
||
(defmethod ct/report [::ct/default :end-run-tests] [m] | ||
(if (ct/successful? m) | ||
(js/process.exit 0) | ||
(js/process.exit 1))) | ||
|
||
(defn run-tests [] | ||
(-> (env/get-test-data) | ||
(env/reset-test-data!)) | ||
(st/run-all-tests)) | ||
|
||
(defn main | ||
"Run tests in a jsom environment." | ||
[] | ||
; Set jsdom to mock a dom environment. | ||
(jsdom-global) | ||
; Run the tests | ||
(run-tests)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
(ns app.core | ||
(:require [reagent.core :as r])) | ||
|
||
(defn app [] | ||
[:div "__NAME__ is running!"]) | ||
(:require [reagent.core :as r] | ||
[app.hello :refer [hello]])) | ||
|
||
(defn ^:dev/after-load start [] | ||
(r/render [app] (.getElementById js/document "app"))) | ||
(r/render [hello] (.getElementById js/document "app"))) | ||
|
||
(defn ^:export main [] | ||
(start)) |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
(ns app.hello | ||
(:require [reagent.core :as r])) | ||
|
||
(defn click-counter [click-count] | ||
[:div | ||
"The atom " [:code "click-count"] " has value: " | ||
@click-count ". " | ||
[:input {:type "button" :value "Click me!" | ||
:on-click #(swap! click-count inc)}]]) | ||
|
||
(def click-count (r/atom 0)) | ||
|
||
(defn hello [] | ||
[:<> | ||
[:p "Hello, __NAME__ is running!"] | ||
[:p "Here's an example of using a component with state:"] | ||
[click-counter click-count]]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(ns app.hello-cards | ||
(:require [reagent.core :as r] | ||
[devcards.core :as dc :refer [defcard deftest]] | ||
[cljs.test :include-macros true :refer [is]] | ||
["@testing-library/react" :refer [render cleanup fireEvent]] | ||
[app.hello :refer [click-counter hello]] | ||
[app.cards.helpers :refer [testing-container]])) | ||
|
||
(defcard | ||
"This is a live interactive development environment using [Devcards](https://github.com/bhauman/devcards). | ||
You can use it to design, test, and think about parts of your app in isolation. | ||
The two 'cards' below show the two components in this app.") | ||
|
||
(defcard hello-card | ||
(dc/reagent hello)) | ||
|
||
(defcard click-counter-card | ||
(dc/reagent click-counter) | ||
(r/atom 0) | ||
{:inspect-data true | ||
:frame true | ||
:history true}) | ||
|
||
(defcard | ||
"You can also add tests here and see their results. | ||
Below are some tests using [React Testing Library](https://testing-library.com/docs/react-testing-library/intro). | ||
Tests will be ran outside the browser when you run the test command.") | ||
|
||
(deftest hello-tests-card | ||
(let [tr (render (r/as-element [hello]) #js {:container (testing-container)})] | ||
(is (.queryByText tr #"Hello") "Should say 'Hello'") | ||
(cleanup))) | ||
|
||
(deftest click-counter-tests-card | ||
(let [atom (r/atom 0) | ||
element (r/as-element [click-counter atom]) | ||
tr (render element #js {:container (testing-container)})] | ||
(is (.queryByText tr #"has value: 0") "Should show the initial value as '0'") | ||
(.click fireEvent (.queryByText tr #"(?i)click me")) | ||
(r/flush) | ||
(is (.queryByText tr #"has value: 1") "Should show the value as '1' after click") | ||
(.click fireEvent (.queryByText tr #"(?i)click me")) | ||
(r/flush) | ||
(is (.queryByText tr #"has value: 2") "Should show the value as '2' after two clicks") | ||
(cleanup))) |