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
8 changes: 6 additions & 2 deletions src/main/react_testing_library_cljs/async.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
(ns react-testing-library-cljs.async)

(defmacro deftest-async
"Like `cljs.test/deftest` but for async tests that return a Promise.
"Defines a `cljs.test/deftest` for an async test whose body returns a Promise.
Wraps the body in `promesa.core/do`, so each top-level form is awaited
before the next runs. Automatically calls `done` when the Promise resolves.
before the next runs, then calls `done` when the Promise resolves.

This is the compatibility path for toolchains without native ClojureScript
async functions. On shadow-cljs 3.4+ (ClojureScript 1.12.145+) prefer a
native `^:async` deftest with `await` instead.

Requires `promesa` on the classpath.

Expand Down
29 changes: 29 additions & 0 deletions src/test/react_testing_library_cljs/deftest_async_macro_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns react-testing-library-cljs.deftest-async-macro-test
"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.screen :as screen]))

(defn- render-el [element]
(rtl/render element))

(deftest-async find-by-text-test
(rtl/cleanup)
(render-el (react/createElement "div" nil "Macro Hello"))
(p/let [el (screen/find-by-text "Macro Hello")]
(is (some? el))
(is (= "Macro Hello" (.-textContent el)))))

(deftest-async find-all-by-text-test
(rtl/cleanup)
(render-el (react/createElement "div" nil
(react/createElement "span" nil "Item")
(react/createElement "span" nil "Item")))
(p/let [els (screen/find-all-by-text "Item")]
(is (vector? els))
(is (= 2 (count els)))))
27 changes: 0 additions & 27 deletions src/test/react_testing_library_cljs/native_async_test.cljs

This file was deleted.

14 changes: 7 additions & 7 deletions src/test/react_testing_library_cljs/screen_async_test.cljs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
(ns react-testing-library-cljs.screen-async-test
"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 [is]]
[promesa.core :as p]
[react-testing-library-cljs.async :refer-macros [deftest-async]]
[cljs.test :refer [deftest is]]
[react-testing-library-cljs.screen :as screen]))

(defn- render-el [element]
(rtl/render element))

(deftest-async find-by-text-test
(deftest ^:async find-by-text-test
(rtl/cleanup)
(render-el (react/createElement "div" nil "Async Hello"))
(p/let [el (screen/find-by-text "Async Hello")]
(let [el (await (screen/find-by-text "Async Hello"))]
(is (some? el))
(is (= "Async Hello" (.-textContent el)))))

(deftest-async find-all-by-text-test
(deftest ^:async find-all-by-text-test
(rtl/cleanup)
(render-el (react/createElement "div" nil
(react/createElement "span" nil "Item")
(react/createElement "span" nil "Item")))
(p/let [els (screen/find-all-by-text "Item")]
(let [els (await (screen/find-all-by-text "Item"))]
(is (vector? els))
(is (= 2 (count els)))
(is (= "Item" (.-textContent (first els))))))
67 changes: 67 additions & 0 deletions src/test/react_testing_library_cljs/user_event_async_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
(ns react-testing-library-cljs.user-event-async-test
"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.screen :as screen]
[react-testing-library-cljs.user-event :as user-event]))

(defn- render-el [element]
(rtl/render element))

(deftest ^:async click-test
(rtl/cleanup)
(let [clicks (atom 0)]
(render-el (react/createElement "button"
#js {:onClick #(swap! clicks inc)}
"Click me"))
(await (user-event/click (user-event/setup) (screen/get-by-role "button")))
(is (= 1 @clicks))))

(deftest ^:async type-test
(rtl/cleanup)
(render-el (react/createElement "input"
#js {:placeholder "type here"
:defaultValue ""}))
(await (user-event/type (user-event/setup) (screen/get-by-placeholder-text "type here") "hello"))
(is (= "hello" (.-value (screen/get-by-placeholder-text "type here")))))

(deftest ^:async clear-test
(rtl/cleanup)
(render-el (react/createElement "input"
#js {:placeholder "to clear"
:defaultValue "existing text"}))
(await (user-event/clear (user-event/setup) (screen/get-by-placeholder-text "to clear")))
(is (= "" (.-value (screen/get-by-placeholder-text "to clear")))))

(deftest ^:async tab-test
(rtl/cleanup)
(render-el (react/createElement "div" nil
(react/createElement "input" #js {:placeholder "first"})
(react/createElement "input" #js {:placeholder "second"})))
(.focus (screen/get-by-placeholder-text "first"))
(await (user-event/tab (user-event/setup)))
(is (= (screen/get-by-placeholder-text "second") (.-activeElement js/document))))

(deftest ^:async keyboard-test
(rtl/cleanup)
(let [submitted (atom false)]
(render-el (react/createElement "form"
#js {:onSubmit (fn [e]
(.preventDefault e)
(reset! submitted true))}
(react/createElement "input" #js {:placeholder "press enter"})))
(.focus (screen/get-by-placeholder-text "press enter"))
(await (user-event/keyboard (user-event/setup) "{Enter}"))
(is (true? @submitted))))

(deftest ^:async select-options-test
(rtl/cleanup)
(render-el (react/createElement "select"
#js {:defaultValue ""}
(react/createElement "option" #js {:value "a"} "Option A")
(react/createElement "option" #js {:value "b"} "Option B")))
(await (user-event/select-options (user-event/setup) (screen/get-by-role "combobox") "Option A"))
(is (= "a" (.-value (screen/get-by-role "combobox")))))
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(ns react-testing-library-cljs.user-event-promesa-test
"Exercises user-event interactions with the `deftest-async` macro + `promesa`.
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]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns react-testing-library-cljs.within-native-async-test
"Native ClojureScript async tests (`^:async` / `await`) for `within`.
Requires shadow-cljs >= 3.4.x (ClojureScript >= 1.12.145)."
(ns react-testing-library-cljs.within-async-test
"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]
Expand Down
Loading