/
electric_ui4_typeahead_test.cljc
257 lines (221 loc) · 9.67 KB
/
electric_ui4_typeahead_test.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
(ns hyperfiddle.electric-ui4-typeahead-test
(:require
#?(:cljs [contrib.dom-test-helpers :as uit])
[contrib.cljs-target :refer [do-browser]]
[hyperfiddle.electric-ui4 :as ui]
[hyperfiddle.rcf :as rcf :refer [% tests with tap]]
[hyperfiddle.electric :as p]
[clojure.string :as str]
[missionary.core :as m]
[hyperfiddle.electric-dom2 :as dom])
(:import
[missionary Cancelled]
[hyperfiddle.electric Pending]))
(def data {:alice {:name "Alice B"}
:bob {:name "Bob C"}
:charlie {:name "Charlie D"}
:derek {:name "Derek E"}})
(defn q [search] (into [] (keep (fn [[k {nm :name}]] (when (str/includes? nm search) k))) data))
(tests
(q "B") := [:alice :bob]
(q "X") := [])
#?(:cljs
(defn ->selection
([node] (->selection node nil))
([node selector]
(fn [transformer pred]
(m/sp
(loop [attempt 1]
(let [nodes (if selector (vec (.querySelectorAll node selector)) node)]
(when-not (try (pred (transformer nodes)) (catch :default _))
(when (= 5 attempt)
(let [v (try [:ok (transformer nodes)] (catch :default e [:err e]))]
(throw (ex-info "failed to assert on selection" {:value v, :pred pred}))))
(m/? (m/sleep (* 10 attempt attempt))) (recur (inc attempt))))))))))
#?(:cljs
(defn holds
([selection pred] (holds selection identity pred))
([selection transformer pred] (selection transformer pred))))
#?(:cljs (defn get-input [tphd] (-> tphd (.getElementsByTagName "input") first)))
#?(:cljs (def options-selector "ul > li"))
#?(:cljs (defn get-options [tphd] (vec (.querySelectorAll tphd options-selector))))
#?(:cljs (defn get-option [tphd s] (some #(when (= s (.-innerText %)) %) (get-options tphd))))
#?(:cljs
(do-browser
(tests "basic behavior"
(def !tphd (atom :missing))
(def discard (p/run (try
(binding [dom/node js/document.body]
(p/server
(let [!v (atom :alice)]
(ui/typeahead (p/watch !v)
(p/fn [v] (p/client (tap [:V! v])) (reset! !v v))
(p/fn [search] (p/client (tap [:Options search])) (q search))
(p/fn [id] (p/client (tap [:OptionLabel id])) (-> data id :name))
#_for-test (reset! !tphd dom/node)))))
(catch Pending _)
(catch Cancelled _)
(catch :default e (prn e)))))
"initially OptionLabel runs on the controlled value"
% := [:OptionLabel :alice]
(def tphd @!tphd)
(def input (get-input tphd))
(some? input) := true
"input has the correct value"
(.-value input) := "Alice B"
"when focused Options runs along with OptionLabel for each result"
(uit/focus input)
% := [:Options ""]
(hash-set % % % %) := #{[:OptionLabel :alice] [:OptionLabel :bob] [:OptionLabel :charlie] [:OptionLabel :derek]}
"when we click on an option V! runs, OptionLabel recalculates the new string and puts it into the input"
(uit/click (get-option tphd "Charlie D"))
% := [:V! :charlie]
% := [:OptionLabel :charlie]
(.-value input) := "Charlie D"
(discard)
)))
#?(:cljs
(do-browser
(tests "controlled value"
(def !tphd (atom :missing))
(def !v (atom :alice))
(def discard (p/run (try
(binding [dom/node js/document.body]
(let [v (p/watch !v)]
(tap [:controlled-value v])
(p/server
(ui/typeahead v
(p/fn [v] (reset! !v v))
(p/fn [search] (q search))
(p/fn [id] (tap [:OptionLabel id]) (-> data id :name))
#_for-test (reset! !tphd dom/node)))))
(catch Pending _)
(catch Cancelled _)
(catch :default e (prn e)))))
"initially OptionLabel runs on the controlled value"
% := [:controlled-value :alice]
% := [:OptionLabel :alice]
(def tphd @!tphd)
(def input (get-input tphd))
(some? input) := true
"input has the correct value"
(.-value input) := "Alice B"
"when picking OptionLabel runs for each result"
(uit/focus input)
(hash-set % % % %) := #{[:OptionLabel :alice] [:OptionLabel :bob] [:OptionLabel :charlie] [:OptionLabel :derek]}
"while picking controlled value changes have no effect"
(reset! !v :bob)
% := [:controlled-value :bob] ; no OptionLabel call here
"after picking we see new value flow through"
(uit/click (get-option tphd "Charlie D"))
% := [:controlled-value :charlie]
% := [:OptionLabel :charlie]
(discard)
)))
#?(:cljs
(do-browser
(tests "close when clicked outside"
(def !tphd (atom :missing))
(def discard (p/run (try
(binding [dom/node js/document.body]
(p/server
(let [!v (atom :alice)]
(ui/typeahead (p/watch !v)
(p/fn [v] (reset! !v v))
(p/fn [search] (q search))
(p/fn [id] (-> data id :name))
#_for-test (reset! !tphd dom/node)))))
(catch Pending _)
(catch Cancelled _)
(catch :default e (prn e)))))
(def tphd @!tphd)
(def input (get-input tphd))
(some? input) := true
"input has the correct value"
(.-value input) := "Alice B"
"when we click outside of an open typeahead it closes and reverts value"
(uit/focus input)
(uit/set-value! input "")
(.-value input) := ""
(uit/click (.querySelector js/document ".hyperfiddle-modal-backdrop"))
((m/sp
(try
(m/? (-> (->selection tphd options-selector) (holds count zero?)))
(m/? (-> (->selection input) (holds #(.-value %) #{"Alice B"})))
(finally (discard))))
identity prn))))
#?(:cljs
(do-browser
(tests "keyboard"
(def !tphd (atom :missing))
(def discard (p/run (try
(binding [dom/node js/document.body]
(p/server
(let [!v (atom :alice)]
(ui/typeahead (p/watch !v)
(p/fn [v] (reset! !v v))
(p/fn [search] (q search))
(p/fn [id] (-> data id :name))
#_for-test (reset! !tphd dom/node)))))
(catch Pending _)
(catch Cancelled _)
(catch :default e (prn e)))))
(def tphd @!tphd)
(def input (get-input tphd))
(some? input) := true
"input has the correct value"
(.-value input) := "Alice B"
"escape closes typeahead"
(uit/focus input)
(count (get-options tphd)) := (count data)
(uit/press (get-option tphd "Bob C") "Escape")
(count (get-options tphd)) := 0
"down arrow & Enter selects Bob"
(uit/focus input)
(count (get-options tphd)) := (count data)
(uit/press tphd "ArrowDown")
(uit/press tphd "Enter")
(count (get-options tphd)) := 0
(.-value input) := "Bob C"
"Enter on bad input is noop"
(def original-value (.-value input))
(uit/focus input)
(count (get-options tphd)) := (count data)
(uit/set-value! input "xyz")
(count (get-options tphd)) := 0
(uit/press tphd "Enter")
(.-value input) := "xyz" ; noop
(uit/press tphd "Escape") ; reverts
(.-value input) := original-value
"typing into input filters correctly"
(uit/focus input)
(uit/set-value! input "B")
(into #{} (map #(.-innerText %)) (get-options tphd)) := #{"Alice B" "Bob C"}
(uit/press tphd "Escape")
(discard)
)))
#?(:cljs
(do-browser
(tests "truncated result"
(def !tphd (atom :missing))
(def discard (p/run (try
(binding [dom/node js/document.body]
(p/server
(let [!v (atom :alice)]
(ui/typeahead (p/watch !v)
(p/fn [v] (reset! !v v))
(p/fn [search] (take 30 (cycle (q search))))
(p/fn [id] (-> data id :name))
#_for-test (reset! !tphd dom/node)))))
(catch Pending _)
(catch Cancelled _)
(catch :default e (prn e)))))
(def tphd @!tphd)
(def input (get-input tphd))
(some? input) := true
"when there's too many options we see a signifier telling we should refine our query"
(uit/focus input)
((m/sp
(try (m/? (-> (->selection tphd "ul > div") (holds #(some-> % first .-innerText) #{"refine your query..."})))
(finally (discard))))
identity prn))))