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
3 changes: 2 additions & 1 deletion bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:tasks
{generate:screen {:task (shell {:dir "scripts"} "bb" "generate_screen.clj")}
generate:fire-event {:task (shell {:dir "scripts"} "bb" "generate_fire_event.clj")}
generate {:depends [generate:screen generate:fire-event]}
generate:within {:task (shell {:dir "scripts"} "bb" "generate_within.clj")}
generate {:depends [generate:screen generate:fire-event generate:within]}
release {:doc "Perform release"
:task (shell "lein release")}}}
19 changes: 19 additions & 0 deletions scripts/common.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(require '[clojure.string :as str])

(defn insert-string [original string position]
(str (subs original 0 position)
string
(subs original position)))

(defn remove-string-between [string begin end]
(str (subs string 0 begin)
(subs string end)))

(defn camel-case->kebab-case [string]
(reduce
(fn [result char]
(if (= (str char) (str/upper-case char))
(str result "-" (str/lower-case char))
(str result char)))
""
string))
20 changes: 2 additions & 18 deletions scripts/generate_fire_event.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

(require '[clojure.string :as str])

(load-file "common.clj")

(def event-types
["copy"
"cut"
Expand Down Expand Up @@ -90,24 +92,6 @@
(def begin-marker ";; Begin - Generated Code (Do not modify manually)\n")
(def end-marker ";; End - Generated Code (Do not modify manually)\n")

(defn insert-string [original string position]
(str (subs original 0 position)
string
(subs original position)))

(defn remove-string-between [string begin end]
(str (subs string 0 begin)
(subs string end)))

(defn camel-case->kebab-case [string]
(reduce
(fn [result char]
(if (= (str char) (str/upper-case char))
(str result "-" (str/lower-case char))
(str result char)))
""
string))

(let [fire-event-source-file "../src/main/react_testing_library_cljs/fire_event.cljs"
fire-event-fn-template (slurp "fire_event_fn.template.clj")
event-type-fns (->> event-types
Expand Down
20 changes: 2 additions & 18 deletions scripts/generate_screen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

(require '[clojure.string :as str])

(load-file "common.clj")

(def screen-source-file "../src/main/react_testing_library_cljs/screen.cljs")

(def begin-marker ";; Begin - Generated Code (Do not modify manually)\n")
Expand All @@ -10,24 +12,6 @@
(def query-fn-template (slurp "query_fn.template.clj"))
(def query-all-fn-template (slurp "query_all_fn.template.clj"))

(defn insert-string [original string position]
(str (subs original 0 position)
string
(subs original position)))

(defn remove-string-between [string begin end]
(str (subs string 0 begin)
(subs string end)))

(defn camel-case->kebab-case [string]
(reduce
(fn [result char]
(if (= (str char) (str/upper-case char))
(str result "-" (str/lower-case char))
(str result char)))
""
string))

(let [query-types [{:type "getBy"
:docstring (str "Returns the matching element for a query.\n\n "
"Throws a descriptive error if no elements match or if more than one match is found.\n "
Expand Down
106 changes: 106 additions & 0 deletions scripts/generate_within.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env bb

(require '[clojure.string :as str])

(load-file "common.clj")

(def within-source-file "../src/main/react_testing_library_cljs/within.cljs")

(def begin-marker ";; Begin - Generated Code (Do not modify manually)\n")
(def end-marker ";; End - Generated Code (Do not modify manually)\n")

(def query-fn-template (slurp "within_query_fn.template.clj"))
(def query-all-fn-template (slurp "within_query_all_fn.template.clj"))

(let [query-types [{:type "getBy"
:docstring (str "Returns the matching element scoped to `element` for a query.\n\n "
"Throws a descriptive error if no elements match or if more than one match is found.\n "
"Use `get-all-by` instead if more than one element is expected.\n\n "
"- No match: Throws error\n "
"- One match: Returns element\n "
"- Multiple match: Throws error\n "
"- Async: No")
:template query-fn-template}
{:type "queryBy"
:docstring (str "Returns the matching element scoped to `element` for a query, or `nil` if no elements match.\n\n "
"Useful for asserting an element that is not present. Throws an error if more than one\n "
"match is found. Use `query-all-by` instead if this is OK.\n\n "
"- No match: Returns nil\n "
"- One match: Returns element\n "
"- Multiple match: Throws error\n "
"- Async: No")
:template query-fn-template}
{:type "findBy"
:docstring (str "Returns a promise which resolves when a matching element is found within `element`.\n\n "
"The promise is rejected if no element is found or if more than one element is found\n "
"after a default timeout of 1000ms. If you need to find more than one element, use\n "
"`find-all-by`. This is a combination of `get-by` queries and `waitFor`.\n\n "
"- No match: Rejects\n "
"- One match: Resolves with element\n "
"- Multiple match: Rejects\n "
"- Async: Yes")
:template query-fn-template}
{:type "getAllBy"
:docstring (str "Returns a vector of all matching elements scoped to `element` for a query.\n\n "
"Throws an error if no elements match.\n\n "
"- No match: Throws error\n "
"- One match: Returns vector\n "
"- Multiple match: Returns vector\n "
"- Async: No")
:template query-all-fn-template}
{:type "queryAllBy"
:docstring (str "Returns a vector of all matching elements scoped to `element` for a query.\n\n "
"Returns an empty vector if no elements match.\n\n "
"- No match: Returns []\n "
"- One match: Returns vector\n "
"- Multiple match: Returns vector\n "
"- Async: No")
:template query-all-fn-template}
{:type "findAllBy"
:docstring (str "Returns a promise which resolves to a vector of matching elements scoped to `element`.\n\n "
"The promise is rejected if no elements are found after a default timeout of 1000ms.\n "
"This is a combination of `get-all-by` queries and `waitFor`.\n\n "
"- No match: Rejects\n "
"- One match: Resolves with vector\n "
"- Multiple match: Resolves with vector\n "
"- Async: Yes")
:template query-all-fn-template}]
query-values [{:by "Role"
:url "https://testing-library.com/docs/queries/byrole"}
{:by "LabelText"
:url "https://testing-library.com/docs/queries/bylabeltext"}
{:by "PlaceholderText"
:url "https://testing-library.com/docs/queries/byplaceholdertext"}
{:by "Text"
:url "https://testing-library.com/docs/queries/bytext"}
{:by "DisplayValue"
:url "https://testing-library.com/docs/queries/bydisplayvalue"}
{:by "AltText"
:url "https://testing-library.com/docs/queries/byalttext"}
{:by "Title"
:url "https://testing-library.com/docs/queries/bytitle"}
{:by "TestId"
:url "https://testing-library.com/docs/queries/bytestid"}]
queries (for [query-type query-types
query-value query-values]
(let [js-fn-name (str (:type query-type) (:by query-value))
cljs-fn-name (camel-case->kebab-case js-fn-name)]
{:cljs-fn-name cljs-fn-name
:js-fn-name js-fn-name
:url (:url query-value)
:docstring (:docstring query-type)
:template (:template query-type)}))
query-fns (->> queries
(map #(-> (:template %)
(str/replace "$cljs-fn-name" (:cljs-fn-name %))
(str/replace "$js-fn-name" (:js-fn-name %))
(str/replace "$docstring" (:docstring %))
(str/replace "$url" (:url %))))
(str/join "\n"))
source-file (slurp within-source-file)
begin-position (+ (str/index-of source-file begin-marker) (count begin-marker))
end-position (str/index-of source-file end-marker)
source-file-updated (-> source-file
(remove-string-between begin-position end-position)
(insert-string query-fns begin-position))]
(spit within-source-file source-file-updated))
8 changes: 8 additions & 0 deletions scripts/within_query_all_fn.template.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(defn $cljs-fn-name
"$docstring

See [$js-fn-name]($url)."
([element matcher]
(vec (.$js-fn-name (within element) matcher)))
([element matcher options]
(vec (.$js-fn-name (within element) matcher (clj->js options)))))
8 changes: 8 additions & 0 deletions scripts/within_query_fn.template.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(defn $cljs-fn-name
"$docstring

See [$js-fn-name]($url)."
([element matcher]
(.$js-fn-name (within element) matcher))
([element matcher options]
(.$js-fn-name (within element) matcher (clj->js options))))
1 change: 0 additions & 1 deletion src/main/react_testing_library_cljs/screen.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -815,5 +815,4 @@
(vec (.findAllByTestId screen matcher)))
([matcher options]
(vec (.findAllByTestId screen matcher (clj->js options)))))

;; End - Generated Code (Do not modify manually)
Loading