Make json-rpc/call effect more in line with re-frame standards (#15936)

Changes effect :json/rpc-call to accept on-success and on-error parameters
either as functions OR as re-frame event vectors. The changes are 100% backwards
compatible.

## Why?

Re-frame is very explicit in its documentation and its architecture, saying that
event handlers should be pure. Calling re-frame.core/dispatch in event handlers
makes them sort of stateful.

> So, you can "get away with it". But it ain't pure.
>
> -- https://day8.github.io/re-frame/EffectfulHandlers/#90-solution

In status-mobile, arguably one of our most important effects (not to be confused
with event handlers) is :json-rpc/call, but at the moment, the on-success and
on-error values are expected to be stateful functions (e.g. usually used for
logging and dispatching subsequent events).

This creates two important problems:

1. The value returned by event handlers is more opaque and cannot be easily
   inspected (for example using tap>, log/debug or just println). If we try to
   inspect or log them, on-success and on-error will be printed as
   #object[Function].
2. Testing event handlers using :json-rpc/call becomes an exercise of
   frustration, because we can't simply compare the results of the event handler
   with a good expected value, which is one of the big selling points of testing
   pure functions.

### The testability of event handlers

> For any re-frame app, there's three things to test:
>
> - Event Handlers - most of your testing focus will be here because this is
>   where most of the logic lives
> - Subscription Handlers - often not a lot to test here. Only Layer 3
>   subscriptions need testing.
> - View functions - I don't tend to write tests for views.
>
> -- https://day8.github.io/re-frame/Testing/#what-to-test

So re-frame is saying event handlers should be pure, and that event handlers
should be tested.

In order to achieve the divine simplicity of testing event handlers as pure
functions, we need to make :json-rpc/call also accept on-success and
on-error as event vectors.

Good news is that there is a known pattern to solve this problem, e.g. used by
the library https://github.com/Day8/re-frame-http-fx.

The pattern is simple once we see it: behind the scenes, :json-rpc/call conj'es
the results of the RPC call into the event vectors on-success and on-error, and
:json-rpc/call dispatches the events itself. This eliminates the need for the
stateful dispatch call in event handlers.
This commit is contained in:
Icaro Motta 2023-05-18 15:56:10 -03:00 committed by GitHub
parent c7bc16608a
commit a17efa7299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 456 additions and 520 deletions

View File

@ -365,6 +365,43 @@ dispatch.
(str "Hello " username)]]) (str "Hello " username)]])
``` ```
### Registering effects
When registering re-frame effects (`reg-fx`), prefer to expose a data-only
interface because that will allow event handlers to stay pure.
For instance, if an effect needs a `on-success` callback, allow it to receive a
*re-frame event vector*. This approach is used by us in the [json-rpc/call
effect](src/status_im2/common/json_rpc/events.cljs), but also by third-party
effects, such as https://github.com/Day8/re-frame-http-fx. For the complete
rationale, see [PR #15936](https://github.com/status-im/status-mobile/pull/15936).
### Using the effect `:json-rpc/call`
Prefer the pure version of `:json-rpc/call` (no callbacks).
```clojure
;; not as good
(rf/defn accept-contact-request
{:events [:activity-center.contact-requests/accept]}
[_ contact-id]
{:json-rpc/call
[{:method "wakuext_acceptContactRequest"
:params [{:id contact-id}]
:on-success #(rf/dispatch [:sanitize-messages-and-process-response %])
:on-error #(rf/dispatch [:activity-center.contact-requests/accept-error contact-id %])}]})
;; better
(rf/defn accept-contact-request
{:events [:activity-center.contact-requests/accept]}
[_ contact-id]
{:json-rpc/call
[{:method "wakuext_acceptContactRequest"
:params [{:id contact-id}]
:on-success [:sanitize-messages-and-process-response]
:on-error [:activity-center.contact-requests/accept-error contact-id]}]})
```
### Registering event handlers ### Registering event handlers
Events must always be declared with the `utils.fx/defn` macro. Also, don't use Events must always be declared with the `utils.fx/defn` macro. Also, don't use

View File

@ -1,12 +1,13 @@
(ns status-im2.common.json-rpc.events (ns status-im2.common.json-rpc.events
(:require [clojure.string :as string] (:require [clojure.string :as string]
[native-module.core :as native-module]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[react-native.background-timer :as background-timer] [react-native.background-timer :as background-timer]
[native-module.core :as native-module]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[utils.re-frame :as rf]
[utils.transforms :as transforms])) [utils.transforms :as transforms]))
(defn on-error-retry (defn- on-error-retry
[call-method {:keys [method number-of-retries delay on-error] :as arg}] [call-method {:keys [method number-of-retries delay on-error] :as arg}]
(if (pos? number-of-retries) (if (pos? number-of-retries)
(fn [error] (fn [error]
@ -24,6 +25,31 @@
on-error)) on-error))
(defn call (defn call
"Call private RPC endpoint.
method: string - The name of an endpoint function in status-go, with the first
character lowercased and prefixed by wakuext_. For example, the BackupData
function should be represented as the string wakuext_backupData.
params: sequence - A positional sequence of zero or more arguments.
on-success/on-error: function/vector (optional) - When a function, it will be
called with the transformed response as the sole argument. When a vector, it
is expected to be a valid re-frame event vector, and the event will be
dispatched with the transformed response conj'ed at the end.
js-response: boolean - When non-nil, the successful response will not be
recursively converted to Clojure data structures. Default: nil.
number-of-retries: integer - The maximum number of retries in case of failure.
Default: nil.
delay: integer - The number of milliseconds to wait between retries. Default:
nil.
Note that on-error is optional, but if not provided, a default implementation
will be used.
"
[{:keys [method params on-success on-error js-response] :as arg}] [{:keys [method params on-success on-error js-response] :as arg}]
(let [params (or params []) (let [params (or params [])
on-error (or on-error on-error (or on-error
@ -34,15 +60,25 @@
:id 1 :id 1
:method method :method method
:params params}) :params params})
(fn [response] (fn [raw-response]
(if (string/blank? response) (if (string/blank? raw-response)
(on-error {:message "Blank response"}) (let [error {:message "Blank response"}]
(let [response-js (transforms/json->js response)] (if (vector? on-error)
(if (.-error response-js) (rf/dispatch (conj on-error error))
(on-error (transforms/js->clj (.-error response-js))) (on-error error)))
(on-success (if js-response (let [^js response-js (transforms/json->js raw-response)]
(.-result response-js) (if-let [error (.-error response-js)]
(transforms/js->clj (.-result response-js))))))))))) (let [error (transforms/js->clj error)]
(if (vector? on-error)
(rf/dispatch (conj on-error error))
(on-error error)))
(when on-success
(let [result (if js-response
(.-result response-js)
(transforms/js->clj (.-result response-js)))]
(if (vector? on-success)
(rf/dispatch (conj on-success result))
(on-success result)))))))))))
(re-frame/reg-fx (re-frame/reg-fx
:json-rpc/call :json-rpc/call

View File

@ -112,12 +112,9 @@
(when-let [notification (get-notification db notification-id)] (when-let [notification (get-notification db notification-id)]
{:json-rpc/call [{:method "wakuext_markActivityCenterNotificationsRead" {:json-rpc/call [{:method "wakuext_markActivityCenterNotificationsRead"
:params [[notification-id]] :params [[notification-id]]
:on-success #(rf/dispatch [:activity-center.notifications/mark-as-read-success :on-success [:activity-center.notifications/mark-as-read-success notification]
notification]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :notification/mark-as-read]}]}))
notification-id
:notification/mark-as-read
%])}]}))
(rf/defn mark-as-read-success (rf/defn mark-as-read-success
{:events [:activity-center.notifications/mark-as-read-success]} {:events [:activity-center.notifications/mark-as-read-success]}
@ -130,12 +127,9 @@
(when-let [notification (get-notification db notification-id)] (when-let [notification (get-notification db notification-id)]
{:json-rpc/call [{:method "wakuext_markActivityCenterNotificationsUnread" {:json-rpc/call [{:method "wakuext_markActivityCenterNotificationsUnread"
:params [[notification-id]] :params [[notification-id]]
:on-success #(rf/dispatch [:activity-center.notifications/mark-as-unread-success :on-success [:activity-center.notifications/mark-as-unread-success notification]
notification]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :notification/mark-as-unread]}]}))
notification-id
:notification/mark-as-unread
%])}]}))
(rf/defn mark-as-unread-success (rf/defn mark-as-unread-success
{:events [:activity-center.notifications/mark-as-unread-success]} {:events [:activity-center.notifications/mark-as-unread-success]}
@ -149,12 +143,9 @@
(when (>= now undoable-till) (when (>= now undoable-till)
{:json-rpc/call [{:method "wakuext_markAllActivityCenterNotificationsRead" {:json-rpc/call [{:method "wakuext_markAllActivityCenterNotificationsRead"
:params [] :params []
:on-success #(rf/dispatch :on-success [:activity-center.notifications/mark-all-as-read-success]
[:activity-center.notifications/mark-all-as-read-success]) :on-error [:activity-center/process-notification-failure nil
:on-error #(rf/dispatch [:activity-center/process-notification-failure :notification/mark-all-as-read]}]})))
nil
:notification/mark-all-as-read
%])}]})))
(rf/defn mark-all-as-read-success (rf/defn mark-all-as-read-success
{:events [:activity-center.notifications/mark-all-as-read-success]} {:events [:activity-center.notifications/mark-all-as-read-success]}
@ -210,17 +201,14 @@
[{:keys [db]} notification-id] [{:keys [db]} notification-id]
{:json-rpc/call [{:method "wakuext_acceptActivityCenterNotifications" {:json-rpc/call [{:method "wakuext_acceptActivityCenterNotifications"
:params [[notification-id]] :params [[notification-id]]
:on-success #(rf/dispatch [:activity-center.notifications/accept-success :on-success [:activity-center.notifications/accept-success notification-id]
notification-id %]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :notification/accept]}]})
notification-id
:notification/accept
%])}]})
(rf/defn accept-notification-success (rf/defn accept-notification-success
{:events [:activity-center.notifications/accept-success]} {:events [:activity-center.notifications/accept-success]}
[{:keys [db] :as cofx} notification-id {:keys [chats]}] [{:keys [db] :as cofx} notification-id {:keys [chats]}]
(let [notification (get-notification db notification-id)] (when-let [notification (get-notification db notification-id)]
(rf/merge cofx (rf/merge cofx
(chat.events/ensure-chats (map data-store.chats/<-rpc chats)) (chat.events/ensure-chats (map data-store.chats/<-rpc chats))
(notifications-reconcile [(assoc notification :read true :accepted true)])))) (notifications-reconcile [(assoc notification :read true :accepted true)]))))
@ -230,17 +218,14 @@
[{:keys [db]} notification-id] [{:keys [db]} notification-id]
{:json-rpc/call [{:method "wakuext_dismissActivityCenterNotifications" {:json-rpc/call [{:method "wakuext_dismissActivityCenterNotifications"
:params [[notification-id]] :params [[notification-id]]
:on-success #(rf/dispatch [:activity-center.notifications/dismiss-success :on-success [:activity-center.notifications/dismiss-success notification-id]
notification-id]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :notification/dismiss]}]})
notification-id
:notification/dismiss
%])}]})
(rf/defn dismiss-notification-success (rf/defn dismiss-notification-success
{:events [:activity-center.notifications/dismiss-success]} {:events [:activity-center.notifications/dismiss-success]}
[{:keys [db] :as cofx} notification-id] [{:keys [db] :as cofx} notification-id]
(let [notification (get-notification db notification-id)] (when-let [notification (get-notification db notification-id)]
(notifications-reconcile cofx [(assoc notification :read true :dismissed true)]))) (notifications-reconcile cofx [(assoc notification :read true :dismissed true)])))
(rf/defn delete-notification (rf/defn delete-notification
@ -248,12 +233,9 @@
[{:keys [db]} notification-id] [{:keys [db]} notification-id]
{:json-rpc/call [{:method "wakuext_deleteActivityCenterNotifications" {:json-rpc/call [{:method "wakuext_deleteActivityCenterNotifications"
:params [[notification-id]] :params [[notification-id]]
:on-success #(rf/dispatch [:activity-center.notifications/delete-success :on-success [:activity-center.notifications/delete-success notification-id]
notification-id]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :notification/delete]}]})
notification-id
:notification/delete
%])}]})
(rf/defn delete-notification-success (rf/defn delete-notification-success
{:events [:activity-center.notifications/delete-success]} {:events [:activity-center.notifications/delete-success]}
@ -268,48 +250,36 @@
[_ notification-id] [_ notification-id]
{:json-rpc/call [{:method "wakuext_declineContactVerificationRequest" {:json-rpc/call [{:method "wakuext_declineContactVerificationRequest"
:params [notification-id] :params [notification-id]
:on-success #(rf/dispatch [:activity-center/reconcile-notifications-from-response :on-success [:activity-center/reconcile-notifications-from-response]
%]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :contact-verification/decline]}]})
notification-id
:contact-verification/decline
%])}]})
(rf/defn contact-verification-reply (rf/defn contact-verification-reply
{:events [:activity-center.contact-verification/reply]} {:events [:activity-center.contact-verification/reply]}
[_ notification-id reply] [_ notification-id reply]
{:json-rpc/call [{:method "wakuext_acceptContactVerificationRequest" {:json-rpc/call [{:method "wakuext_acceptContactVerificationRequest"
:params [notification-id reply] :params [notification-id reply]
:on-success #(rf/dispatch [:activity-center/reconcile-notifications-from-response :on-success [:activity-center/reconcile-notifications-from-response]
%]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :contact-verification/reply]}]})
notification-id
:contact-verification/reply
%])}]})
(rf/defn contact-verification-mark-as-trusted (rf/defn contact-verification-mark-as-trusted
{:events [:activity-center.contact-verification/mark-as-trusted]} {:events [:activity-center.contact-verification/mark-as-trusted]}
[_ notification-id] [_ notification-id]
{:json-rpc/call [{:method "wakuext_verifiedTrusted" {:json-rpc/call [{:method "wakuext_verifiedTrusted"
:params [{:id notification-id}] :params [{:id notification-id}]
:on-success #(rf/dispatch [:activity-center/reconcile-notifications-from-response :on-success [:activity-center/reconcile-notifications-from-response]
%]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :contact-verification/mark-as-trusted]}]})
notification-id
:contact-verification/mark-as-trusted
%])}]})
(rf/defn contact-verification-mark-as-untrustworthy (rf/defn contact-verification-mark-as-untrustworthy
{:events [:activity-center.contact-verification/mark-as-untrustworthy]} {:events [:activity-center.contact-verification/mark-as-untrustworthy]}
[_ notification-id] [_ notification-id]
{:json-rpc/call [{:method "wakuext_verifiedUntrustworthy" {:json-rpc/call [{:method "wakuext_verifiedUntrustworthy"
:params [{:id notification-id}] :params [{:id notification-id}]
:on-success #(rf/dispatch [:activity-center/reconcile-notifications-from-response :on-success [:activity-center/reconcile-notifications-from-response]
%]) :on-error [:activity-center/process-notification-failure notification-id
:on-error #(rf/dispatch [:activity-center/process-notification-failure :contact-verification/mark-as-untrustworthy]}]})
notification-id
:contact-verification/mark-as-untrustworthy
%])}]})
;;;; Notifications fetching and pagination ;;;; Notifications fetching and pagination
@ -368,10 +338,9 @@
:limit per-page :limit per-page
:activityTypes (filter-type->rpc-param filter-type) :activityTypes (filter-type->rpc-param filter-type)
:readType (->rpc-read-type filter-status)}] :readType (->rpc-read-type filter-status)}]
:on-success #(rf/dispatch [:activity-center.notifications/fetch-success :on-success [:activity-center.notifications/fetch-success reset-data?]
reset-data? %]) :on-error [:activity-center.notifications/fetch-error filter-type
:on-error #(rf/dispatch [:activity-center.notifications/fetch-error filter-status]}]})))
filter-type filter-status %])}]})))
(rf/defn notifications-fetch-first-page (rf/defn notifications-fetch-first-page
{:events [:activity-center.notifications/fetch-first-page]} {:events [:activity-center.notifications/fetch-first-page]}
@ -434,9 +403,8 @@
:limit 20 :limit 20
:activityTypes [types/contact-request] :activityTypes [types/contact-request]
:readType (->rpc-read-type :unread)}] :readType (->rpc-read-type :unread)}]
:on-success #(rf/dispatch [:activity-center.notifications/fetch-pending-contact-requests-success %]) :on-success [:activity-center.notifications/fetch-pending-contact-requests-success]
:on-error #(rf/dispatch [:activity-center.notifications/fetch-error types/contact-request :unread :on-error [:activity-center.notifications/fetch-error types/contact-request :unread]}]})
%])}]})
(rf/defn notifications-fetch-pending-contact-requests-success (rf/defn notifications-fetch-pending-contact-requests-success
{:events [:activity-center.notifications/fetch-pending-contact-requests-success]} {:events [:activity-center.notifications/fetch-pending-contact-requests-success]}
@ -464,8 +432,8 @@
{:json-rpc/call {:json-rpc/call
[{:method "wakuext_hasUnseenActivityCenterNotifications" [{:method "wakuext_hasUnseenActivityCenterNotifications"
:params [] :params []
:on-success #(rf/dispatch [:activity-center/update-seen-state-success %]) :on-success [:activity-center/update-seen-state-success]
:on-error #(rf/dispatch [:activity-center/update-seen-state-error %])}]}) :on-error [:activity-center/update-seen-state-error]}]})
(rf/defn update-seen-state-success (rf/defn update-seen-state-success
{:events [:activity-center/update-seen-state-success]} {:events [:activity-center/update-seen-state-success]}
@ -485,8 +453,8 @@
{:json-rpc/call {:json-rpc/call
[{:method "wakuext_markAsSeenActivityCenterNotifications" [{:method "wakuext_markAsSeenActivityCenterNotifications"
:params [] :params []
:on-success #(rf/dispatch [:activity-center/mark-as-seen-success %]) :on-success [:activity-center/mark-as-seen-success]
:on-error #(rf/dispatch [:activity-center/mark-as-seen-error %])}]}) :on-error [:activity-center/mark-as-seen-error]}]})
(rf/defn mark-as-seen-success (rf/defn mark-as-seen-success
{:events [:activity-center/mark-as-seen-success]} {:events [:activity-center/mark-as-seen-success]}
@ -509,8 +477,8 @@
[{:method "wakuext_activityCenterNotificationsCount" [{:method "wakuext_activityCenterNotificationsCount"
:params [{:activityTypes types/all-supported :params [{:activityTypes types/all-supported
:readType (->rpc-read-type :unread)}] :readType (->rpc-read-type :unread)}]
:on-success #(rf/dispatch [:activity-center.notifications/fetch-unread-count-success %]) :on-success [:activity-center.notifications/fetch-unread-count-success]
:on-error #(rf/dispatch [:activity-center.notifications/fetch-unread-count-error %])}]}) :on-error [:activity-center.notifications/fetch-unread-count-error]}]})
(rf/defn notifications-fetch-unread-count-success (rf/defn notifications-fetch-unread-count-success
{:events [:activity-center.notifications/fetch-unread-count-success]} {:events [:activity-center.notifications/fetch-unread-count-success]}

View File

@ -1,163 +1,169 @@
(ns status-im2.contexts.activity-center.events-test (ns status-im2.contexts.activity-center.events-test
(:require [cljs.test :refer [deftest is testing]] (:require [cljs.test :refer [deftest is testing]]
[status-im2.constants :as constants] [status-im2.constants :as constants]
status-im.events [status-im2.contexts.activity-center.events :as events]
[test-helpers.unit :as h]
[status-im2.contexts.activity-center.notification-types :as types] [status-im2.contexts.activity-center.notification-types :as types]
[utils.re-frame :as rf])) [test-helpers.unit :as h]))
(h/use-log-fixture) (h/use-log-fixture)
(def notification-id "0x1") (def notification-id "0x1")
(defn setup
[]
(h/register-helper-events)
(rf/dispatch [:setup/app-started]))
(defn test-log-on-failure
[{:keys [before-test notification-id event action]}]
(h/run-test-sync
(setup)
(when before-test
(before-test))
(h/stub-fx-with-callbacks :json-rpc/call :on-error (constantly :fake-error))
(rf/dispatch event)
(is (= {:args [(str "Failed to " action)
{:notification-id notification-id
:error :fake-error}]
:level :warn}
(last @h/logs)))))
;;;; Misc ;;;; Misc
(deftest open-activity-center-test (deftest open-activity-center-test
(testing "opens the activity center with filters enabled"
(h/run-test-sync
(setup)
(rf/dispatch [:activity-center/open
{:filter-type types/contact-request
:filter-status :unread}])
(is (= {:status :unread
:type types/contact-request}
(get-in (h/db) [:activity-center :filter])))))
(testing "opens the activity center with default filters" (testing "opens the activity center with default filters"
(h/run-test-sync (is (= {:db {}
(setup) :dispatch [:open-modal :activity-center {}]
:dispatch-later [{:ms 1000 :dispatch [:activity-center/mark-as-seen]}]}
(events/open-activity-center {:db {}} nil))))
(rf/dispatch [:activity-center/open]) (testing "opens the activity center with filters enabled"
(is (= {:db {:activity-center {:filter {:status :unread :type types/contact-request}}}
:dispatch [:open-modal :activity-center {}]
:dispatch-later [{:ms 1000 :dispatch [:activity-center/mark-as-seen]}]}
(events/open-activity-center {:db {}}
{:filter-type types/contact-request
:filter-status :unread})))))
(is (= {:status :unread :type types/no-type} (deftest process-notification-failure-test
(get-in (h/db) [:activity-center :filter])))))) (testing "logs and returns nil"
(is (nil? (events/process-notification-failure
{:db {}}
notification-id
:some-action-name
:some-error)))
(is (= {:args ["Failed to :some-action-name"
{:notification-id notification-id
:error :some-error}]
:level :warn}
(last @h/logs)))))
;;;; Mark as read/unread
(deftest mark-as-read-test (deftest mark-as-read-test
(testing "does nothing if the notification ID cannot be found in the app db" (testing "does nothing if the notification ID cannot be found in the app db"
(h/run-test-sync (let [cofx {:db {:activity-center
(setup) {:notifications [{:id "0x1"
(let [spy-queue (atom [])] :read false
(h/spy-fx spy-queue :json-rpc/call) :type types/one-to-one-chat}]}}}]
(let [notifications [{:id notification-id (is (nil? (events/mark-as-read cofx "0x99")))))
:read false
:type types/one-to-one-chat}]]
(rf/dispatch [:test/assoc-in [:activity-center :notifications] notifications])
(rf/dispatch [:activity-center.notifications/mark-as-read "0x666"]) (testing "dispatches RPC call"
(let [notif {:id "0x1" :read false :type types/one-to-one-chat}
cofx {:db {:activity-center {:notifications [notif]}}}]
(is (= {:json-rpc/call
[{:method "wakuext_markActivityCenterNotificationsRead"
:params [[(:id notif)]]
:on-success [:activity-center.notifications/mark-as-read-success notif]
:on-error [:activity-center/process-notification-failure (:id notif)
:notification/mark-as-read]}]}
(events/mark-as-read cofx (:id notif)))))))
(is (= [] @spy-queue)) (deftest mark-as-read-success-test
(is (= notifications (get-in (h/db) [:activity-center :notifications]))))))) (let [f-args (atom [])
cofx {:db {}}
notif {:id "0x1" :read false :type types/one-to-one-chat}]
(with-redefs [events/notifications-reconcile
(fn [& args]
(reset! f-args args)
:result)]
(is (= :result (events/mark-as-read-success cofx notif)))
(is (= [cofx [(assoc notif :read true)]]
@f-args)))))
(testing "marks notifications as read and updates app db" (deftest mark-as-unread-test
(h/run-test-sync (testing "does nothing if the notification ID cannot be found in the app db"
(setup) (let [cofx {:db {:activity-center
(let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat} {:notifications [{:id "0x1"
notif-2 {:id "0x2" :read false :type types/one-to-one-chat} :read true
notif-3 {:id "0x3" :read false :type types/one-to-one-chat} :type types/one-to-one-chat}]}}}]
new-notif-3 (assoc notif-3 :read true) (is (nil? (events/mark-as-unread cofx "0x99")))))
new-notif-2 (assoc notif-2 :read true)]
(h/stub-fx-with-callbacks :json-rpc/call :on-success (constantly nil))
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:status :all :type types/no-type}
:notifications [notif-3 notif-2 notif-1]}])
(rf/dispatch [:activity-center.notifications/mark-as-read (:id notif-2)]) (testing "dispatches RPC call"
(is (= [notif-3 new-notif-2 notif-1] (let [notif {:id "0x1" :read true :type types/one-to-one-chat}
(get-in (h/db) [:activity-center :notifications]))) cofx {:db {:activity-center {:notifications [notif]}}}]
(is (= {:json-rpc/call
[{:method "wakuext_markActivityCenterNotificationsUnread"
:params [[(:id notif)]]
:on-success [:activity-center.notifications/mark-as-unread-success notif]
:on-error [:activity-center/process-notification-failure (:id notif)
:notification/mark-as-unread]}]}
(events/mark-as-unread cofx (:id notif)))))))
(rf/dispatch [:activity-center.notifications/mark-as-read (:id notif-3)]) (deftest mark-as-unread-success-test
(is (= [new-notif-3 new-notif-2 notif-1] (let [f-args (atom [])
(get-in (h/db) [:activity-center :notifications])))))) cofx {:db {}}
notif {:id "0x1" :read true :type types/one-to-one-chat}]
(testing "logs on failure" (with-redefs [events/notifications-reconcile
(test-log-on-failure (fn [& args]
{:notification-id notification-id (reset! f-args args)
:event [:activity-center.notifications/mark-as-read notification-id] :reconciliation-result)]
:action :notification/mark-as-read (is (= :reconciliation-result (events/mark-as-unread-success cofx notif)))
:before-test (fn [] (is (= [cofx [(assoc notif :read false)]]
(rf/dispatch @f-args)))))
[:test/assoc-in [:activity-center :notifications]
[{:id notification-id
:read false
:type types/one-to-one-chat}]]))})))
;;;; Acceptance/dismissal ;;;; Acceptance/dismissal
(deftest notification-acceptance-test (deftest accept-notification-test
(is (= {:json-rpc/call
[{:method "wakuext_acceptActivityCenterNotifications"
:params [[notification-id]]
:on-success [:activity-center.notifications/accept-success notification-id]
:on-error [:activity-center/process-notification-failure notification-id
:notification/accept]}]}
(events/accept-notification {:db {}} notification-id))))
(deftest accept-notification-success-test
(testing "does nothing if the notification ID cannot be found in the app db"
(let [cofx {:db {:activity-center
{:notifications [{:id "0x1"
:read false
:type types/one-to-one-chat}]}}}]
(is (nil? (events/accept-notification-success cofx "0x99" nil)))))
(testing "marks notification as accepted and read, then reconciles" (testing "marks notification as accepted and read, then reconciles"
(h/run-test-sync (let [notif-1 {:id "0x1" :type types/private-group-chat}
(setup) notif-2 {:id "0x2" :type types/private-group-chat}
(let [notif-1 {:id "0x1" :type types/private-group-chat} notif-2-accepted (assoc notif-2 :accepted true :read true)
notif-2 {:id "0x2" :type types/private-group-chat} cofx {:db {:activity-center {:filter {:type types/no-type :status :all}
notif-2-accepted (assoc notif-2 :accepted true :read true)] :notifications [notif-2 notif-1]}}}]
(h/stub-fx-with-callbacks :json-rpc/call :on-success (constantly notif-2)) (is (= {:db {:activity-center {:filter {:type 0 :status :all}
(rf/dispatch [:test/assoc-in [:activity-center] :notifications [notif-2-accepted notif-1]}
{:filter {:type types/no-type :status :all} :chats {}
:notifications [notif-2 notif-1]}]) :chats-home-list nil}
:dispatch-n [[:activity-center.notifications/fetch-unread-count]
[:activity-center.notifications/fetch-pending-contact-requests]]}
(events/accept-notification-success cofx (:id notif-2) nil))))))
(rf/dispatch [:activity-center.notifications/accept (:id notif-2)]) (deftest dismiss-notification-test
(is (= {:json-rpc/call
[{:method "wakuext_dismissActivityCenterNotifications"
:params [[notification-id]]
:on-success [:activity-center.notifications/dismiss-success notification-id]
:on-error [:activity-center/process-notification-failure notification-id
:notification/dismiss]}]}
(events/dismiss-notification {:db {}} notification-id))))
(is (= [notif-2-accepted notif-1] (deftest dismiss-notification-success-test
(get-in (h/db) [:activity-center :notifications]))) (testing "does nothing if the notification ID cannot be found in the app db"
(let [cofx {:db {:activity-center
{:notifications [{:id "0x1"
:read false
:type types/one-to-one-chat}]}}}]
(is (nil? (events/dismiss-notification-success cofx "0x99")))))
;; Ignores accepted notification if the Unread filter is enabled because (testing "marks notification as dismissed and read, then reconciles"
;; accepted notifications are also marked as read in status-go. (let [notif-1 {:id "0x1" :type types/private-group-chat}
(rf/dispatch [:test/assoc-in [:activity-center :filter] notif-2 {:id "0x2" :type types/private-group-chat}
{:filter {:type types/no-type :status :unread}}]) notif-2-dismissed (assoc notif-2 :dismissed true :read true)
(rf/dispatch [:activity-center.notifications/accept (:id notif-2)]) cofx {:db {:activity-center {:filter {:type types/no-type :status :all}
(is (= [notif-1] :notifications [notif-2 notif-1]}}}]
(get-in (h/db) [:activity-center :notifications])))))) (is (= {:db {:activity-center {:filter {:type 0 :status :all}
:notifications [notif-2-dismissed notif-1]}}
(testing "logs on failure" :dispatch-n [[:activity-center.notifications/fetch-unread-count]
(test-log-on-failure [:activity-center.notifications/fetch-pending-contact-requests]]}
{:notification-id notification-id (events/dismiss-notification-success cofx (:id notif-2)))))))
:event [:activity-center.notifications/accept notification-id]
:action :notification/accept})))
(deftest notification-dismissal-test
(testing "dismisses & mark notification as read, and keep it in the app db"
(h/run-test-sync
(setup)
(let [notif-1 {:id "0x1" :type types/private-group-chat}
notif-2 {:id "0x2" :type types/admin}
dismissed-notif-1 (assoc notif-1 :dismissed true :read true)]
(h/stub-fx-with-callbacks :json-rpc/call :on-success (constantly notif-2))
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:type types/no-type :status :all}
:notifications [notif-2 notif-1]}])
(rf/dispatch [:activity-center.notifications/dismiss (:id notif-1)])
(is (= [notif-2 dismissed-notif-1]
(get-in (h/db) [:activity-center :notifications]))))))
(testing "logs on failure"
(test-log-on-failure
{:notification-id notification-id
:event [:activity-center.notifications/dismiss notification-id]
:action :notification/dismiss})))
;;;; Contact verification ;;;; Contact verification
@ -204,339 +210,228 @@
:type types/contact-verification}) :type types/contact-verification})
(deftest contact-verification-decline-test (deftest contact-verification-decline-test
(testing "declines notification and reconciles" (is (= {:json-rpc/call
(h/run-test-sync [{:method "wakuext_declineContactVerificationRequest"
(setup) :params [notification-id]
(let [spy-queue (atom [])] :on-success [:activity-center/reconcile-notifications-from-response]
(h/stub-fx-with-callbacks :json-rpc/call :on-error [:activity-center/process-notification-failure notification-id
:on-success :contact-verification/decline]}]}
(constantly contact-verification-rpc-response)) (events/contact-verification-decline {:db {}} notification-id))))
(h/spy-fx spy-queue :json-rpc/call)
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:type types/contact-verification :status :all}}])
(rf/dispatch [:activity-center.contact-verification/decline notification-id])
(is (= [contact-verification-expected-notification]
(get-in (h/db) [:activity-center :notifications]))))))
(testing "logs on failure"
(test-log-on-failure
{:notification-id notification-id
:event [:activity-center.contact-verification/decline notification-id]
:action :contact-verification/decline})))
(deftest contact-verification-reply-test (deftest contact-verification-reply-test
(testing "sends reply and reconciles" (let [reply "The answer is 42"]
(let [reply "any answer"] (is (= {:json-rpc/call
(h/run-test-sync [{:method "wakuext_acceptContactVerificationRequest"
(setup) :params [notification-id reply]
(let [spy-queue (atom [])] :on-success [:activity-center/reconcile-notifications-from-response]
(h/stub-fx-with-callbacks :json-rpc/call :on-error [:activity-center/process-notification-failure notification-id
:on-success :contact-verification/reply]}]}
(constantly contact-verification-rpc-response)) (events/contact-verification-reply {:db {}} notification-id reply)))))
(h/spy-fx spy-queue :json-rpc/call)
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:type types/contact-verification :status :all}}])
(rf/dispatch [:activity-center.contact-verification/reply notification-id reply])
(is (= [contact-verification-expected-notification]
(get-in (h/db) [:activity-center :notifications])))))))
(testing "logs on failure"
(test-log-on-failure
{:notification-id notification-id
:event [:activity-center.contact-verification/reply notification-id "any answer"]
:action :contact-verification/reply})))
(deftest contact-verification-mark-as-trusted-test (deftest contact-verification-mark-as-trusted-test
(testing "app db reconciliation" (is (= {:json-rpc/call
(h/run-test-sync [{:method "wakuext_verifiedTrusted"
(setup) :params [{:id notification-id}]
(h/stub-fx-with-callbacks :json-rpc/call :on-success [:activity-center/reconcile-notifications-from-response]
:on-success :on-error [:activity-center/process-notification-failure notification-id
(constantly contact-verification-rpc-response)) :contact-verification/mark-as-trusted]}]}
(events/contact-verification-mark-as-trusted {:db {}} notification-id))))
;; With "Unread" filter disabled
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:type types/no-type :status :all}}])
(rf/dispatch [:activity-center.contact-verification/mark-as-trusted notification-id])
(is (= [contact-verification-expected-notification]
(get-in (h/db) [:activity-center :notifications])))
;; With "Unread" filter enabled
(rf/dispatch [:test/assoc-in [:activity-center :filter :status] :unread])
(rf/dispatch [:activity-center.contact-verification/mark-as-trusted notification-id])
(is (= [] (get-in (h/db) [:activity-center :notifications])))))
(testing "logs on failure"
(test-log-on-failure
{:notification-id notification-id
:event [:activity-center.contact-verification/mark-as-trusted notification-id]
:action :contact-verification/mark-as-trusted})))
(deftest contact-verification-mark-as-untrustworthy-test (deftest contact-verification-mark-as-untrustworthy-test
(testing "app db reconciliation" (is (= {:json-rpc/call
(h/run-test-sync [{:method "wakuext_verifiedUntrustworthy"
(setup) :params [{:id notification-id}]
(h/stub-fx-with-callbacks :on-success [:activity-center/reconcile-notifications-from-response]
:json-rpc/call :on-error [:activity-center/process-notification-failure notification-id
:on-success :contact-verification/mark-as-untrustworthy]}]}
(constantly contact-verification-rpc-response)) (events/contact-verification-mark-as-untrustworthy {:db {}} notification-id))))
;; With "Unread" filter disabled
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:type types/no-type :status :all}}])
(rf/dispatch [:activity-center.contact-verification/mark-as-untrustworthy notification-id])
(is (= [contact-verification-expected-notification]
(get-in (h/db) [:activity-center :notifications])))
;; With "Unread" filter enabled
(rf/dispatch [:test/assoc-in [:activity-center :filter :status] :unread])
(rf/dispatch [:activity-center.contact-verification/mark-as-untrustworthy notification-id])
(is (= [] (get-in (h/db) [:activity-center :notifications])))))
(testing "logs on failure"
(test-log-on-failure
{:notification-id notification-id
:event [:activity-center.contact-verification/mark-as-untrustworthy notification-id]
:action :contact-verification/mark-as-untrustworthy})))
;;;; Notification reconciliation ;;;; Notification reconciliation
(deftest notifications-reconcile-test (deftest notifications-reconcile-test
(testing "All tab + All filter" (testing "All tab + All filter"
(h/run-test-sync (let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat}
(setup) notif-2 {:id "0x2" :read false :type types/system}
(let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat} new-notif-3 {:id "0x3" :read false :type types/system}
notif-2 {:id "0x2" :read false :type types/system} new-notif-4 {:id "0x4" :read true :type types/system}
new-notif-3 {:id "0x3" :read false :type types/system} new-notif-2 (assoc notif-2 :read true)
new-notif-4 {:id "0x4" :read true :type types/system} cofx {:db {:activity-center
new-notif-2 (assoc notif-2 :read true)] {:filter {:type types/no-type :status :all}
(rf/dispatch [:test/assoc-in [:activity-center] :notifications [notif-2 notif-1]}}}]
{:filter {:type types/no-type :status :all} (is (= {:db {:activity-center
:notifications [notif-2 notif-1]}]) {:filter {:type types/no-type :status :all}
:notifications [new-notif-4 new-notif-3 new-notif-2]}}
(rf/dispatch :dispatch-n [[:activity-center.notifications/fetch-unread-count]
[:activity-center.notifications/reconcile [:activity-center.notifications/fetch-pending-contact-requests]]}
[(assoc notif-1 :deleted true) ; will be removed (events/notifications-reconcile
new-notif-2 cofx
new-notif-3 [(assoc notif-1 :deleted true) ; will be removed
new-notif-4]]) new-notif-2
new-notif-3
(is (= [new-notif-4 new-notif-3 new-notif-2] new-notif-4])))))
(get-in (h/db) [:activity-center :notifications]))))))
(testing "All tab + Unread filter" (testing "All tab + Unread filter"
(h/run-test-sync (let [notif-1 {:id "0x1" :read false :type types/one-to-one-chat}
(setup) notif-2 {:id "0x2" :read false :type types/system}
(let [notif-1 {:id "0x1" :read false :type types/one-to-one-chat} new-notif-2 (assoc notif-2 :read true)
notif-2 {:id "0x2" :read false :type types/system} new-notif-3 {:id "0x3" :read false :type types/system}
new-notif-2 (assoc notif-2 :read true) new-notif-4 {:id "0x4" :read true :type types/system}
new-notif-3 {:id "0x3" :read false :type types/system} notif-5 {:id "0x5" :type types/system}
new-notif-4 {:id "0x4" :read true :type types/system} cofx {:db {:activity-center
notif-5 {:id "0x5" :type types/system}] {:filter {:type types/no-type :status :unread}
(rf/dispatch [:test/assoc-in [:activity-center] :notifications [notif-5 notif-2 notif-1]}}}]
{:filter {:type types/no-type :status :unread} (is (= {:db {:activity-center
:notifications [notif-5 notif-2 notif-1]}]) {:filter {:type types/no-type :status :unread}
:notifications [new-notif-3 notif-1]}}
(rf/dispatch :dispatch-n [[:activity-center.notifications/fetch-unread-count]
[:activity-center.notifications/reconcile [:activity-center.notifications/fetch-pending-contact-requests]]}
[new-notif-2 ; will be removed because it's read (events/notifications-reconcile
new-notif-3 ; will be inserted cofx
new-notif-4 ; will be ignored because it's read [new-notif-2 ; will be removed because it's read
(assoc notif-5 :deleted true) ; will be removed new-notif-3 ; will be inserted
]]) new-notif-4 ; will be ignored because it's read
(assoc notif-5 :deleted true) ; will be removed
(is (= [new-notif-3 notif-1] ])))))
(get-in (h/db) [:activity-center :notifications]))))))
(testing "Contact request tab + All filter" (testing "Contact request tab + All filter"
(h/run-test-sync (let [notif-1 {:id "0x1" :read true :type types/contact-request}
(setup) notif-2 {:id "0x2" :read false :type types/contact-request}
(let [notif-1 {:id "0x1" :read true :type types/contact-request} new-notif-2 (assoc notif-2 :read true)
notif-2 {:id "0x2" :read false :type types/contact-request} new-notif-3 {:id "0x3" :read false :type types/contact-request}
new-notif-2 (assoc notif-2 :read true) new-notif-4 {:id "0x4" :read true :type types/system}
new-notif-3 {:id "0x3" :read false :type types/contact-request} notif-5 {:id "0x5" :read false :type types/contact-request}
new-notif-4 {:id "0x4" :read true :type types/system} cofx {:db {:activity-center
notif-5 {:id "0x5" :read false :type types/contact-request}] {:filter {:type types/contact-request :status :all}
(rf/dispatch [:test/assoc-in [:activity-center] :notifications [notif-5 notif-2 notif-1]}}}]
{:filter {:type types/contact-request :status :all} (is (= {:db {:activity-center
:notifications [notif-5 notif-2 notif-1]}]) {:filter {:type types/contact-request :status :all}
:notifications [new-notif-3 new-notif-2 notif-1]}}
(rf/dispatch :dispatch-n [[:activity-center.notifications/fetch-unread-count]
[:activity-center.notifications/reconcile [:activity-center.notifications/fetch-pending-contact-requests]]}
[new-notif-2 ; will be updated (events/notifications-reconcile
new-notif-3 ; will be inserted cofx
new-notif-4 ; will be ignored because it's not a contact request [new-notif-2 ; will be updated
(assoc notif-5 :deleted true) ; will be removed new-notif-3 ; will be inserted
]]) new-notif-4 ; will be ignored because it's not a contact request
(assoc notif-5 :deleted true) ; will be removed
(is (= [new-notif-3 new-notif-2 notif-1] ])))))
(get-in (h/db) [:activity-center :notifications]))))))
(testing "Contact request tab + Unread filter" (testing "Contact request tab + Unread filter"
(h/run-test-sync (let [notif-1 {:id "0x1" :read false :type types/contact-request}
(setup) notif-2 {:id "0x2" :read false :type types/contact-request}
(let [notif-1 {:id "0x1" :read false :type types/contact-request} new-notif-2 (assoc notif-2 :read true)
notif-2 {:id "0x2" :read false :type types/contact-request} new-notif-3 {:id "0x3" :read false :type types/contact-request}
new-notif-2 (assoc notif-2 :read true) new-notif-4 {:id "0x4" :read true :type types/contact-request}
new-notif-3 {:id "0x3" :read false :type types/contact-request} new-notif-5 {:id "0x5" :read true :type types/system}
new-notif-4 {:id "0x4" :read true :type types/contact-request} notif-6 {:id "0x6" :read false :type types/contact-request}
new-notif-5 {:id "0x5" :read true :type types/system} cofx {:db {:activity-center
notif-6 {:id "0x6" :read false :type types/contact-request}] {:filter {:type types/contact-request
(rf/dispatch [:test/assoc-in [:activity-center] :status :unread}
{:filter {:type types/contact-request :status :unread} :notifications [notif-6 notif-2 notif-1]}}}]
:notifications [notif-6 notif-2 notif-1]}]) (is (= {:db {:activity-center
{:filter {:type types/contact-request
(rf/dispatch :status :unread}
[:activity-center.notifications/reconcile :notifications [new-notif-3 notif-1]}}
[new-notif-2 ; will be removed because it's read :dispatch-n [[:activity-center.notifications/fetch-unread-count]
new-notif-3 ; will be inserted [:activity-center.notifications/fetch-pending-contact-requests]]}
new-notif-4 ; will be ignored because it's read (events/notifications-reconcile
new-notif-5 ; will be ignored because it's not a contact request cofx
(assoc notif-6 :deleted true) ; will be removed [new-notif-2 ; will be removed because it's read
]]) new-notif-3 ; will be inserted
new-notif-4 ; will be ignored because it's read
(is (= [new-notif-3 notif-1] new-notif-5 ; will be ignored because it's not a contact request
(get-in (h/db) [:activity-center :notifications])))))) (assoc notif-6 :deleted true) ; will be removed
])))))
;; Sorting by timestamp and ID is compatible with what the backend does when ;; Sorting by timestamp and ID is compatible with what the backend does when
;; returning paginated results. ;; returning paginated results.
(testing "sorts notifications by timestamp and id in descending order" (testing "sorts notifications by timestamp and id in descending order"
(h/run-test-sync (let [notif-1 {:id "0x1" :timestamp 1}
(setup) notif-2 {:id "0x2" :timestamp 1}
(let [notif-1 {:id "0x1" :timestamp 1} notif-3 {:id "0x3" :timestamp 50}
notif-2 {:id "0x2" :timestamp 1} notif-4 {:id "0x4" :timestamp 100}
notif-3 {:id "0x3" :timestamp 50} notif-5 {:id "0x5" :timestamp 100}
notif-4 {:id "0x4" :timestamp 100} new-notif-1 (assoc notif-1 :last-message {})
notif-5 {:id "0x5" :timestamp 100} new-notif-4 (assoc notif-4 :last-message {})
new-notif-1 (assoc notif-1 :last-message {}) cofx {:db {:activity-center
new-notif-4 (assoc notif-4 :last-message {})] {:notifications [notif-1 notif-3 notif-4 notif-2
(rf/dispatch [:test/assoc-in [:activity-center :notifications] notif-5]}}}]
[notif-1 notif-3 notif-4 notif-2 notif-5]]) (is (= {:db {:activity-center
{:notifications [notif-5
(rf/dispatch [:activity-center.notifications/reconcile [new-notif-1 new-notif-4]]) new-notif-4
notif-3
(is (= [notif-5 new-notif-4 notif-3 notif-2 new-notif-1] notif-2
(get-in (h/db) [:activity-center :notifications]))))))) new-notif-1]}}
:dispatch-n [[:activity-center.notifications/fetch-unread-count]
[:activity-center.notifications/fetch-pending-contact-requests]]}
(events/notifications-reconcile cofx [new-notif-1 new-notif-4]))))))
(deftest remove-pending-contact-request-test (deftest remove-pending-contact-request-test
(testing "removes notification from all related filter types and status" (testing "removes notification from all related filter types and status"
(h/run-test-sync (let [author "0x99"
(setup) notif-1 {:id "0x1" :read true :type types/contact-request}
(let [author "0x99" notif-2 {:id "0x2" :read false :type types/contact-request :author author}
notif-1 {:id "0x1" :read true :type types/contact-request} notif-3 {:id "0x3" :read false :type types/private-group-chat :author author}
notif-2 {:id "0x2" :read false :type types/contact-request :author author} cofx {:db {:activity-center
notif-3 {:id "0x3" :read false :type types/private-group-chat :author author}] {:notifications
(rf/dispatch [:test/assoc-in [:activity-center :notifications] [notif-3 ; will be ignored because it's not a contact
[notif-3 ; will be ignored because it's not a contact request ; request
notif-2 ; will be removed notif-2 ; will be removed
notif-1 ; will be ignored because it's not from the same author notif-1 ; will be ignored because it's not from the
]]) ; same author
]}}}]
(rf/dispatch [:activity-center/remove-pending-contact-request author]) (is (= {:db {:activity-center {:notifications [notif-3 notif-1]}}}
(events/notifications-remove-pending-contact-request cofx author))))))
(is (= [notif-3 notif-1]
(get-in (h/db) [:activity-center :notifications])))))))
;;;; Notifications fetching and pagination ;;;; Notifications fetching and pagination
(deftest notifications-fetch-test (deftest notifications-fetch-first-page-test
(testing "fetches first page" (testing "fetches first page"
(h/run-test-sync (let [cofx {:db {}}]
(setup) (is (= {:db {:activity-center {:filter {:type types/one-to-one-chat
(let [spy-queue (atom [])] :status :unread}
(h/stub-fx-with-callbacks :loading? true}}
:json-rpc/call :json-rpc/call [{:method "wakuext_activityCenterNotifications"
:on-success :params [{:cursor ""
(constantly {:cursor "10" :limit (:notifications-per-page events/defaults)
:notifications [{:id "0x1" :activityTypes [types/one-to-one-chat]
:type types/one-to-one-chat :readType events/read-type-unread}]
:read false :on-success [:activity-center.notifications/fetch-success true]
:chatId "0x9"}]})) :on-error [:activity-center.notifications/fetch-error
(h/spy-fx spy-queue :json-rpc/call) types/one-to-one-chat :unread]}]}
(events/notifications-fetch-first-page cofx {:filter-type types/one-to-one-chat}))))))
(rf/dispatch [:activity-center.notifications/fetch-first-page
{:filter-type types/one-to-one-chat}])
(is (= :unread (get-in (h/db) [:activity-center :filter :status])))
(is (= "" (get-in @spy-queue [0 :args 0 :params 0 :cursor]))
"Should be called with empty cursor when fetching first page")
(is (= "10" (get-in (h/db) [:activity-center :cursor])))
(is (= [{:chat-id "0x9"
:chat-name nil
:chat-type types/one-to-one-chat
:group-chat false
:id "0x1"
:public? false
:last-message nil
:message nil
:read false
:reply-message nil
:type types/one-to-one-chat}]
(get-in (h/db) [:activity-center :notifications]))))))
(deftest notifications-fetch-next-next-page-test
(testing "does not fetch next page when pagination cursor reached the end" (testing "does not fetch next page when pagination cursor reached the end"
(h/run-test-sync (is (nil? (events/notifications-fetch-next-page
(setup) {:db {:activity-center {:cursor events/start-or-end-cursor}}}))))
(let [spy-queue (atom [])]
(h/spy-fx spy-queue :json-rpc/call)
(rf/dispatch [:test/assoc-in [:activity-center :cursor] ""]) (testing "fetches the next page"
(rf/dispatch [:activity-center.notifications/fetch-next-page]) (let [f-args (atom [])
(is (= [] @spy-queue))))) cursor "abc"
cofx {:db {:activity-center {:cursor cursor
:filter {:type types/one-to-one-chat
:status :unread}}}}]
(with-redefs [events/notifications-fetch
(fn [& args]
(reset! f-args args)
:result)]
(is (= :result (events/notifications-fetch-next-page cofx)))
(is (= [cofx
{:cursor cursor
:filter-type types/one-to-one-chat
:filter-status :unread
:reset-data? false}]
@f-args))))))
(testing "fetches next page when pagination cursor is not empty" (deftest notifications-fetch-error-test
(h/run-test-sync (testing "resets loading state"
(setup) (let [cofx {:db {:activity-center
(let [spy-queue (atom [])] {:loading? true
(h/stub-fx-with-callbacks :filter {:status :unread
:json-rpc/call :type types/one-to-one-chat}
:on-success :cursor ""}}}]
(constantly {:cursor "" (is (= {:db {:activity-center {:filter {:status :unread
:notifications [{:id "0x1" :type types/one-to-one-chat}
:type types/mention :cursor ""}}}
:read false (events/notifications-fetch-error cofx :dummy-error))))))
:chatId "0x9"}]}))
(h/spy-fx spy-queue :json-rpc/call)
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:status :unread :type types/mention}
:cursor "10"}])
(rf/dispatch [:activity-center.notifications/fetch-next-page])
(is (= "10" (get-in @spy-queue [0 :args 0 :params 0 :cursor]))
"Should be called with current cursor")
(is (= "" (get-in (h/db) [:activity-center :cursor])))
(is (= [{:chat-id "0x9"
:chat-name nil
:chat-type 3
:id "0x1"
:last-message nil
:message nil
:read false
:reply-message nil
:type types/mention}]
(get-in (h/db) [:activity-center :notifications]))))))
(testing "resets loading state after error"
(h/run-test-sync
(setup)
(let [spy-queue (atom [])]
(h/stub-fx-with-callbacks :json-rpc/call :on-error (constantly :fake-error))
(h/spy-event-fx spy-queue :activity-center.notifications/fetch-error)
(h/spy-fx spy-queue :json-rpc/call)
(rf/dispatch [:test/assoc-in [:activity-center]
{:filter {:status :unread :type types/one-to-one-chat}
:cursor ""}])
(rf/dispatch [:activity-center.notifications/fetch-first-page])
(is (nil? (get-in (h/db) [:activity-center :loading?])))
(is (= [:activity-center.notifications/fetch-error
types/one-to-one-chat
:unread
:fake-error]
(:args (last @spy-queue))))))))