From 59b5369654d82df2facfb7a736f223d0840dd3ed Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 27 Jan 2022 18:23:31 -0800 Subject: [PATCH] Updates tests --- bookwyrm/static/js/bookwyrm.js | 5 +++ bookwyrm/tests/views/test_updates.py | 46 ++++++++++++++++++++-------- bookwyrm/views/updates.py | 6 ++-- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index eccd4c55..cf3ce303 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -122,6 +122,11 @@ let BookWyrm = new (class { */ updateCountElement(counter, data) { let count = data.count; + + if (count === undefined) { + return; + } + const currentCount = counter.innerText; const hasMentions = data.has_mentions; diff --git a/bookwyrm/tests/views/test_updates.py b/bookwyrm/tests/views/test_updates.py index 4ca704fc..03cf5866 100644 --- a/bookwyrm/tests/views/test_updates.py +++ b/bookwyrm/tests/views/test_updates.py @@ -45,31 +45,51 @@ class UpdateViews(TestCase): data = json.loads(result.getvalue()) self.assertEqual(data["count"], 1) - def test_get_unread_status_count(self): + def test_get_unread_status_string(self): """there are so many views, this just makes sure it LOADS""" request = self.factory.get("") request.user = self.local_user with patch( "bookwyrm.activitystreams.ActivityStream.get_unread_count" - ) as mock_count: - with patch( - # pylint:disable=line-too-long - "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" - ) as mock_count_by_status: - mock_count.return_value = 3 - mock_count_by_status.return_value = {"review": 5} - result = views.get_unread_status_count(request, "home") + ) as mock_count, patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" + ) as mock_count_by_status: + mock_count.return_value = 3 + mock_count_by_status.return_value = {"review": 5} + result = views.get_unread_status_string(request, "home") self.assertIsInstance(result, JsonResponse) data = json.loads(result.getvalue()) - self.assertEqual(data["count"], 3) - self.assertEqual(data["count_by_type"]["review"], 5) + self.assertEqual(data["count"], "Load 5 unread statuses") - def test_get_unread_status_count_invalid_stream(self): + def test_get_unread_status_string_with_filters(self): + """there are so many views, this just makes sure it LOADS""" + self.local_user.feed_status_types = ["comment", "everything"] + request = self.factory.get("") + request.user = self.local_user + + with patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count" + ) as mock_count, patch( + "bookwyrm.activitystreams.ActivityStream.get_unread_count_by_status_type" + ) as mock_count_by_status: + mock_count.return_value = 3 + mock_count_by_status.return_value = { + "generated_note": 1, + "comment": 1, + "review": 10, + } + result = views.get_unread_status_string(request, "home") + + self.assertIsInstance(result, JsonResponse) + data = json.loads(result.getvalue()) + self.assertEqual(data["count"], "Load 2 unread statuses") + + def test_get_unread_status_string_invalid_stream(self): """there are so many views, this just makes sure it LOADS""" request = self.factory.get("") request.user = self.local_user with self.assertRaises(Http404): - views.get_unread_status_count(request, "fish") + views.get_unread_status_string(request, "fish") diff --git a/bookwyrm/views/updates.py b/bookwyrm/views/updates.py index 3c3dc6af..82e85764 100644 --- a/bookwyrm/views/updates.py +++ b/bookwyrm/views/updates.py @@ -25,7 +25,6 @@ def get_unread_status_string(request, stream="home"): raise Http404 counts_by_type = stream.get_unread_count_by_status_type(request.user).items() - print(counts_by_type) if counts_by_type == {}: count = stream.get_unread_count(request.user) else: @@ -39,8 +38,11 @@ def get_unread_status_string(request, stream="home"): if k not in ["review", "comment", "quotation"] ) + if not count: + return JsonResponse({}) + translation_string = lambda c: ngettext( "Load %(count)d unread status", "Load %(count)d unread statuses", c ) % {"count": c} - return JsonResponse({"total": translation_string(count)}) + return JsonResponse({"count": translation_string(count)})