Skip to content

Commit

Permalink
unit tests for utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
briansztamfater committed Apr 25, 2024
1 parent 131b574 commit 36af720
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 96 deletions.
19 changes: 0 additions & 19 deletions src/status_im/contexts/wallet/common/utils/send.cljs

This file was deleted.

75 changes: 0 additions & 75 deletions src/status_im/contexts/wallet/common/utils/send_test.cljs

This file was deleted.

4 changes: 2 additions & 2 deletions src/status_im/contexts/wallet/send/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@
(conj {:type :add}))))

(defn loading-network-amounts
[affordable-networks disabled-chain-ids receiver-networks to?]
[valid-networks disabled-chain-ids receiver-networks to?]
(let [disabled-set (set disabled-chain-ids)
receiver-networks-set (set receiver-networks)
receiver-networks-count (count receiver-networks)
valid-networks (concat affordable-networks disabled-chain-ids)]
valid-networks (concat valid-networks disabled-chain-ids)]
(cond-> (->> valid-networks
(map (fn [k]
(cond-> {:chain-id k
Expand Down
163 changes: 163 additions & 0 deletions src/status_im/contexts/wallet/send/utils_test.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns status-im.contexts.wallet.send.utils-test
(:require [cljs.test :refer [deftest is testing]]
[status-im.contexts.wallet.send.utils :as utils]
[utils.map :as map]
[utils.money :as money]))

(deftest test-amount-in-hex
Expand Down Expand Up @@ -203,3 +204,165 @@
(is (= expected
(utils/token-valid-networks {:balances-per-chain balances-per-chain
:disabled-chain-ids disabled-chain-ids}))))))

(deftest test-clean-network-amounts
(testing "Correctly resets loading network amounts to zero and changes type to default"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :loading}
{:chain-id "2" :total-amount (money/bignumber "200") :type :default}]
expected [{:chain-id "1" :total-amount (money/bignumber "0") :type :default}
{:chain-id "2" :total-amount (money/bignumber "200") :type :default}]
result (utils/clean-network-amounts network-amounts)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Leaves non-loading types unchanged"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :default}
{:chain-id "2" :total-amount (money/bignumber "0") :type :disabled}]
expected [{:chain-id "1" :total-amount (money/bignumber "100") :type :default}
{:chain-id "2" :total-amount (money/bignumber "0") :type :disabled}]
result (utils/clean-network-amounts network-amounts)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Processes an empty list without error"
(let [network-amounts []
expected []
result (utils/clean-network-amounts network-amounts)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Applies transformations to multiple loading entries"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :loading}
{:chain-id "2" :total-amount (money/bignumber "200") :type :loading}]
expected [{:chain-id "1" :total-amount (money/bignumber "0") :type :default}
{:chain-id "2" :total-amount (money/bignumber "0") :type :default}]
result (utils/clean-network-amounts network-amounts)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Mix of loading and non-loading types"
(let [network-amounts [{:chain-id "1" :total-amount (money/bignumber "100") :type :loading}
{:chain-id "2" :total-amount (money/bignumber "200") :type :default}
{:chain-id "3" :total-amount (money/bignumber "300") :type :loading}
{:chain-id "4" :total-amount (money/bignumber "0") :type :disabled}]
expected [{:chain-id "1" :total-amount (money/bignumber "0") :type :default}
{:chain-id "2" :total-amount (money/bignumber "200") :type :default}
{:chain-id "3" :total-amount (money/bignumber "0") :type :default}
{:chain-id "4" :total-amount (money/bignumber "0") :type :disabled}]
result (utils/clean-network-amounts network-amounts)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons)))))

(deftest test-loading-network-amounts
(testing "Assigns :loading type to affordable networks except for disabled ones"
(let [valid-networks ["1" "2" "3"]
disabled-chain-ids ["3"]
receiver-networks ["1" "2"]
to? true
expected [{:chain-id "1" :type :loading}
{:chain-id "2" :type :loading}
{:type :add}]
result (utils/loading-network-amounts valid-networks
disabled-chain-ids
receiver-networks
to?)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Assigns :disabled type with zero total-amount to disabled networks when to? is false"
(let [valid-networks ["1" "2" "3"]
disabled-chain-ids ["2" "3"]
receiver-networks ["1"]
to? false
expected [{:chain-id "1" :type :loading}
{:chain-id "2" :type :disabled :total-amount (money/bignumber "0")}
{:chain-id "3" :type :disabled :total-amount (money/bignumber "0")}]
result (utils/loading-network-amounts valid-networks
disabled-chain-ids
receiver-networks
to?)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Filters out networks not in receiver networks when to? is true"
(let [valid-networks ["1" "2" "3" "4"]
disabled-chain-ids ["2"]
receiver-networks ["1" "3"]
to? true
expected [{:chain-id "1" :type :loading}
{:chain-id "3" :type :loading}]
result (utils/loading-network-amounts valid-networks
disabled-chain-ids
receiver-networks
to?)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons))))

(testing "Appends :add type if receiver network count is less than available networks and to? is true"
(let [valid-networks ["1" "2" "3"]
disabled-chain-ids ["2"]
receiver-networks ["1"]
to? true
expected [{:chain-id "1" :type :loading}
{:type :add}]
result (utils/loading-network-amounts valid-networks
disabled-chain-ids
receiver-networks
to?)
comparisons (map #(map/deep-compare-maps %1 %2)
expected
result)]
(is (every? identity comparisons)))))

(deftest test-network-links
(testing "Calculates position differences correctly"
(let [route [{:from {:chain-id "1"} :to {:chain-id "3"}}
{:from {:chain-id "2"} :to {:chain-id "1"}}
{:from {:chain-id "3"} :to {:chain-id "2"}}]
from-values-by-chain [{:chain-id "1"} {:chain-id "2"} {:chain-id "3"}]
to-values-by-chain [{:chain-id "3"} {:chain-id "1"} {:chain-id "2"}]
expected [{:from-chain-id "1" :to-chain-id "3" :position-diff 0}
{:from-chain-id "2" :to-chain-id "1" :position-diff 0}
{:from-chain-id "3" :to-chain-id "2" :position-diff 0}]
result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result))))

(testing "Handles cases with no position difference"
(let [route [{:from {:chain-id "1"} :to {:chain-id "1"}}]
from-values-by-chain [{:chain-id "1"} {:chain-id "2"} {:chain-id "3"}]
to-values-by-chain [{:chain-id "1"} {:chain-id "2"} {:chain-id "3"}]
expected [{:from-chain-id "1" :to-chain-id "1" :position-diff 0}]
result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result))))

(testing "Handles empty route"
(let [route []
from-values-by-chain []
to-values-by-chain []
expected []
result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result))))

(testing "Verifies negative position differences"
(let [route [{:from {:chain-id "3"} :to {:chain-id "1"}}]
from-values-by-chain [{:chain-id "1"} {:chain-id "2"} {:chain-id "3"}]
to-values-by-chain [{:chain-id "1"} {:chain-id "2"} {:chain-id "3"}]
expected [{:from-chain-id "1" :to-chain-id "3" :position-diff -2}]
result (utils/network-links route from-values-by-chain to-values-by-chain)]
(is (= expected result)))))
17 changes: 17 additions & 0 deletions src/utils/map.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns utils.map
(:require [utils.money :as money]))

(defn deep-compare-values
"Compares two values, using special handling for BigNumbers and regular equality for others."
[v1 v2]
(cond
(and (money/bignumber? v1) (money/bignumber? v2)) (money/equal-to v1 v2)
:else (= v1 v2)))

(defn deep-compare-maps
"Recursively compare two maps, specially handling BigNumber values within the maps."
[map1 map2]
(and
(= (set (keys map1)) (set (keys map2)))
(every? (fn [k] (deep-compare-values (get map1 k) (get map2 k)))
(keys map1))))
5 changes: 5 additions & 0 deletions src/utils/money.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
(new BigNumber (normalize (str n)))
(catch :default _ nil))))

(defn bignumber?
"Check if the value is a bignumber."
[x]
(instance? BigNumber x))

(defn greater-than-or-equals
[^js bn1 ^js bn2]
(.greaterThanOrEqualTo bn1 bn2))
Expand Down

0 comments on commit 36af720

Please sign in to comment.