Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ For more information about the principles and concepts behind the testing librar

- The `react-testing-library-cljs.screen` namespace provides a wrapper around the `react-testing-library`'s `screen` object, making it easier to interact with rendered components.

- The `react-testing-library-cljs.render` namespace wraps `render`, `cleanup`, and `act` from `@testing-library/react` so plain-React (non-Reagent) tests don't import the JS module directly. Reagent users should use `react-testing-library-cljs.reagent.render` instead.

- The `react-testing-library-cljs.fire-event` namespace simplifies firing events on rendered components, allowing you to simulate user interactions.

- The `react-testing-library-cljs.within` namespace scopes queries to a specific element, useful when multiple similar elements exist in the DOM.
Expand Down
7 changes: 5 additions & 2 deletions examples/node-test-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ no browser required. This is the simplest setup and runs end to end in CI.

## Setup essentials

- `shadow-cljs.edn` declares `reagent` and the library as `:dependencies` and a
`:node-test` build with an `app.test-setup` preload.
- `shadow-cljs.edn` declares `reagent` as a dependency and a `:node-test` build
with an `app.test-setup` preload. It references the library's source in this
repo via `:source-paths` (so the example always tracks HEAD); a standalone
project would add the Clojars dependency instead — see the repository root
README.
- `src/app/test_setup.cljs` registers jsdom (`global-jsdom/register`) and sets
`IS_REACT_ACT_ENVIRONMENT`, giving the tests a DOM and reliable React updates.

Expand Down
9 changes: 6 additions & 3 deletions examples/node-test-example/shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
:ns-regexp "-test$"
:output-to "out/test.js"
:devtools {:preloads [app.test-setup]}}}
:dependencies [[reagent "2.0.1"]
[org.clojars.olecve/react-testing-library-cljs "0.0.20"]]
:source-paths ["src"]}
:dependencies [[reagent "2.0.1"]]
;; Reference the library's source in this repo directly (instead of the
;; published Clojars dependency) so the example always tracks HEAD and needs
;; no release. A standalone project would add the Clojars dependency instead
;; (see the repository root README).
:source-paths ["src" "../../src/main"]}
2 changes: 1 addition & 1 deletion examples/node-test-example/src/app/plain_react_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"Testing plain React (no Reagent): raw createElement with user-event and
async queries. Uses native ClojureScript async (`^:async` / `await`)."
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]
[react-testing-library-cljs.user-event :as user-event]))

Expand Down
28 changes: 28 additions & 0 deletions src/main/react_testing_library_cljs/render.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns react-testing-library-cljs.render
"Wraps @testing-library/react's render, cleanup, and act for plain-React tests.
Reagent users should use `react-testing-library-cljs.reagent.render` instead."
(:require
["@testing-library/react" :as rtl]))

(defn render
"Renders a React element and returns the @testing-library/react result.

See [render](https://testing-library.com/docs/react-testing-library/api/#render)."
([element]
(rtl/render element))
([element options]
(rtl/render element (clj->js options))))

(defn cleanup
"Unmounts anything rendered by `render`; call it between tests.

See [cleanup](https://testing-library.com/docs/react-testing-library/api/#cleanup)."
[]
(rtl/cleanup))

(defn act
"Runs `callback` inside React's act() so updates and effects flush.

See [act](https://testing-library.com/docs/react-testing-library/api/#act)."
[callback]
(rtl/act callback))
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"Smoke-tests the `deftest-async` macro, the pre-3.4 compatibility path.
Requires `promesa`; native `^:async` tests need shadow-cljs 3.4+ instead."
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [is]]
[promesa.core :as p]
[react-testing-library-cljs.async :refer-macros [deftest-async]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]))

(defn- render-el [element]
Expand Down
2 changes: 1 addition & 1 deletion src/test/react_testing_library_cljs/fire_event_test.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns react-testing-library-cljs.fire-event-test
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is testing]]
[react-testing-library-cljs.fire-event :as fire-event]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]))

(defn- counter-component []
Expand Down
2 changes: 1 addition & 1 deletion src/test/react_testing_library_cljs/screen_async_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"Exercises `screen`'s async queries with native async (`^:async` / `await`).
Requires shadow-cljs 3.4+ (ClojureScript 1.12.145+)."
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]))

(defn- render-el [element]
Expand Down
2 changes: 1 addition & 1 deletion src/test/react_testing_library_cljs/screen_test.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns react-testing-library-cljs.screen-test
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is testing]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]))

(defn- render-el [element]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"Exercises user-event interactions with native async (`^:async` / `await`).
Requires shadow-cljs 3.4+ (ClojureScript 1.12.145+)."
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]
[react-testing-library-cljs.user-event :as user-event]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
This is the pre-3.4 compatibility path; the native equivalents live in
`user-event-async-test`."
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [is]]
[promesa.core :as p]
[react-testing-library-cljs.async :refer-macros [deftest-async]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]
[react-testing-library-cljs.user-event :as user-event]))

Expand Down
2 changes: 1 addition & 1 deletion src/test/react_testing_library_cljs/user_event_test.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns react-testing-library-cljs.user-event-test
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is async]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]
[react-testing-library-cljs.user-event :as user-event]))

Expand Down
2 changes: 1 addition & 1 deletion src/test/react_testing_library_cljs/within_async_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"Exercises `within`'s async queries with native async (`^:async` / `await`).
Requires shadow-cljs 3.4+ (ClojureScript 1.12.145+)."
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]
[react-testing-library-cljs.within :as within]))

Expand Down
2 changes: 1 addition & 1 deletion src/test/react_testing_library_cljs/within_test.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns react-testing-library-cljs.within-test
(:require
["@testing-library/react" :as rtl]
["react" :as react]
[cljs.test :refer [deftest is testing]]
[react-testing-library-cljs.render :as rtl]
[react-testing-library-cljs.screen :as screen]
[react-testing-library-cljs.within :as within]))

Expand Down
Loading