|
2 | 2 | (:require [cljs.test :refer-macros [is deftest]] |
3 | 3 | [day8.re-frame.async-flow-fx :as core])) |
4 | 4 |
|
| 5 | + |
5 | 6 | (deftest test-all-events-seen? |
6 | | - (is (= (core/seen-all-of? #{:a} #{:a}) true)) |
7 | | - (is (= (core/seen-all-of? #{:a} #{:a :b}) true)) |
8 | | - (is (= (core/seen-all-of? #{:a :b} #{:a :b}) true)) |
9 | | - (is (= (core/seen-all-of? #{:a} #{:b}) false)) |
10 | | - (is (= (core/seen-all-of? #{:a :b} #{:a :c}) false)) |
11 | | - (is (= (core/seen-all-of? #{:a} #{:b :c}) false)) |
| 7 | + (is (= (core/seen-all-of? #{:a} #{[:a]}) true)) |
| 8 | + (is (= (core/seen-all-of? #{:a} #{[:a] [:b]}) true)) |
| 9 | + (is (= (core/seen-all-of? #{:a :b} #{[:a] [:b]}) true)) |
| 10 | + (is (= (core/seen-all-of? #{:a} #{[:b]}) false)) |
| 11 | + (is (= (core/seen-all-of? #{:a :b} #{[:a] [:c]}) false)) |
| 12 | + (is (= (core/seen-all-of? #{:a} #{[:b] [:c]}) false)) |
12 | 13 | (is (= (core/seen-all-of? #{:a} #{}) false))) |
13 | 14 |
|
| 15 | +(deftest test-all-events-seen-vec? |
| 16 | + (is (= (core/seen-all-of? #{[:a]} #{[:a]}) true)) |
| 17 | + (is (= (core/seen-all-of? #{[:a]} #{[:a] [:b]}) true)) |
| 18 | + (is (= (core/seen-all-of? #{[:a] [:b]} #{[:a] [:b]}) true)) |
| 19 | + (is (= (core/seen-all-of? #{:a [:b]} #{[:a] [:b]}) true)) |
| 20 | + (is (= (core/seen-all-of? #{[:a] [:b :c]} #{[:a] [:b]}) false)) |
| 21 | + (is (= (core/seen-all-of? #{[:a]} #{[:b]}) false)) |
| 22 | + (is (= (core/seen-all-of? #{[:a] [:b]} #{[:a] [:c]}) false)) |
| 23 | + (is (= (core/seen-all-of? #{(fn [[e _]] |
| 24 | + (keyword? e))} #{[:b] [:c]}) true)) |
| 25 | + (is (= (core/seen-all-of? #{[:a]} #{}) false))) |
| 26 | + |
14 | 27 |
|
15 | 28 | (deftest test-any-events-seen? |
16 | | - (is (= (core/seen-any-of? #{:a} #{:a}) true)) |
17 | | - (is (= (core/seen-any-of? #{:a :b} #{:a :b}) true)) |
18 | | - (is (= (core/seen-any-of? #{:a :b} #{:a :c}) true)) |
19 | | - (is (= (core/seen-any-of? #{:a} #{:b}) false)) |
| 29 | + (is (= (core/seen-any-of? #{:a} #{[:a]}) true)) |
| 30 | + (is (= (core/seen-any-of? #{:a [:b]} #{[:a] [:b]}) true)) |
| 31 | + (is (= (core/seen-any-of? #{:a [:b]} #{[:a] [:c]}) true)) |
| 32 | + (is (= (core/seen-any-of? #{:a} #{[:b]}) false)) |
20 | 33 | (is (= (core/seen-any-of? #{:a} #{}) false))) |
21 | 34 |
|
| 35 | +(deftest test-any-events-seen-vec? |
| 36 | + (is (= (core/seen-any-of? #{[:a]} #{[:a]}) true)) |
| 37 | + (is (= (core/seen-any-of? #{[:a] [:b]} #{[:a] [:b]}) true)) |
| 38 | + (is (= (core/seen-any-of? #{[:a] [:b]} #{[:a] [:c]}) true)) |
| 39 | + (is (= (core/seen-any-of? #{[:a]} #{[:b]}) false)) |
| 40 | + (is (= (core/seen-any-of? #{[:a]} #{}) false))) |
22 | 41 |
|
23 | 42 | (deftest test-newly-startable-tasks |
24 | 43 | (let [rules [{:id 1 :when core/seen-all-of? :events #{:a :b}} |
25 | 44 | {:id 2 :when core/seen-all-of? :events #{:a}}]] |
26 | | - (is (= (core/startable-rules rules #{:c} #{}) |
| 45 | + (is (= (core/startable-rules rules #{[:c]} #{}) |
27 | 46 | [])) |
28 | | - (is (= (core/startable-rules rules #{:a} #{2}) |
| 47 | + (is (= (core/startable-rules rules #{[:a]} #{2}) |
29 | 48 | [])) |
30 | | - (is (= (core/startable-rules rules #{:a} #{1}) |
| 49 | + (is (= (core/startable-rules rules #{[:a]} #{1}) |
31 | 50 | [(nth rules 1)])) |
32 | | - (is (= (core/startable-rules rules #{:a :b} #{2}) |
| 51 | + (is (= (core/startable-rules rules #{[:a] [:b]} #{2}) |
33 | 52 | [(nth rules 0)])) |
34 | | - (is (= (core/startable-rules rules #{:a} #{}) |
| 53 | + (is (= (core/startable-rules rules #{[:a]} #{}) |
35 | 54 | [(nth rules 1)])))) |
36 | 55 |
|
37 | 56 |
|
|
73 | 92 |
|
74 | 93 | ;; event :no should cause nothing to happen |
75 | 94 | (is (= (handler-fn |
76 | | - {:db {:p {:seen-events #{:33} |
| 95 | + {:db {:p {:seen-events #{[:33]} |
77 | 96 | :rules-fired #{}}}} |
78 | 97 | [:test-id [:no]]) |
79 | | - {:db {:p {:seen-events #{:33 :no} |
80 | | - :rules-fired #{}}}})) |
| 98 | + {:db {:p {:seen-events #{[:33] [:no]} |
| 99 | + :rules-fired #{}}}})) |
81 | 100 |
|
82 | 101 | ;; new event should not cause a new dispatch because task is already started (:id 0 is in :rules-fired) |
83 | 102 | (is (= (handler-fn |
84 | | - {:db {:p {:seen-events #{:1} |
| 103 | + {:db {:p {:seen-events #{[:1]} |
85 | 104 | :rules-fired #{0}}}} |
86 | 105 | [:test-id [:1]]) |
87 | | - {:db {:p {:seen-events #{:1} :rules-fired #{0}}}})) |
| 106 | + {:db {:p {:seen-events #{[:1]} :rules-fired #{0}}}})) |
88 | 107 |
|
89 | 108 | ;; new event should cause a dispatch |
90 | 109 | (is (= (handler-fn |
91 | 110 | {:db {:p {:seen-events #{} |
92 | 111 | :rules-fired #{}}}} |
93 | 112 | [:test-id [:1]]) |
94 | | - {:db {:p {:seen-events #{:1} :rules-fired #{0}}} |
95 | | - :dispatch-n [[:2]]})) |
| 113 | + {:db {:p {:seen-events #{[:1]} :rules-fired #{0}}} |
| 114 | + :dispatch-n [[:2]]})) |
96 | 115 |
|
97 | 116 | ;; make sure :seen-any-of? works |
98 | 117 | (is (= (handler-fn |
99 | 118 | {:db {:p {:seen-events #{} |
100 | 119 | :rules-fired #{}}}} |
101 | 120 | [:test-id [:4]]) |
102 | | - {:db {:p {:seen-events #{:4} :rules-fired #{2}}} |
103 | | - :dispatch-n [[:6]]})))) |
| 121 | + {:db {:p {:seen-events #{[:4]} :rules-fired #{2}}} |
| 122 | + :dispatch-n [[:6]]})))) |
| 123 | + |
| 124 | + |
| 125 | +(deftest test-vector-handling |
| 126 | + (let [flow {:first-dispatch [:start] |
| 127 | + :id :test-id |
| 128 | + :db-path [:p] |
| 129 | + :rules [{:id 0 :when :seen? :events [[:1 :a]] :dispatch [:2]} |
| 130 | + {:id 2 :when :seen-any-of? :events [[:4 :b] :5] :dispatch [:6]} |
| 131 | + ]} |
| 132 | + handler-fn (core/make-flow-event-handler flow)] |
| 133 | + |
| 134 | + ;; new event should cause a dispatch |
| 135 | + (is (= (handler-fn |
| 136 | + {:db {:p {:seen-events #{} |
| 137 | + :rules-fired #{}}}} |
| 138 | + [:test-id [:1 :a]]) |
| 139 | + {:db {:p {:seen-events #{[:1 :a]} :rules-fired #{0}}} |
| 140 | + :dispatch-n [[:2]]})) |
104 | 141 |
|
| 142 | + ;; new event shouldn't cause a dispatch |
| 143 | + (is (= (handler-fn |
| 144 | + {:db {:p {:seen-events #{} |
| 145 | + :rules-fired #{}}}} |
| 146 | + [:test-id [:1]]) |
| 147 | + {:db {:p {:seen-events #{[:1]} :rules-fired #{}}}})) |
| 148 | + |
| 149 | + ;; make sure :seen-any-of? works |
| 150 | + (is (= (handler-fn |
| 151 | + {:db {:p {:seen-events #{} |
| 152 | + :rules-fired #{}}}} |
| 153 | + [:test-id [:4 :b]]) |
| 154 | + {:db {:p {:seen-events #{[:4 :b]} :rules-fired #{2}}} |
| 155 | + :dispatch-n [[:6]]})))) |
105 | 156 |
|
106 | 157 | (deftest test-halt1 |
107 | 158 | (let [flow {:first-dispatch [:start] |
|
113 | 164 | handler-fn (core/make-flow-event-handler flow)] |
114 | 165 | ;; halt event should clean up |
115 | 166 | (is (= (handler-fn |
116 | | - {:db {:p {:seen-events #{:1} |
| 167 | + {:db {:p {:seen-events #{[:1]} |
117 | 168 | :rules-fired #{0}}}} |
118 | 169 | [:test-id [:3]]) |
119 | 170 | {:db {} |
|
122 | 173 |
|
123 | 174 | ;; halt event should clean up and dispatch |
124 | 175 | (is (= (handler-fn |
125 | | - {:db {:p {:seen-events #{:1} |
| 176 | + {:db {:p {:seen-events #{[:1]} |
126 | 177 | :rules-fired #{0}}}} |
127 | 178 | [:test-id [:6]]) |
128 | 179 | {:db {} |
|
138 | 189 | :first-dispatch [:1] |
139 | 190 | :rules [{:when :seen? :events :3 :halt? true}]} |
140 | 191 | handler-fn (core/make-flow-event-handler flow)] |
141 | | - (is (= (handler-fn {:db {:p {:seen-events #{:33} :rules-fired #{}}}} |
| 192 | + (is (= (handler-fn {:db {:p {:seen-events #{[:33]} :rules-fired #{}}}} |
142 | 193 | [:blah [:3]]) |
143 | 194 | {:db {} |
144 | 195 | :deregister-event-handler :blah |
|
0 commit comments