8000 test: add reagent tests · homebaseio/homebase-react@509a14c · GitHub
[go: up one dir, main page]

Skip to content

Commit 509a14c

Browse files
test: add reagent tests
1 parent 6a97768 commit 509a14c

File tree

5 files changed

+258
-27
lines changed

5 files changed

+258
-27
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@babel/preset-react": "^7.12.13",
4141
"@commitlint/cli": "^11.0.0",
4242
"@commitlint/config-conventional": "^11.0.0",
43+
"@peculiar/webcrypto": "^1.1.7",
4344
"@semantic-release/changelog": "5.0.1",
4445
"@semantic-release/commit-analyzer": "8.0.1",
4546
"@semantic-release/git": "9.0.0",
@@ -67,11 +68,13 @@
6768
"eslint-plugin-react-hooks": "^4.2.0",
6869
"firebase": "^8.0.2",
6970
"firebaseui": "^4.7.1",
71+
"global-jsdom": "^8.1.0",
7072
"highlight.js": "10.4.1",
7173
"hoist-non-react-statics": "^3.3.0",
7274
"husky": "5.0.0-beta.0",
7375
"jest": "26.6.0",
7476
"jest-performance-testing": "^1.0.0",
77+
"jsdom": "^16.6.0",
7578
"marked": "2.0.0",
7679
"pinst": "2.0.0",
7780
"prettier": "^2.2.1",

src/dev/example/reagent/todo.cljs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,37 @@
55
[homebase.reagent :as hbr]
66
[datalog-console.integrations.datascript :as datalog-console]))
77

8-
(def db-conn (d/create-conn {:db/ident {:db/unique :db.unique/identity}
9-
:todo/project {:db/type :db.type/ref
10-
:db/cardinality :db.cardinality/one}
11-
:todo/owner {:db/type :db.type/ref
12-
:db/cardinality :db.cardinality/one}}))
8+
(def schema {:db/ident {:db/unique :db.unique/identity}
9+
:todo/project {:db/type :db.type/ref
10+
:db/cardinality :db.cardinality/one}
11+
:todo/owner {:db/type :db.type/ref
12+
:db/cardinality :db.cardinality/one}})
1313

14-
(d/transact! db-conn [{:db/ident :todo.filters
15-
:todo.filter/show-completed? true
16-
:todo.filter/owner 0
17-
:todo.filter/project 0}
18-
{:todo/name "Go home"
19-
:todo/created-at (js/Date.now)
20-
:todo/owner -2
21-
:todo/project -3}
22-
{:todo/name "Fix ship"
23-
:todo/completed? true
24-
:todo/created-at (js/Date.now)
25-
:todo/owner -1
26-
:todo/project -4}
27-
{:db/id -1
28-
:user/name "Stella"}
29-
{:db/id -2
30-
:user/name "Arpegius"}
31-
{:db/id -3
32-
:project/name "Do it"}
33-
{:db/id -4
34-
:project/name "Make it"}])
14+
(def db-conn (d/create-conn schema))
15+
16+
(def initial-tx [{:db/ident :todo.filters
17+
:todo.filter/show-completed? true
18+
:todo.filter/owner 0
19+
:todo.filter/project 0}
20+
{:todo/name "Go home"
21+
:todo/created-at (js/Date.now)
22+
:todo/owner -2
23+
:todo/project -3}
24+
{:todo/name "Fix ship"
25+
:todo/completed? true
26+
:todo/created-at (js/Date.now)
27+
:todo/owner -1
28+
:todo/project -4}
29+
{:db/id -1
30+
:user/name "Stella"}
31+
{:db/id -2
32+
:user/name "Arpegius"}
33+
{:db/id -3
34+
:project/name "Do it"}
35+
{:db/id -4
36+
:project/name "Make it"}])
37+
38+
(d/transact! db-conn initial-tx)
3539

3640
(hbr/connect! db-conn)
3741

src/homebase/reagent_test.cljs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
(ns homebase.reagent-test
2+
(:require
3+
[homebase.test-polyfills]
4+
[reagent.core :as r]
5+
[datascript.core :as d]
6+
[homebase.reagent :as hbr]
7+
[clojure.test :refer [deftest testing is use-fixtures]]
8+
[dev.example.reagent.counter :as counter]
9+
[dev.example.reagent.todo :as todo]
10+
["@testing-library/react" :as rt]))
11+
12+
(set! *warn-on-infer* false)
13+
14+
(use-fixtures :each
15+
{:after rt/cleanup})
16+
17+
;; Idea from https://github.com/reagent-project/reagent/blob/master/test/reagenttest/utils.cljs
18+
(defn with-mounted-component [comp f]
19+
(let [mounted-component (rt/render (r/as-element comp))]
20+
(try
21+
(f mounted-component)
22+
(finally
23+
(.unmount mounted-component)
24+
(r/flush)))))
25+
26+
(defn click-element [el]
27+
(.click rt/fireEvent el)
28+
(r/flush))
29+
30+
(deftest test-counter
31+
(do
32+
(reset! counter/db-conn @(d/create-conn))
33+
(d/transact! counter/db-conn [[:db/add 1 :count 0]])
34+
(hbr/connect! counter/db-conn)
35+
(with-mounted-component
36+
[counter/counter]
37+
(fn [^js/React component]
38+
(testing "The count should start at 0"
39+
(is (not (nil? (.getByText component "Count: 0")))))
40+
(testing "The count should inc by 1"
41+
(click-element (.getByText component "Increment"))
42+
(is (not (nil? (.getByText component "Count: 1")))))
43+
(testing "The count should inc by 2 more"
44+
(click-element (.getByText component "Increment"))
45+
(click-element (.getByText component "Increment"))
46+
(is (not (nil? (.getByText component "Count: 3")))))))))
47+
48+
(deftest test-todo
49+
(do
50+
(reset! todo/db-conn @(d/create-conn todo/schema))
51+
(d/transact! todo/db-conn todo/initial-tx)
52+
(hbr/connect! todo/db-conn)
53+
(with-mounted-component
54+
[todo/todo-app]
55+
(fn [component]
56+
(testing "render list"
57+
(is (not (nil? (.getByDisplayValue component "Go home"))))
58+
(is (not (nil? (.getByDisplayValue component "Fix ship")))))
59+
(testing "query updates list on filter change"
60+
(d/transact! todo/db-conn [{:db/id (:db/id (d/entity @todo/db-conn [:db/ident :todo.filters]))
61+
:todo.filter/show-completed? false}])
62+
(r/flush)
63+
(is (not (nil? (.getByDisplayValue component "Go home"))))
64+
(is (thrown-with-msg?
65+
js/Error
66+
#"Unable to find an element with the display value: Fix ship"
67+
(.getByDisplayValue component "Fix ship")))
68+
(d/transact! todo/db-conn [{:db/id (:db/id (d/entity @todo/db-conn [:db/ident :todo.filters]))
69+
:todo.filter/show-completed? true}])
70+
(r/flush))
71+
(testing "deletion"
72+
(click-element (nth (.getAllByText component "Delete") 0))
73+
(is (thrown-with-msg?
74+
js/Error
75+
#"Unable to find an element with the display value: Fix ship."
76+
(.getByDisplayValue component "Fix ship")))
77+
(is (not (nil? (.getByDisplayValue component "Go home")))))
78+
(testing "insertion"
79+
(d/transact! todo/db-conn [{:todo/name "A new test todo" :todo/created-at (js/Date.now)}])
80+
(r/flush)
81+
(is (not (nil? (.getByDisplayValue component "A new test todo")))))))))

src/homebase/test_polyfills.cljs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(ns homebase.test-polyfills
2+
(:require
3+
["@peculiar/webcrypto" :refer [Crypto]]
4+
["jsdom" :refer [JSDOM]]))
5+
6+
; nano-id node.js polyfill
7+
(set! js/crypto (Crypto.))
8+
9+
; jsdom polyfill
10+
(def dom (JSDOM. "<!DOCTYPE html>" #js {:pretendToBeVisual true}))
11+
(set! js/window dom.window)
12+
(set! js/document dom.window.document)
13+
(set! js/navigator #js {:userAgent "node.js"})

0 commit comments

Comments
 (0)
0