|
3 | 3 | [reagent.ratom :as r :refer-macros [reaction]] |
4 | 4 | [re-frame.subs :as subs] |
5 | 5 | [re-frame.db :as db] |
| 6 | + [re-frame.interop] |
6 | 7 | [re-frame.core :as re-frame])) |
7 | 8 |
|
8 | 9 | (test/use-fixtures :each {:before (fn [] (subs/clear-all-handlers!))}) |
|
87 | 88 | (swap! side-effect-atom inc) |
88 | 89 | (reaction @db))) |
89 | 90 |
|
90 | | - (let [test-sub (subs/subscribe [:side-effecting-handler])] |
91 | | - (reset! db/app-db :test) |
92 | | - (is (= :test @test-sub)) |
93 | | - (is (= @side-effect-atom 1)) |
94 | | - (subs/subscribe [:side-effecting-handler]) ;; this should be handled by cache |
95 | | - (is (= @side-effect-atom 1)) |
96 | | - (subs/subscribe [:side-effecting-handler :a]) ;; should fire again because of the param |
97 | | - (is (= @side-effect-atom 2)) |
98 | | - (subs/subscribe [:side-effecting-handler :a]) ;; this should be handled by cache |
99 | | - (is (= @side-effect-atom 2)))) |
| 91 | + (testing "Inside of a reactive context" |
| 92 | + (with-redefs [re-frame.interop/reactive? (constantly true)] |
| 93 | + (let [test-sub (subs/subscribe [:side-effecting-handler])] |
| 94 | + (reset! db/app-db :test) |
| 95 | + (is (= :test @test-sub)) |
| 96 | + (is (= @side-effect-atom 1)) |
| 97 | + (subs/subscribe [:side-effecting-handler]) ;; this should be handled by cache |
| 98 | + (is (= @side-effect-atom 1)) |
| 99 | + (subs/subscribe [:side-effecting-handler :a]) ;; should fire again because of the param |
| 100 | + (is (= @side-effect-atom 2)) |
| 101 | + (subs/subscribe [:side-effecting-handler :a]) ;; this should be handled by cache |
| 102 | + (is (= @side-effect-atom 2))))) |
| 103 | + |
| 104 | + (subs/clear-subscription-cache!) |
| 105 | + (reset! side-effect-atom 0) |
| 106 | + |
| 107 | + (testing "Outside a reactive context" |
| 108 | + (let [test-sub (subs/subscribe [:side-effecting-handler])] |
| 109 | + (is (= :test @test-sub)) |
| 110 | + (is (= @side-effect-atom 1) |
| 111 | + "Subscribed once") |
| 112 | + (subs/subscribe [:side-effecting-handler]) |
| 113 | + (is (= @side-effect-atom 2) |
| 114 | + "Subscribed twice and the cache was never populated.") |
| 115 | + (with-redefs [re-frame.interop/reactive? (constantly true)] |
| 116 | + (subs/subscribe [:side-effecting-handler :a]) |
| 117 | + (is (= @side-effect-atom 3) |
| 118 | + "Fires again because of the param, but this time the cache is populated")) |
| 119 | + (subs/subscribe [:side-effecting-handler :a]) |
| 120 | + (is (= @side-effect-atom 3) |
| 121 | + "The cache was already populated, so the cached reaction is used.")))) |
100 | 122 |
|
101 | 123 | ;============== test clear-subscription-cache! ================ |
102 | 124 |
|
|
105 | 127 | :clear-subscription-cache! |
106 | 128 | (fn clear-subs-cache [db _] 1)) |
107 | 129 |
|
108 | | - (testing "cold cache" |
109 | | - (is (nil? (subs/cache-lookup [:clear-subscription-cache!])))) |
110 | | - (testing "cache miss" |
111 | | - (is (= 1 @(subs/subscribe [:clear-subscription-cache!]))) |
112 | | - (is (some? (subs/cache-lookup [:clear-subscription-cache!])))) |
113 | | - (testing "clearing" |
114 | | - (subs/clear-subscription-cache!) |
115 | | - (is (nil? (subs/cache-lookup [:clear-subscription-cache!]))))) |
| 130 | + (with-redefs [re-frame.interop/reactive? (constantly true)] |
| 131 | + (testing "cold cache" |
| 132 | + (is (nil? (subs/cache-lookup [:clear-subscription-cache!])))) |
| 133 | + (testing "cache miss" |
| 134 | + (is (= 1 @(subs/subscribe [:clear-subscription-cache!]))) |
| 135 | + (is (some? (subs/cache-lookup [:clear-subscription-cache!])))) |
| 136 | + (testing "clearing" |
| 137 | + (subs/clear-subscription-cache!) |
| 138 | + (is (nil? (subs/cache-lookup [:clear-subscription-cache!])))))) |
116 | 139 |
|
117 | 140 | ;============== test register-pure macros ================ |
118 | 141 |
|
|
0 commit comments