Skip to content

Commit 02df758

Browse files
committed
add notification tests and refactor
1 parent 08fc5ee commit 02df758

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

.clj-kondo/metosin/malli-types-cljs/config.edn

+2-4
Original file line numberDiff line numberDiff line change
@@ -4758,8 +4758,7 @@
47584758
:label :string,
47594759
:id :any,
47604760
:attrs :map,
4761-
:visible :boolean,
4762-
:cached-attrs :map},
4761+
:visible :boolean},
47634762
:req {:tag :any}},
47644763
:focused :boolean,
47654764
:filter :keyword},
@@ -4870,8 +4869,7 @@
48704869
:label :string,
48714870
:id :any,
48724871
:attrs :map,
4873-
:visible :boolean,
4874-
:cached-attrs :map},
4872+
:visible :boolean},
48754873
:req {:tag :any}},
48764874
:focused :boolean,
48774875
:filter :keyword},

src/renderer/notification/db.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
(def Notification
66
[:map {:closed true}
7-
[:count {:optional true} int?]
7+
[:count pos-int?]
88
[:content Hiccup]])

src/renderer/notification/handlers.cljs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
(if (= notification (-> notifications peek :content))
1212
(assoc db :notifications
1313
(conj (pop notifications) (update (peek notifications) :count inc)))
14-
(update db :notifications conj {:content notification}))))
14+
(update db :notifications conj {:content notification
15+
:count 1}))))

src/renderer/notification/views.cljs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(defn unavailable-feature
1010
[feature compatibility-url]
1111
[:div
12-
[:h2.font-bold.text-error feature " is unavailable"]
12+
[:h2.font-bold.text-error (str feature " is unavailable")]
1313
[:div.mt-4
1414
"Your browser does not support this API."
1515
"You can check the "
@@ -50,10 +50,10 @@
5050
{:aria-label "Close"
5151
:class "icon-button absolute top-3 right-3 small"
5252
:on-click #(rf/dispatch [::notification.e/remove-nth index])}]
53-
(when-let [n (:count notification)]
53+
(when (> (:count notification) 1)
5454
[:div.absolute.bg-error.left-0.top-0.px-1.py-0.5.rounded
5555
{:class "-translate-x-1/2 -translate-y-1/2"}
56-
(inc n)])])
56+
(:count notification)])])
5757
notifications)
5858

5959
(when (second notifications)

test/notification_test.cljs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(ns notification-test
2+
(:require
3+
[cljs.test :refer-macros [deftest is testing]]
4+
[day8.re-frame.test :as rf-test]
5+
[re-frame.core :as rf]
6+
[renderer.app.events :as app.e]
7+
[renderer.notification.events :as e]
8+
[renderer.notification.subs :as s]))
9+
10+
(deftest add-and-remove
11+
(rf-test/run-test-sync
12+
(rf/dispatch [::app.e/initialize-db])
13+
14+
(let [notifications (rf/subscribe [::s/entities])]
15+
(testing "intial"
16+
(is (= @notifications [])))
17+
18+
(testing "add"
19+
(rf/dispatch [::e/add [:div "test"]])
20+
(is (= (count @notifications) 1))
21+
(is (= (first @notifications) {:content [:div "test"]
22+
:count 1})))
23+
24+
(testing "merge identical"
25+
(rf/dispatch [::e/add [:div "test"]])
26+
(is (= (count @notifications) 1))
27+
(is (= (first @notifications) {:content [:div "test"]
28+
:count 2})))
29+
30+
(testing "add different"
31+
(rf/dispatch [::e/add [:div "test 2"]])
32+
(is (= (count @notifications) 2))
33+
(is (= (second @notifications) {:content [:div "test 2"]
34+
:count 1})))
35+
36+
(testing "remove nth"
37+
(rf/dispatch [::e/remove-nth 1])
38+
(is (= (count @notifications) 1)))
39+
40+
(testing "clear all"
41+
(rf/dispatch [::e/clear-all])
42+
(is (= (count @notifications) 0))))))
43+
44+
(deftest exception
45+
(rf-test/run-test-sync
46+
(rf/dispatch [::app.e/initialize-db])
47+
48+
(let [notifications (rf/subscribe [::s/entities])]
49+
(testing "string exception"
50+
(try
51+
(throw (js/Error. "Error message"))
52+
(catch js/Error e
53+
(rf/dispatch [::e/exception e])
54+
(is (= (:content (first @notifications))
55+
[:div
56+
[:h2.font-bold.text-error "Error"]
57+
[:div.mt-4 "Error message"]]))))))))

0 commit comments

Comments
 (0)