Skip to content

Commit e90e0d9

Browse files
committed
Updated notification unread count test scenarios
1 parent 31164e2 commit e90e0d9

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

features/notifications.feature

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ Feature: Notifications
77
When an authenticated request is made to "/.ghost/activitypub/v1/notifications?limit=200"
88
Then the request is rejected with a 400
99

10-
Scenario: Getting unread notifications count
10+
Scenario: New notifications are marked as unread
1111
Given we are following "Alice"
1212
And we are not following "Bob"
13-
And we reset unread notifications count
1413
When we get a like notification from "Alice"
15-
And we get a like notification from "Bob"
16-
And we get a reply notification from "Alice"
1714
And we get a reply notification from "Bob"
18-
Then the unread notifications count is 4
19-
When we reset unread notifications count
20-
Then the unread notifications count is 0
15+
Then we have unread notifications
16+
17+
Scenario: Reading notifications resets the unread count
18+
Given we are following "Alice"
19+
And we are not following "Bob"
20+
And we get a like notification from "Alice"
21+
And we get a reply notification from "Bob"
22+
And we have unread notifications
23+
When we visit the notifications page
24+
Then we don't have unread notifications

features/step_definitions/notifications_steps.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ import { createActivity, createObject } from '../support/fixtures.js';
77
import {
88
waitForItemInNotifications,
99
waitForUnreadNotifications,
10+
waitForZeroUnreadNotifications,
1011
} from '../support/notifications.js';
1112
import { fetchActivityPub } from '../support/request.js';
1213

13-
Then('the unread notifications count is {int}', async (count) => {
14-
const found = await waitForUnreadNotifications(count);
15-
assert(found);
16-
});
17-
1814
When('we get a like notification from {string}', async function (actorName) {
1915
if (!this.articleId) {
2016
const article = await publishArticle();
@@ -76,11 +72,21 @@ When('we get a reply notification from {string}', async function (actorName) {
7672
await waitForItemInNotifications(object.id);
7773
});
7874

79-
When('we reset unread notifications count', async () => {
75+
When('we visit the notifications page', async () => {
8076
await fetchActivityPub(
8177
'https://self.test/.ghost/activitypub/v1/notifications/unread/reset',
8278
{
8379
method: 'PUT',
8480
},
8581
);
8682
});
83+
84+
Then('we have unread notifications', async () => {
85+
const unreadNotifications = await waitForUnreadNotifications();
86+
assert(unreadNotifications);
87+
});
88+
89+
Then("we don't have unread notifications", async () => {
90+
const zeroUnreadNotifications = await waitForZeroUnreadNotifications();
91+
assert(zeroUnreadNotifications);
92+
});

features/support/notifications.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export async function waitForItemInNotifications(
5454
}
5555

5656
export async function waitForUnreadNotifications(
57-
unreadNotificationCount,
5857
options = {
5958
retryCount: 0,
6059
delay: 0,
@@ -68,23 +67,59 @@ export async function waitForUnreadNotifications(
6867

6968
const responseJson = await response.clone().json();
7069

71-
const found = responseJson.count === unreadNotificationCount;
70+
const unreadNotifications = responseJson.count > 0;
7271

73-
if (found) {
74-
return found;
72+
if (unreadNotifications) {
73+
return true;
74+
}
75+
76+
if (options.retryCount === MAX_RETRIES) {
77+
throw new Error(
78+
`Max retries reached (${MAX_RETRIES}) when waiting for unread notifications. No unread notifications found.`,
79+
);
80+
}
81+
82+
if (options.delay > 0) {
83+
await new Promise((resolve) => setTimeout(resolve, options.delay));
84+
}
85+
86+
return await waitForUnreadNotifications({
87+
retryCount: options.retryCount + 1,
88+
delay: options.delay + 500,
89+
});
90+
}
91+
92+
export async function waitForZeroUnreadNotifications(
93+
options = {
94+
retryCount: 0,
95+
delay: 0,
96+
},
97+
) {
98+
const MAX_RETRIES = 5;
99+
100+
const response = await fetchActivityPub(
101+
'https://self.test/.ghost/activitypub/v1/notifications/unread/count',
102+
);
103+
104+
const responseJson = await response.clone().json();
105+
106+
const zeroUnreadNotifications = responseJson.count === 0;
107+
108+
if (zeroUnreadNotifications) {
109+
return true;
75110
}
76111

77112
if (options.retryCount === MAX_RETRIES) {
78113
throw new Error(
79-
`Max retries reached (${MAX_RETRIES}) when waiting for notifications count ${unreadNotificationCount}. Notification count found ${responseJson.count}`,
114+
`Max retries reached (${MAX_RETRIES}) when waiting for zero unread notifications. Unread notifications found: ${responseJson.count}.`,
80115
);
81116
}
82117

83118
if (options.delay > 0) {
84119
await new Promise((resolve) => setTimeout(resolve, options.delay));
85120
}
86121

87-
return await waitForUnreadNotifications(unreadNotificationCount, {
122+
return await waitForZeroUnreadNotifications({
88123
retryCount: options.retryCount + 1,
89124
delay: options.delay + 500,
90125
});

0 commit comments

Comments
 (0)