status-react/src/legacy/status_im/utils/transducers_test.cljs

64 lines
1.9 KiB
Plaintext
Raw Normal View History

2023-12-19 17:41:30 +00:00
(ns legacy.status-im.utils.transducers-test
(:require
[cljs.test :refer-macros [deftest is testing]]
2023-12-19 17:41:30 +00:00
[legacy.status-im.utils.transducers :as transducers]))
2017-10-24 10:51:32 +00:00
(def preview-call-1
{:jail-id 1
:path [:preview]
:params {:chat-id 1}
2017-10-24 10:51:32 +00:00
:callback (fn []
[[:msg-id 1]])})
(def preview-call-2
{:jail-id 1
:path [:preview]
:params {:chat-id 1}
2017-10-24 10:51:32 +00:00
:callback (fn []
[[:msg-id 2]])})
(def jail-calls
2017-10-24 10:51:32 +00:00
'({:jail-id 1
:path [:suggestions]
:params {:arg 0}}
2017-10-24 10:51:32 +00:00
{:jail-id 1
:path [:function]
:params {:sub :a}}
2017-10-24 10:51:32 +00:00
{:jail-id 1
:path [:function]
:params {:sub :b}}
2017-10-24 10:51:32 +00:00
{:jail-id 1
:path [:suggestions]
:params {:arg 1}}
2017-10-24 10:51:32 +00:00
{:jail-id 1
:path [:suggestions]
:params {:arg 2}}
2017-10-24 10:51:32 +00:00
preview-call-1
preview-call-2))
(deftest last-distinct-by-test
(testing
"Elements are removed from input according to provided `compare-fn`,
2017-10-24 10:51:32 +00:00
when duplicate elements are removed, the last one stays"
(is (= (sequence (transducers/last-distinct-by (fn [{:keys [jail-id path] :as call}]
(if (= :suggestions (last path))
[jail-id path]
call)))
jail-calls)
2017-10-24 10:51:32 +00:00
'({:jail-id 1
:path [:suggestions]
:params {:arg 2}}
2017-10-24 10:51:32 +00:00
{:jail-id 1
:path [:function]
:params {:sub :a}}
2017-10-24 10:51:32 +00:00
{:jail-id 1
:path [:function]
:params {:sub :b}}
2017-10-24 10:51:32 +00:00
preview-call-1
preview-call-2))))
(testing "Edge cases with input size `N=0` and `N=1` work as well"
(is (= (sequence (transducers/last-distinct-by identity) '())
'()))
(is (= (sequence (transducers/last-distinct-by identity) '(1))
'(1)))))