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" )))))))))
0 commit comments