From 0067468d5794f599886a54537e60f61620942a59 Mon Sep 17 00:00:00 2001 From: Ibrahem Khalil Date: Sun, 16 Jul 2023 15:02:32 +0300 Subject: [PATCH] Fix failing mute till test (#16453) --- src/status_im2/common/home/actions/view.cljs | 4 +- src/status_im2/common/muting/helpers.cljs | 39 +++++++++ .../common/muting/helpers_test.cljs | 83 +++++++++++++++++++ src/status_im2/contexts/chat/events.cljs | 6 +- .../communities/actions/chat/view.cljs | 8 +- src/utils/datetime.cljs | 32 ------- src/utils/datetime_test.cljs | 75 +---------------- 7 files changed, 132 insertions(+), 115 deletions(-) create mode 100644 src/status_im2/common/muting/helpers.cljs create mode 100644 src/status_im2/common/muting/helpers_test.cljs diff --git a/src/status_im2/common/home/actions/view.cljs b/src/status_im2/common/home/actions/view.cljs index a7c6a93b65..4bb9910aa4 100644 --- a/src/status_im2/common/home/actions/view.cljs +++ b/src/status_im2/common/home/actions/view.cljs @@ -9,7 +9,7 @@ [utils.i18n :as i18n] [utils.re-frame :as rf] [status-im2.common.mute-chat-drawer.view :as mute-chat-drawer] - [utils.datetime :as datetime])) + [status-im2.common.muting.helpers :refer [format-mute-till]])) (defn- entry [{:keys [icon label on-press danger? sub-label chevron? add-divider? accessibility-label]}] @@ -133,7 +133,7 @@ :sub-label (when (and muted? (some? muted-till)) (str (i18n/label :t/muted-until) " " - (datetime/format-mute-till muted-till))) + (format-mute-till muted-till))) :on-press (if muted? #(unmute-chat-action chat-id) #(mute-chat-action chat-id chat-type)) diff --git a/src/status_im2/common/muting/helpers.cljs b/src/status_im2/common/muting/helpers.cljs new file mode 100644 index 0000000000..63781c1cb8 --- /dev/null +++ b/src/status_im2/common/muting/helpers.cljs @@ -0,0 +1,39 @@ +(ns status-im2.common.muting.helpers + (:require [cljs-time.core :as t] + [utils.i18n :as i18n] + [utils.datetime :refer + [go-default-time int->weekday months + time-zone-offset today? tomorrow?]]) + (:require [cljs-time.format :as t.format])) + +(defn- add-leading-zero + [input-string] + (if (> 10 input-string) + (str "0" input-string) + input-string)) + +(defn format-mute-till + [muted-till-string] + (let [parsed-time (t.format/parse (t.format/formatters :date-time-no-ms) muted-till-string) + hours-and-minutes (str (add-leading-zero (t/hour (t/plus parsed-time time-zone-offset))) + ":" + (add-leading-zero (t/minute parsed-time))) + when-to-unmute (cond (= go-default-time + muted-till-string) (i18n/label :t/until-you-turn-it-back-on) + (today? parsed-time) (str hours-and-minutes " today") + (tomorrow? parsed-time) (str hours-and-minutes " tomorrow") + :else (str hours-and-minutes + " " + (i18n/label + (keyword "t" + (get int->weekday + (t/day-of-week + parsed-time)))) + " " + (t/day parsed-time) + " " + (i18n/label + (keyword "t" + (get months + (t/month parsed-time))))))] + when-to-unmute)) diff --git a/src/status_im2/common/muting/helpers_test.cljs b/src/status_im2/common/muting/helpers_test.cljs new file mode 100644 index 0000000000..56d7a93567 --- /dev/null +++ b/src/status_im2/common/muting/helpers_test.cljs @@ -0,0 +1,83 @@ +(ns status-im2.common.muting.helpers-test + (:require [cljs-time.coerce :as time-coerce] + [cljs-time.core :as t] + [cljs-time.format :as t.format] + [cljs.test :refer-macros [deftest testing is are]] + [clojure.string :as string] + [utils.datetime :as datetime] + [status-im2.common.muting.helpers :as muting])) + +(def mock-current-time-epoch 1655731506000) + +(deftest format-mute-till-test + (with-redefs [t/*ms-fn* (constantly mock-current-time-epoch)] + (let [remove-msecs #(string/replace % #"\.\w*Z" "Z") + time-str-to-obj #(t.format/parse (remove-msecs (time-coerce/to-string %))) + curr-time (t/now) + custom-HH-MM-formatter (t.format/formatter "HH:mm") + custom-DD-formatter (t.format/formatter "DD") + get-hh-mm #(t.format/unparse custom-HH-MM-formatter %) + get-day #(t.format/unparse custom-DD-formatter %) + get-week-day #(->> % + t/day-of-week + (get datetime/int->weekday)) + mock-today (t.format/unparse (t.format/formatters :date-time-no-ms) curr-time) + in-n-days #(-> (time-str-to-obj mock-today) + (t/plus (t/days %))) + in-n-minutes #(-> (time-str-to-obj mock-today) + (t/plus (t/minutes %))) + in-n-hours #(-> (time-str-to-obj mock-today) + (t/plus (t/hours %))) + mock-tomorrow (in-n-days 1) + mock-in-two-days (in-n-days 2) + mock-in-three-days (in-n-days 3) + mock-in-four-days (in-n-days 4) + mock-in-five-days (in-n-days 5) + mock-in-six-days (in-n-days 6) + mock-in-15-minutes (in-n-minutes 15) + mock-in-1-hour (in-n-hours 1) + mock-in-8-hour (in-n-hours 8) + get-month-day-int #(int (get-day %)) + today? (fn [mocked curr-time] + (= + (t.format/unparse (t.format/formatter "MM:DD") mocked) + (t.format/unparse (t.format/formatter "MM:DD") curr-time))) + tomorrow? (fn [mocked curr-time] + (some #(= % + (- + (int (get-month-day-int mocked)) + (int (get-month-day-int curr-time)))) + [1 30 29 27])) + form-full-date #(str (get-hh-mm %) + " " (string/capitalize (get-week-day %)) + " " (get-month-day-int %) + " " (string/capitalize (get datetime/months (t/month %)))) + today-date #(str (get-hh-mm %) " today") + tomorrow-date #(str (get-hh-mm %) " tomorrow") + write-date #(cond (today? % curr-time) (today-date %) + (tomorrow? % curr-time) (tomorrow-date %) + :else (form-full-date %)) + will-unmute-in-1-hour (remove-msecs (time-coerce/to-string mock-in-1-hour)) + will-unmute-in-8-hours (remove-msecs (time-coerce/to-string mock-in-8-hour)) + will-unmute-in-15-mins (remove-msecs (time-coerce/to-string mock-in-15-minutes)) + will-unmute-in-two-days (remove-msecs (time-coerce/to-string mock-in-two-days)) + will-unmute-tomorrow (remove-msecs (time-coerce/to-string mock-tomorrow)) + will-unmute-in-three-days (remove-msecs (time-coerce/to-string mock-in-three-days)) + will-unmute-in-four-days (remove-msecs (time-coerce/to-string mock-in-four-days)) + will-unmute-in-five-days (remove-msecs (time-coerce/to-string mock-in-five-days)) + will-unmute-in-six-days (remove-msecs (time-coerce/to-string mock-in-six-days))] + (testing "Mute for minutes and hours" + (are [arg expected] (= (muting/format-mute-till arg) expected) + will-unmute-in-15-mins (write-date mock-in-15-minutes) + will-unmute-in-1-hour (write-date mock-in-1-hour) + will-unmute-in-8-hours (write-date mock-in-8-hour))) + (testing "Weekdays" + (are [arg expected] (= (muting/format-mute-till arg) expected) + will-unmute-tomorrow (write-date mock-tomorrow) + will-unmute-in-two-days (write-date mock-in-two-days) + will-unmute-in-three-days (write-date mock-in-three-days) + will-unmute-in-four-days (write-date mock-in-four-days) + will-unmute-in-five-days (write-date mock-in-five-days) + will-unmute-in-six-days (write-date mock-in-six-days))) + (testing "Until the user turns it back on" + (is (= "you turn it back on" (muting/format-mute-till datetime/go-default-time))))))) diff --git a/src/status_im2/contexts/chat/events.cljs b/src/status_im2/contexts/chat/events.cljs index 1376920a33..2a44cd5524 100644 --- a/src/status_im2/contexts/chat/events.cljs +++ b/src/status_im2/contexts/chat/events.cljs @@ -17,10 +17,10 @@ [status-im.utils.types :as types] [reagent.core :as reagent] [quo2.foundations.colors :as colors] - [utils.datetime :as datetime] [re-frame.core :as re-frame] [status-im.async-storage.core :as async-storage] - [status-im2.contexts.shell.jump-to.constants :as shell.constants])) + [status-im2.contexts.shell.jump-to.constants :as shell.constants] + [status-im2.common.muting.helpers :refer [format-mute-till]])) (defn- get-chat [cofx chat-id] @@ -348,7 +348,7 @@ :icon-color (colors/theme-colors colors/success-60 colors/success-50) :text (mute-duration-text (when (some? muted-till) - (str (datetime/format-mute-till muted-till))))}]})) + (str (format-mute-till muted-till))))}]})) (rf/defn mute-chat {:events [:chat.ui/mute]} diff --git a/src/status_im2/contexts/communities/actions/chat/view.cljs b/src/status_im2/contexts/communities/actions/chat/view.cljs index 1f661a29e6..daa304ae78 100644 --- a/src/status_im2/contexts/communities/actions/chat/view.cljs +++ b/src/status_im2/contexts/communities/actions/chat/view.cljs @@ -1,10 +1,10 @@ (ns status-im2.contexts.communities.actions.chat.view (:require [quo2.core :as quo] [status-im2.common.not-implemented :as not-implemented] - [utils.datetime :as datetime] [utils.i18n :as i18n] - [status-im2.common.mute-chat-drawer.view :as mute-chat-drawer] - [utils.re-frame :as rf])) + [utils.re-frame :as rf] + [status-im2.common.muting.helpers :refer [format-mute-till]] + [status-im2.common.mute-chat-drawer.view :as mute-chat-drawer])) (defn hide-sheet-and-dispatch [event] @@ -52,7 +52,7 @@ :sub-label (when muted (str (i18n/label :t/muted-until) " " - (datetime/format-mute-till muted-till))) + (format-mute-till muted-till))) :on-press (if muted? #(unmute-channel-action id) #(mute-channel-action id chat-type)) diff --git a/src/utils/datetime.cljs b/src/utils/datetime.cljs index 803fad9936..b0da527c49 100644 --- a/src/utils/datetime.cljs +++ b/src/utils/datetime.cljs @@ -296,35 +296,3 @@ (def ^:const go-default-time "Zero value for golang's time var" "0001-01-01T00:00:00Z") - -(defn- add-leading-zero - [input-string] - (if (> 10 input-string) - (str "0" input-string) - input-string)) - -(defn format-mute-till - [muted-till-string] - (let [parsed-time (t.format/parse (t.format/formatters :date-time-no-ms) muted-till-string) - hours-and-minutes (str (add-leading-zero (t/hour (t/plus parsed-time time-zone-offset))) - ":" - (add-leading-zero (t/minute parsed-time))) - when-to-unmute (cond (= go-default-time - muted-till-string) (i18n/label :t/until-you-turn-it-back-on) - (today? parsed-time) (str hours-and-minutes " today") - (tomorrow? parsed-time) (str hours-and-minutes " tomorrow") - :else (str hours-and-minutes - " " - (i18n/label - (keyword "t" - (get int->weekday - (t/day-of-week - parsed-time)))) - " " - (t/day parsed-time) - " " - (i18n/label - (keyword "t" - (get months - (t/month parsed-time))))))] - when-to-unmute)) diff --git a/src/utils/datetime_test.cljs b/src/utils/datetime_test.cljs index e9af903ce3..ca4a9fe1a0 100644 --- a/src/utils/datetime_test.cljs +++ b/src/utils/datetime_test.cljs @@ -23,6 +23,7 @@ (def epoch 0) ;; 1970-01-03 00:00:00 UTC (def epoch-plus-3d 172800000) +(def mock-current-time-epoch 1655731506000) (deftest is-24-hour-locale-en-test (is (= (#'utils.datetime/is-24-hour-locsym (i18n-goog/locale-symbols "en")) false))) @@ -189,77 +190,3 @@ "it" #'utils.datetime/medium-date-time-format)] (is (= (datetime/day-relative epoch) "01 gen 1970, 12:00:00 AM"))))) - -;; TODO(@ibrkhalil): This test sometimes fail in the CI. Make the test agnostic -;; of the current time and timezone. -#_(deftest format-mute-till-test - (let [remove-msecs #(string/replace % #"\.\w*Z" "Z") - time-str-to-obj #(t.format/parse (remove-msecs (time-coerce/to-string %))) - curr-time (t/now) - custom-HH-MM-formatter (t.format/formatter "HH:mm") - custom-DD-formatter (t.format/formatter "DD") - get-hh-mm #(t.format/unparse custom-HH-MM-formatter %) - get-day #(t.format/unparse custom-DD-formatter %) - get-week-day #(->> % - t/day-of-week - (get datetime/int->weekday)) - mock-today (t.format/unparse (t.format/formatters :date-time-no-ms) curr-time) - in-n-days #(-> (time-str-to-obj mock-today) - (t/plus (t/days %))) - in-n-minutes #(-> (time-str-to-obj mock-today) - (t/plus (t/minutes %))) - in-n-hours #(-> (time-str-to-obj mock-today) - (t/plus (t/hours %))) - mock-tomorrow (in-n-days 1) - mock-in-two-days (in-n-days 2) - mock-in-three-days (in-n-days 3) - mock-in-four-days (in-n-days 4) - mock-in-five-days (in-n-days 5) - mock-in-six-days (in-n-days 6) - mock-in-15-minutes (in-n-minutes 15) - mock-in-1-hour (in-n-hours 1) - mock-in-8-hour (in-n-hours 8) - get-month-day-int #(int (get-day %)) - today? (fn [mocked curr-time] - (= - (t.format/unparse (t.format/formatter "MM:DD") mocked) - (t.format/unparse (t.format/formatter "MM:DD") curr-time))) - tomorrow? (fn [mocked curr-time] - (some #(= % - (- - (int (get-month-day-int mocked)) - (int (get-month-day-int curr-time)))) - [1 30 29 27])) - form-full-date #(str (get-hh-mm %) - " " (string/capitalize (get-week-day %)) - " " (get-month-day-int %) - " " (string/capitalize (get datetime/months (t/month %)))) - today-date #(str (get-hh-mm %) " today") - tomorrow-date #(str (get-hh-mm %) " tomorrow") - write-date #(cond (today? % curr-time) (today-date %) - (tomorrow? % curr-time) (tomorrow-date %) - :else (form-full-date %)) - will-unmute-in-1-hour (remove-msecs (time-coerce/to-string mock-in-1-hour)) - will-unmute-in-8-hours (remove-msecs (time-coerce/to-string mock-in-8-hour)) - will-unmute-in-15-mins (remove-msecs (time-coerce/to-string mock-in-15-minutes)) - will-unmute-in-two-days (remove-msecs (time-coerce/to-string mock-in-two-days)) - will-unmute-tomorrow (remove-msecs (time-coerce/to-string mock-tomorrow)) - will-unmute-in-three-days (remove-msecs (time-coerce/to-string mock-in-three-days)) - will-unmute-in-four-days (remove-msecs (time-coerce/to-string mock-in-four-days)) - will-unmute-in-five-days (remove-msecs (time-coerce/to-string mock-in-five-days)) - will-unmute-in-six-days (remove-msecs (time-coerce/to-string mock-in-six-days))] - (testing "Mute for minutes and hours" - (are [arg expected] (= (datetime/format-mute-till arg) expected) - will-unmute-in-15-mins (write-date mock-in-15-minutes) - will-unmute-in-1-hour (write-date mock-in-1-hour) - will-unmute-in-8-hours (write-date mock-in-8-hour))) - (testing "Weekdays" - (are [arg expected] (= (datetime/format-mute-till arg) expected) - will-unmute-tomorrow (write-date mock-tomorrow) - will-unmute-in-two-days (write-date mock-in-two-days) - will-unmute-in-three-days (write-date mock-in-three-days) - will-unmute-in-four-days (write-date mock-in-four-days) - will-unmute-in-five-days (write-date mock-in-five-days) - will-unmute-in-six-days (write-date mock-in-six-days))) - (testing "Until the user turns it back on" - (is (= "you turn it back on" (datetime/format-mute-till datetime/go-default-time))))))