events.cljs: Refactor

Reduce the overall nesting level in the test.  Each `testing'
statement now fits on a single screen, instead of spanning hundreds of
lines.

The final top-level expression went from 16 to 5 consecutive closing
parens.

Signed-off-by: Goran Jovic <goranjovic@gmail.com>
This commit is contained in:
Oleh Krehel 2018-02-05 22:57:39 +01:00 committed by Goran Jovic
parent 90863f1670
commit f4b33d47ee
No known key found for this signature in database
GPG Key ID: A778DE3CD7D2D10D

View File

@ -192,24 +192,21 @@
(rf/dispatch [:initialize-db]) (rf/dispatch [:initialize-db])
(let [contacts (rf/subscribe [:get-contacts]) (def contacts (rf/subscribe [:get-contacts]))
contact-groups (rf/subscribe [:get-contact-groups]) (def contact-groups (rf/subscribe [:get-contact-groups]))
view-id (rf/subscribe [:get :view-id])] (def view-id (rf/subscribe [:get :view-id]))
(testing ":load-contacts event" (testing ":load-contacts event"
;;Assert the initial state ;;Assert the initial state
(is (and (map? @contacts) (empty? @contacts))) (is (and (map? @contacts) (empty? @contacts)))
(rf/dispatch [:load-contacts])) (rf/dispatch [:load-contacts]))
(testing ":load-contact-groups event" (testing ":load-contact-groups event"
;;Assert the initial state ;;Assert the initial state
(is (and (map? @contact-groups) (empty? @contact-groups))) (is (and (map? @contact-groups) (empty? @contact-groups)))
(rf/dispatch [:load-contact-groups]) (rf/dispatch [:load-contact-groups])
(is (= {(:group-id test-contact-group) test-contact-group} (is (= {(:group-id test-contact-group) test-contact-group}
@contact-groups))) @contact-groups)))
@ -239,14 +236,14 @@
(testing "it does not add any other contact" (testing "it does not add any other contact"
(is (= 2 (count (keys @contacts)))))) (is (= 2 (count (keys @contacts))))))
(let [new-contact-public-key "0x048f7d5d4bda298447bbb5b021a34832509bd1a8dbe4e06f9b7223d00a59b6dc14f6e142b21d3220ceb3155a6d8f40ec115cd96394d3cc7c55055b433a1758dc74" (def new-contact-public-key "0x048f7d5d4bda298447bbb5b021a34832509bd1a8dbe4e06f9b7223d00a59b6dc14f6e142b21d3220ceb3155a6d8f40ec115cd96394d3cc7c55055b433a1758dc74")
new-contact-address "5392ccb49f2e9fef8b8068b3e3b5ba6c020a9aca" (def new-contact-address "5392ccb49f2e9fef8b8068b3e3b5ba6c020a9aca")
new-contact {:name "" (def new-contact {:name ""
:photo-path "" :photo-path ""
:whisper-identity new-contact-public-key :whisper-identity new-contact-public-key
:address new-contact-address} :address new-contact-address})
contact (rf/subscribe [:contact-by-identity new-contact-public-key]) (def contact (rf/subscribe [:contact-by-identity new-contact-public-key]))
current-chat-id (rf/subscribe [:get-current-chat-id])] (def current-chat-id (rf/subscribe [:get-current-chat-id]))
(testing ":add-contact-handler event - new contact" (testing ":add-contact-handler event - new contact"
@ -286,16 +283,15 @@
;; :watch-contact ;; :watch-contact
;;TODO :update-chat! ;;TODO :update-chat!
(rf/reg-event-db :update-chat! (fn [db _] db)) (rf/reg-event-db :update-chat! (fn [db _] db))
(def received-contact {:name "test"
(let [received-contact {:name "test"
:profile-image "" :profile-image ""
:address new-contact-address :address new-contact-address
:status "test status" :status "test status"
:fcm-token "0xwhatever"} :fcm-token "0xwhatever"})
received-contact' (merge new-contact (def received-contact1 (merge new-contact
(dissoc received-contact :profile-image) (dissoc received-contact :profile-image)
{:public-key new-contact-public-key {:public-key new-contact-public-key
:private-key ""})] :private-key ""}))
(rf/dispatch [:contact-request-received {:from new-contact-public-key (rf/dispatch [:contact-request-received {:from new-contact-public-key
:payload {:contact received-contact :payload {:contact received-contact
@ -303,8 +299,8 @@
:private ""}}}]) :private ""}}}])
(testing "it adds the new contact to the list of contacts" (testing "it adds the new contact to the list of contacts"
(is (= received-contact' (is (= received-contact1
(get @contacts new-contact-public-key)))) (get @contacts new-contact-public-key)))))
(testing ":contact-update-received event" (testing ":contact-update-received event"
@ -312,21 +308,21 @@
;; ;;
;; :update-contact! ;; :update-contact!
;;TODO :update-chat! ;;TODO :update-chat!
(let [timestamp (datetime/now-ms) (let [timestamp (datetime/now-ms)]
received-contact'' (assoc received-contact' (def received-contact2 (assoc received-contact1
:last-updated timestamp :last-updated timestamp
:status "new status" :status "new status"
:name "new name")] :name "new name"))
(rf/dispatch [:contact-update-received {:from new-contact-public-key (rf/dispatch [:contact-update-received {:from new-contact-public-key
:payload {:content {:profile {:profile-image "" :payload {:content {:profile {:profile-image ""
:status "new status" :status "new status"
:name "new name"}} :name "new name"}}
:timestamp timestamp}}]) :timestamp timestamp}}]))
(testing "it updates the contact and set the :last-updated key" (testing "it updates the contact and set the :last-updated key"
(is (= received-contact'' (is (= received-contact2
(get @contacts new-contact-public-key)))) (get @contacts new-contact-public-key)))))
(testing ":hide-contact event" (testing ":hide-contact event"
@ -339,7 +335,7 @@
(rf/dispatch [:hide-contact @contact]) (rf/dispatch [:hide-contact @contact])
(testing "it sets the pending? flag to true" (testing "it sets the pending? flag to true"
(is (= (assoc received-contact'' :pending? true) (is (= (assoc received-contact2 :pending? true)
(get @contacts new-contact-public-key))))) (get @contacts new-contact-public-key)))))
(testing ":add-contact-handler event - :add-pending-contact" (testing ":add-contact-handler event - :add-pending-contact"
@ -358,7 +354,7 @@
(rf/dispatch [:add-contact-handler]) (rf/dispatch [:add-contact-handler])
(testing "it sets the pending? flag to false" (testing "it sets the pending? flag to false"
(is (= (assoc received-contact'' :pending? false) (is (= (assoc received-contact2 :pending? false)
(get @contacts new-contact-public-key)))) (get @contacts new-contact-public-key))))
(testing "it loads the 1-1 chat" (testing "it loads the 1-1 chat"
@ -369,8 +365,7 @@
(testing ":create-new-contact-group event" (testing ":create-new-contact-group event"
(let [new-group-name "new group"] (def new-group-name "new group")
(rf/dispatch [:select-contact new-contact-public-key]) (rf/dispatch [:select-contact new-contact-public-key])
(rf/dispatch [:select-contact "demo-bot"]) (rf/dispatch [:select-contact "demo-bot"])
(rf/dispatch [:select-contact "browse"]) (rf/dispatch [:select-contact "browse"])
@ -380,27 +375,28 @@
(rf/dispatch [:deselect-contact "demo-bot"]) (rf/dispatch [:deselect-contact "demo-bot"])
(rf/dispatch [:deselect-contact new-contact-public-key]) (rf/dispatch [:deselect-contact new-contact-public-key])
(def new-group-id (->> @contact-groups
(let [new-group-id (->> @contact-groups
(vals) (vals)
(filter #(= (:name %) new-group-name)) (filter #(= (:name %) new-group-name))
(first) (first)
(:group-id)) (:group-id)))
new-group {:group-id new-group-id (def new-group {:group-id new-group-id
:name new-group-name :name new-group-name
:order 2 :order 2
:timestamp 0 :timestamp 0
:contacts [{:identity new-contact-public-key} :contacts [{:identity new-contact-public-key}
{:identity "demo-bot"}]} {:identity "demo-bot"}]})
groups-with-new-group {new-group-id new-group (def groups-with-new-group {new-group-id new-group
"dapps" dapps-contact-group "dapps" dapps-contact-group
(:group-id test-contact-group) test-contact-group}] (:group-id test-contact-group) test-contact-group})
(def groups-with-new-group1 (update groups-with-new-group new-group-id assoc :name "new group name"))
(def groups-with-new-group2 (-> groups-with-new-group1
(update new-group-id assoc :order 1)
(update "dapps" assoc :order 2)))
(rf/dispatch [:set-in [:group/contact-groups new-group-id :timestamp] 0]) (rf/dispatch [:set-in [:group/contact-groups new-group-id :timestamp] 0])
(is (= groups-with-new-group @contact-groups)) (is (= groups-with-new-group @contact-groups)))
(let [groups-with-new-group' (update groups-with-new-group new-group-id assoc :name "new group name")]
(testing ":set-contact-group-name event" (testing ":set-contact-group-name event"
@ -412,11 +408,7 @@
(rf/dispatch [::prepare-group-name]) (rf/dispatch [::prepare-group-name])
(rf/dispatch [:set-contact-group-name]) (rf/dispatch [:set-contact-group-name])
(is (= groups-with-new-group' @contact-groups))) (is (= groups-with-new-group1 @contact-groups)))
(let [groups-with-new-group'' (-> groups-with-new-group'
(update new-group-id assoc :order 1)
(update "dapps" assoc :order 2))]
(testing ":save-contact-group-order event" (testing ":save-contact-group-order event"
@ -432,7 +424,7 @@
(rf/dispatch [:change-contact-group-order 1 0]) (rf/dispatch [:change-contact-group-order 1 0])
(rf/dispatch [:save-contact-group-order]) (rf/dispatch [:save-contact-group-order])
(is (= groups-with-new-group'' @contact-groups))) (is (= groups-with-new-group2 @contact-groups)))
(testing ":add-selected-contacts-to-group event" (testing ":add-selected-contacts-to-group event"
@ -440,7 +432,7 @@
(rf/dispatch [:add-selected-contacts-to-group]) (rf/dispatch [:add-selected-contacts-to-group])
(rf/dispatch [:deselect-contact "browse"]) (rf/dispatch [:deselect-contact "browse"])
(is (= (update groups-with-new-group'' new-group-id assoc :contacts [{:identity new-contact-public-key} (is (= (update groups-with-new-group2 new-group-id assoc :contacts [{:identity new-contact-public-key}
{:identity "demo-bot"} {:identity "demo-bot"}
{:identity "browse"}]) {:identity "browse"}])
@contact-groups))) @contact-groups)))
@ -449,14 +441,14 @@
(rf/dispatch [:remove-contact-from-group "browse" new-group-id]) (rf/dispatch [:remove-contact-from-group "browse" new-group-id])
(is (= groups-with-new-group'' @contact-groups))) (is (= groups-with-new-group2 @contact-groups)))
(testing ":add-contacts-to-group event" (testing ":add-contacts-to-group event"
(rf/dispatch [:add-contacts-to-group new-group-id ["browse"]]) (rf/dispatch [:add-contacts-to-group new-group-id ["browse"]])
(is (= (update groups-with-new-group'' new-group-id assoc :contacts [{:identity new-contact-public-key} (is (= (update groups-with-new-group2 new-group-id assoc :contacts [{:identity new-contact-public-key}
{:identity "demo-bot"} {:identity "demo-bot"}
{:identity "browse"}]) {:identity "browse"}])
@contact-groups)) @contact-groups))
@ -467,5 +459,5 @@
(rf/dispatch [:delete-contact-group]) (rf/dispatch [:delete-contact-group])
(is (= (update groups-with-new-group'' new-group-id assoc :pending? true) (is (= (update groups-with-new-group2 new-group-id assoc :pending? true)
@contact-groups)))))))))))))))) @contact-groups)))))