-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_harness.cljs
179 lines (163 loc) · 6.24 KB
/
test_harness.cljs
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
(ns nsfw.test-harness
(:require [dommy.core :as dommy]
[reagent.core :as r]
[nsfw.util :as nu]
[nsfw.css :as nc]
[nsfw.components :as comps]
[cljs.core.async :as async
:refer [<! >! chan close! sliding-buffer put! take! alts! timeout pipe mult tap]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(nc/inject-css-defs
{:sizes {:xs 5 :sm 10 :md 20 :lg 50 :xl 100}
:fonts {:header "'Helvetica Neue', Helvetica, sans-serif"
:copy "'Helvetica Neue', Helvetica, sans-serif"
:impact "'Helvetica Neue', Helvetica, sans-serif"}})
(defonce !current (r/atom nil))
#_(when (:play-all? @!current)
(swap! !current :play-plans (cycle (:plans @!current)))
(go-loop []
(when (:play-all? @!current)
(swap! !current assoc
:selected-plan (first (:play-plans @!current))
:play-plans (reset (:play-plans @!current)))
(reload!)
(<! (timeout 2000))
(recur))))
(defn on-keydown [e]
(when (= 27 (.. e -keyCode))
(swap! !current assoc :open? false)))
(defn reload! []
(doseq [selector (:component-selectors @!current)]
(doseq [$el (dommy/sel selector)]
(r/unmount-component-at-node $el)))
(dommy/unlisten! js/window :keydown on-keydown)
(when-let [f (-> @!current :reload-fn)]
(f)))
(defn set-reload-fn! [f]
(swap! !current assoc :reload-fn f))
(defn set-component-selectors! [sels]
(swap! !current assoc :component-selectors sels))
(defn set-plans! [plans]
(swap! !current assoc :plans plans))
(defn plan->id [plan]
(str (:section plan) (:title plan)))
(defn $state-editor [state-refs]
(let [!state (:!state state-refs)]
[:pre
{:style {:width "100%"
:height 400
:color 'black}}
(nu/pp-str
@!state)]))
(defn $console-root []
(let [{:keys [plans console-opts open?
selected-plan-id]}
@!current
{:keys [position]} console-opts]
[:div
{:style (merge
{:position 'fixed
:z-index 10000
:min-width 150
:background-color 'black
:color 'white}
(condp = position
:top-right {:top 0
:right 0}
{:bottom 0
:left 0}))}
[:div
{:style (merge
nc/flex-right
{:cursor 'pointer})}
[:div
{:style {:padding (str xspx " " smpx)}
:on-click (fn [e]
(let [same-key? (= :edit-state (:tab-key @!current))]
(.preventDefault e)
(swap! !current
(fn [current]
(-> current
(update-in [:open?] not)
(assoc-in [:tab-key] :edit-state))))
nil))}
"State"]
[:div
{:style (merge pad-xs)
:on-click (fn [e]
(let [same-key? (= :test-state-picker (:tab-key @!current))]
(.preventDefault e)
(swap! !current
(fn [current]
(-> current
(update-in [:open?] not)
(assoc-in [:tab-key] :test-state-picker))))
nil))}
"Test States"]]
(when open?
[:div
{:style {:border-top "solid white 1px"}}
[comps/$cstack
{:view (:tab-key @!current)
:transition :quickfade}
[{:key :edit-state
:comp (fn [nav-to]
[$state-editor (:state-refs @!current)])}
{:key :test-state-picker
:comp (fn [nav-to]
[:div
#_[:a
{:href "#"
:style {:display 'block
:color 'white
:padding "5px 10px"}
:on-click (fn [e]
(.preventDefault e)
(swap! !current update :play-all? not)
nil)}
"Play All"]
(->> plans
(map (fn [{:keys [title section] :as plan}]
[:a
{:key (str section "-" title)
:href "#"
:style {:display 'block
:color 'white
:padding "5px 10px"
:font-weight (if (= selected-plan-id (plan->id plan))
'bold
'normal)}
:on-click (fn [e]
(.preventDefault e)
(swap! !current assoc :selected-plan-id (plan->id plan))
(reload!)
nil)}
title])))])}]]])]))
(defn attach-console! [opts]
(when-let [$console (dommy/sel1 "#test-harness-console")]
(r/unmount-component-at-node (dommy/sel1 "#test-harness-console"))
(dommy/remove! $console))
(let [$test-harness-console
(dommy/set-attr! (dommy/create-element :div) :id "test-harness-console")]
(dommy/append! (dommy/sel1 "body") $test-harness-console)
(swap! !current assoc :console-opts opts)
(dommy/listen! js/window :keydown on-keydown)
(r/render-component
[$console-root]
$test-harness-console)))
(defn selected-plan! []
(->> @!current
:plans
(filter #(= (str (:section %) (:title %))
(:selected-plan-id @!current)))
first))
(defn init! [{:keys [component-selectors console plans state-refs]}]
(set-component-selectors! component-selectors)
(attach-console! console)
(set-plans! plans)
(swap! !current assoc :state-refs state-refs))
;; Usage
#_ (th/init!
{:component-selectors (keys components)
:console {:position :top-right}
:plans test/EMPORIUM_ACCOUNT_TEST_PLANS})