2020-04-30 14:44:17 +00:00
|
|
|
(ns status-im.chat.models.message-test
|
2018-04-19 08:48:04 +00:00
|
|
|
(:require [cljs.test :refer-macros [deftest is testing]]
|
2020-05-05 14:18:23 +00:00
|
|
|
[status-im.chat.models.message :as message]
|
2021-02-16 14:16:32 +00:00
|
|
|
[status-im.chat.models.loading :as loading]
|
|
|
|
[status-im.ui.screens.chat.state :as view.state]))
|
2018-04-19 08:48:04 +00:00
|
|
|
|
2020-04-03 11:32:42 +00:00
|
|
|
(deftest add-received-message-test
|
2021-02-16 14:16:32 +00:00
|
|
|
(with-redefs [message/add-message #(identity %1)]
|
2020-04-03 11:32:42 +00:00
|
|
|
(let [chat-id "chat-id"
|
|
|
|
clock-value 10
|
|
|
|
cursor-clock-value (dec clock-value)
|
2021-02-16 14:16:32 +00:00
|
|
|
cursor (loading/clock-value->cursor cursor-clock-value)
|
2020-04-03 11:32:42 +00:00
|
|
|
cofx {:now 0
|
2021-02-16 14:16:32 +00:00
|
|
|
:db {:current-chat-id chat-id
|
|
|
|
:pagination-info {chat-id {:messages-initialized? true
|
|
|
|
:cursor cursor
|
|
|
|
:cursor-clock-value cursor-clock-value}}}}
|
|
|
|
message #js {:localChatId chat-id
|
|
|
|
:clock clock-value
|
|
|
|
:alias "alias"
|
|
|
|
:name "name"
|
|
|
|
:identicon "identicon"
|
|
|
|
:from "from"}]
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- cursor
|
|
|
|
;; <- message
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "there's no hidden item"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom nil)]
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (= {:db {:current-chat-id "chat-id",
|
|
|
|
:pagination-info
|
2020-08-31 09:52:17 +00:00
|
|
|
{"chat-id"
|
2021-02-16 14:16:32 +00:00
|
|
|
{:messages-initialized? true
|
|
|
|
:cursor
|
2020-08-31 09:52:17 +00:00
|
|
|
"00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000",
|
2021-02-16 14:16:32 +00:00
|
|
|
:cursor-clock-value 9}}}}
|
|
|
|
(dissoc (message/receive-many
|
|
|
|
cofx
|
|
|
|
#js {:messages (to-array [message])})
|
|
|
|
:utils/dispatch-later)))))
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- cursor
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- message
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the hidden item has a clock value less than the current"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (dec clock-value)})]
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (= {:db {:current-chat-id "chat-id",
|
|
|
|
:pagination-info
|
2020-08-31 09:52:17 +00:00
|
|
|
{"chat-id"
|
2021-02-16 14:16:32 +00:00
|
|
|
{:messages-initialized? true
|
|
|
|
:cursor
|
2020-08-31 09:52:17 +00:00
|
|
|
"00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000",
|
2021-02-16 14:16:32 +00:00
|
|
|
:cursor-clock-value 9}}}}
|
|
|
|
(dissoc (message/receive-many
|
|
|
|
cofx
|
|
|
|
#js {:messages (to-array [message])})
|
|
|
|
:utils/dispatch-later)))))
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- cursor
|
|
|
|
;; <- message
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the message falls between the first-hidden-item and cursor"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (inc clock-value)})]
|
2021-02-16 14:16:32 +00:00
|
|
|
(let [result (dissoc (message/receive-many
|
|
|
|
cofx
|
|
|
|
#js {:messages (to-array [message])})
|
|
|
|
:utils/dispatch-later)]
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "it sets all-loaded? to false"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (not (get-in result [:db :pagination-info chat-id :all-loaded?]))))
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "it updates cursor-clock-value & cursor"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (= clock-value (get-in result [:db :pagination-info chat-id :cursor-clock-value])))
|
|
|
|
(is (= (loading/clock-value->cursor clock-value) (get-in result [:db :pagination-info chat-id :cursor])))))))
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- message
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the message falls between the first-hidden-item and cursor is nil"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (inc clock-value)})]
|
2021-02-16 14:16:32 +00:00
|
|
|
(let [result (dissoc (message/receive-many
|
|
|
|
(update-in cofx [:db :pagination-info chat-id] dissoc :cursor :cursor-clock-value)
|
|
|
|
#js {:messages (to-array [message])})
|
|
|
|
:utils/dispatch-later)]
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "it sets all-loaded? to false"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (not (get-in result [:db :pagination-info chat-id :all-loaded?]))))
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "it updates cursor-clock-value & cursor"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (= clock-value (get-in result [:db :pagination-info chat-id :cursor-clock-value])))
|
|
|
|
(is (= (loading/clock-value->cursor clock-value) (get-in result [:db :pagination-info chat-id :cursor])))))))
|
2020-04-03 11:32:42 +00:00
|
|
|
;; <- message
|
|
|
|
;; <- cursor
|
|
|
|
;; <- first-hidden-item
|
|
|
|
;; <- top of the chat
|
|
|
|
(testing "the message falls before both the first-hidden-item and cursor"
|
|
|
|
(with-redefs [view.state/first-not-visible-item (atom {:clock-value (inc clock-value)})]
|
2021-02-16 14:16:32 +00:00
|
|
|
(let [message #js {:localChatId chat-id
|
|
|
|
:clock (- clock-value 2)
|
|
|
|
:alias "alias"
|
|
|
|
:name "name"
|
|
|
|
:identicon "identicon"
|
|
|
|
:from "from"}
|
|
|
|
result (dissoc (message/receive-many
|
|
|
|
cofx
|
|
|
|
#js {:messages (to-array [message])})
|
|
|
|
:utils/dispatch-later)]
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "it sets all-loaded? to false"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (not (get-in result [:db :pagination-info chat-id :all-loaded?]))))
|
2020-04-03 11:32:42 +00:00
|
|
|
(testing "it does not update cursor-clock-value & cursor"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (= cursor-clock-value (get-in result [:db :pagination-info chat-id :cursor-clock-value])))
|
|
|
|
(is (= cursor (get-in result [:db :pagination-info chat-id :cursor]))))))))))
|
2020-04-03 11:32:42 +00:00
|
|
|
|
2020-04-08 16:01:11 +00:00
|
|
|
(deftest message-loaded?
|
|
|
|
(testing "it returns false when it's not in loaded message"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (not (#'status-im.chat.models.message/message-loaded? {:messages {"a" {}}} "a" "message-id"))))
|
2020-04-08 16:01:11 +00:00
|
|
|
(testing "it returns true when it's already in the loaded message"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (#'status-im.chat.models.message/message-loaded? {:messages {"a" {"message-id" {}}}} "a" "message-id"))))
|
|
|
|
|
2020-04-08 16:01:11 +00:00
|
|
|
(deftest earlier-than-deleted-at?
|
|
|
|
(testing "it returns true when the clock-value is the same as the deleted-clock-value in chat"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (#'status-im.chat.models.message/earlier-than-deleted-at?
|
|
|
|
{:chats {"a" {:deleted-at-clock-value 1}}}
|
|
|
|
"a"
|
|
|
|
1)))
|
2020-04-08 16:01:11 +00:00
|
|
|
(testing "it returns false when the clock-value is greater than the deleted-clock-value in chat"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (not (#'status-im.chat.models.message/earlier-than-deleted-at?
|
|
|
|
{:chats {"a" {:deleted-at-clock-value 1}}}
|
|
|
|
"a"
|
|
|
|
2))))
|
|
|
|
|
2020-04-08 16:01:11 +00:00
|
|
|
(testing "it returns true when the clock-value is less than the deleted-clock-value in chat"
|
2021-02-16 14:16:32 +00:00
|
|
|
(is (#'status-im.chat.models.message/earlier-than-deleted-at?
|
|
|
|
{:chats {"a" {:deleted-at-clock-value 1}}}
|
|
|
|
"a"
|
|
|
|
0))))
|