From 2717291f83214a50374191799f3ecf9152434783 Mon Sep 17 00:00:00 2001 From: Rende11 Date: Tue, 14 Nov 2023 00:25:04 +0300 Subject: [PATCH] Store as set --- .../sheets/network_preferences/view.cljs | 2 +- src/status_im2/subs/wallet/wallet.cljs | 5 +- src/status_im2/subs/wallet/wallet_test.cljs | 149 ++++++++++-------- 3 files changed, 88 insertions(+), 68 deletions(-) diff --git a/src/status_im2/contexts/wallet/common/sheets/network_preferences/view.cljs b/src/status_im2/contexts/wallet/common/sheets/network_preferences/view.cljs index 9e561c8f5670..9a4b9134d864 100644 --- a/src/status_im2/contexts/wallet/common/sheets/network_preferences/view.cljs +++ b/src/status_im2/contexts/wallet/common/sheets/network_preferences/view.cljs @@ -26,7 +26,7 @@ (defn- view-internal [] (let [{:keys [color address network-preference-names]} (rf/sub [:wallet/current-viewing-account]) - network-preference-names-state (reagent/atom (set network-preference-names)) + network-preference-names-state (reagent/atom network-preference-names) toggle-network (fn [network-name] (if (contains? @network-preference-names-state network-name) diff --git a/src/status_im2/subs/wallet/wallet.cljs b/src/status_im2/subs/wallet/wallet.cljs index e24bf417c163..a87d160b6221 100644 --- a/src/status_im2/subs/wallet/wallet.cljs +++ b/src/status_im2/subs/wallet/wallet.cljs @@ -45,7 +45,8 @@ prod-preferred-chain-ids) network-preference-names (->> network-details (filter-networks current-chain-ids) - (map :network-name))] + (map :network-name) + (set))] (assoc account :network-preference-names network-preference-names))) (re-frame/reg-sub @@ -91,4 +92,4 @@ :<- [:wallet/network-details] (fn [[current-viewing-account network-details]] (let [network-preference-names (:network-preference-names current-viewing-account)] - (filter #(contains? (set network-preference-names) (:network-name %)) network-details)))) + (filter #(contains? network-preference-names (:network-name %)) network-details)))) diff --git a/src/status_im2/subs/wallet/wallet_test.cljs b/src/status_im2/subs/wallet/wallet_test.cljs index 764026011de8..fc81e35795eb 100644 --- a/src/status_im2/subs/wallet/wallet_test.cljs +++ b/src/status_im2/subs/wallet/wallet_test.cljs @@ -119,10 +119,10 @@ (swap! rf-db/app-db #(-> % (assoc-in [:wallet :accounts] accounts) (assoc :wallet/tokens tokens))) - (is (= `({:address "0x1" - :balance 3250} - {:address "0x2" - :balance 2100}) + (is (= [{:address "0x1" + :balance 3250} + {:address "0x2" + :balance 2100}] (rf/sub [sub-name]))))) (h/deftest-sub :wallet/accounts @@ -131,48 +131,57 @@ (swap! rf-db/app-db #(-> % (assoc-in [:wallet :accounts] accounts) - (assoc :wallet/tokens tokens))) + (assoc :wallet/tokens tokens) + (assoc :wallet/networks network-data))) (is - (= `({:path "m/44'/60'/0'/0/0" - :emoji "😃" - :key-uid "0x2f5ea39" - :address "0x1" - :wallet false - :name "Account One" - :type :generated - :chat false - :test-preferred-chain-ids #{5 420 421613} - :color :blue - :hidden false - :prod-preferred-chain-ids #{1 10 42161} - :position 0 - :clock 1698945829328 - :created-at 1698928839000 - :operable "fully" - :mixedcase-address "0x7bcDfc75c431" - :public-key "0x04371e2d9d66b82f056bc128064" - :removed false} - {:path "m/44'/60'/0'/0/1" - :emoji "💎" - :key-uid "0x2f5ea39" - :address "0x2" - :wallet false - :name "Account Two" - :type :generated - :chat false - :test-preferred-chain-ids #{5 420 421613} - :color :purple - :hidden false - :prod-preferred-chain-ids #{1 10 42161} - :position 1 - :clock 1698945829328 - :created-at 1698928839000 - :operable "fully" - :mixedcase-address "0x7bcDfc75c431" - :public-key "0x04371e2d9d66b82f056bc128064" - :removed false}) + (= [{:path "m/44'/60'/0'/0/0" + :emoji "😃" + :key-uid "0x2f5ea39" + :address "0x1" + :wallet false + :name "Account One" + :type :generated + :chat false + :test-preferred-chain-ids #{5 420 421613} + :color :blue + :hidden false + :prod-preferred-chain-ids #{1 10 42161} + :network-preference-names #{:ethereum :arbitrum :optimism} + :position 0 + :clock 1698945829328 + :created-at 1698928839000 + :operable "fully" + :mixedcase-address "0x7bcDfc75c431" + :public-key "0x04371e2d9d66b82f056bc128064" + :removed false} + {:path "m/44'/60'/0'/0/1" + :emoji "💎" + :key-uid "0x2f5ea39" + :address "0x2" + :wallet false + :name "Account Two" + :type :generated + :chat false + :test-preferred-chain-ids #{5 420 421613} + :color :purple + :hidden false + :prod-preferred-chain-ids #{1 10 42161} + :network-preference-names #{:ethereum :arbitrum :optimism} + :position 1 + :clock 1698945829328 + :created-at 1698928839000 + :operable "fully" + :mixedcase-address "0x7bcDfc75c431" + :public-key "0x04371e2d9d66b82f056bc128064" + :removed false}] (rf/sub [sub-name]))))) +(h/deftest-sub :wallet/current-viewing-account-address + [sub-name] + (testing "returns current viewing account address" + (swap! rf-db/app-db #(assoc-in % [:wallet :current-viewing-account-address] "0x1")) + (is (= "0x1" (rf/sub [sub-name]))))) + (h/deftest-sub :wallet/current-viewing-account [sub-name] (testing "returns current account with balance base" @@ -195,6 +204,7 @@ :color :blue :hidden false :prod-preferred-chain-ids #{1 10 42161} + :network-preference-names #{:ethereum :arbitrum :optimism} :position 0 :clock 1698945829328 :created-at 1698928839000 @@ -202,25 +212,34 @@ :mixedcase-address "0x7bcDfc75c431" :public-key "0x04371e2d9d66b82f056bc128064" :removed false - :balance 3250 - :network-preferences #{:ethereum :arbitrum :optimism} - :networks #{{:short-name "opt" - :network-name :optimism - :chain-id 10 - :related-chain-id nil - :layer 2} - {:short-name "eth" - :network-name :ethereum - :chain-id 1 - :related-chain-id nil - :layer 1} - {:short-name "arb1" - :network-name :arbitrum - :chain-id 42161 - :related-chain-id nil - :layer 2}}} - (update (rf/sub [sub-name]) - :networks - (fn [nx] - ;; Exclude `:source` for the correct equality check - (set (map #(dissoc % :source) nx)))))))) + :balance 3250} + (rf/sub [sub-name]))))) + +(h/deftest-sub :wallet/network-preference-details + [sub-name] + (testing "returns current viewing account address" + (swap! rf-db/app-db + #(-> % + (assoc-in [:wallet :accounts] accounts) + (assoc-in [:wallet :current-viewing-account-address] "0x1") + (assoc :wallet/tokens tokens) + (assoc :wallet/networks network-data))) + (is + (= [{:short-name "eth" + :network-name :ethereum + :chain-id 1 + :related-chain-id nil + :layer 1} + {:short-name "arb1" + :network-name :arbitrum + :chain-id 42161 + :related-chain-id nil + :layer 2} + {:short-name "opt" + :network-name :optimism + :chain-id 10 + :related-chain-id nil + :layer 2}] + (->> (rf/sub [sub-name]) + ;; Removed `#js source` property for correct compare + (map #(dissoc % :source)))))))