Skip to content

Commit 962839d

Browse files
committed
feat(notifications): Add functionality to normaliser for user notifications
Remove hardcoded behaviour from normaliser for user notifications while fixing type definitions to be flow acceptable, adding notification normaliser spec and finally updating the Notifications spec to run with actual normaliser
1 parent 4691d5f commit 962839d

6 files changed

Lines changed: 128 additions & 58 deletions

File tree

flow-typed/myLibDef.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@ declare type Notification = {
2929
topic_id: number,
3030
};
3131

32-
declare type Alert = {
33-
...Notification,
34-
notification_type: 5 | 6,
35-
};
32+
declare type Alert = Notification;
3633

37-
declare type Message = {
38-
...Notification,
39-
/* all other types but the Alert ones */
40-
};
34+
declare type Message = Notification;
4135

4236
declare type User = {
4337
admin: boolean,

src/notifications/Notifications.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ export class Notifications extends React.Component<Props, State> {
4040
const { unread_notifications: unreadNotifications } = this.props.user;
4141

4242
if (unreadNotifications > 0) {
43-
const { notifications } = await NotificationService.getNotifications();
44-
const normalisedNotifications = normaliseUserNotifications({
45-
alerts: [notifications[0]],
46-
messages: [notifications[1]],
47-
});
43+
const notifications = await NotificationService.getNotifications();
44+
const normalisedNotifications = normaliseUserNotifications(notifications);
4845

4946
this.setState(normalisedNotifications);
5047

src/notifications/__tests__/Notifications.spec.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// @flow
22
jest.mock("../NotificationService");
3-
jest.mock("../normaliser", { normaliseUserNotifications: i => i });
43

54
import "jest-dom/extend-expect";
6-
import { normaliseUserNotifications } from "../normaliser";
5+
import * as normalisers from "../normaliser";
76
import { Notifications } from "../Notifications";
8-
import notificationsData from "./__fixtures__/notifications";
7+
import { notifications } from "./__fixtures__/notifications";
98
import NotificationService from "../NotificationService";
109
import React from "react";
1110
import { cleanup, render, waitForElement } from "react-testing-library";
@@ -26,6 +25,8 @@ const renderNotifications = (props: Object) => {
2625
};
2726

2827
describe("<Notifications />", () => {
28+
let spyOnNormaliseUserNotifications;
29+
2930
afterEach(() => {
3031
cleanup();
3132
jest.clearAllMocks();
@@ -34,7 +35,12 @@ describe("<Notifications />", () => {
3435
beforeAll(() => {
3536
NotificationService.getNotifications = jest
3637
.fn()
37-
.mockResolvedValue(notificationsData);
38+
.mockResolvedValue(notifications);
39+
40+
spyOnNormaliseUserNotifications = jest.spyOn(
41+
normalisers,
42+
"normaliseUserNotifications"
43+
);
3844
});
3945

4046
it("renders props of shaped object of messages and alerts", async () => {
@@ -48,12 +54,12 @@ describe("<Notifications />", () => {
4854
const messagesElem = await waitForElement(() => getByTestId("messages"));
4955

5056
expect(NotificationService.getNotifications).toBeCalledTimes(1);
51-
expect(normaliseUserNotifications).toHaveBeenCalledTimes(1);
57+
expect(spyOnNormaliseUserNotifications).toHaveBeenCalledTimes(1);
5258
expect(alertsElem.firstChild).toMatchInlineSnapshot(
53-
"[{\"created_at\":\"2019-02-16T14:37:18.891Z\",\"data\":{\"display_username\":\"system\",\"group_id\":1,\"topic_title\":\"Backup failed\"},\"fancy_title\":\"Backup failed\",\"id\":1,\"notification_type\":7,\"post_number\":1,\"read\":false,\"slug\":\"backup-failed\",\"topic_id\":11}]"
59+
"[{\"created_at\":\"2019-03-24T10:37:18.891Z\",\"data\":{\"display_username\":\"system\",\"group_id\":1,\"topic_title\":\"Backup failed\"},\"fancy_title\":\"Backup failed\",\"id\":1,\"notification_type\":7,\"post_number\":1,\"read\":false,\"slug\":\"backup-failed\",\"topic_id\":11}]"
5460
);
5561
expect(messagesElem.firstChild).toMatchInlineSnapshot(
56-
"[{\"created_at\":\"2019-03-24T10:24:32.495Z\",\"data\":{\"display_username\":\"janedoe\",\"original_post_id\":41,\"original_post_type\":1,\"original_username\":\"janedoe\",\"revision_number\":null,\"topic_title\":\"Hello, I'm Jane Doe\"},\"fancy_title\":\"Hello, I&rsquo;m Jane Doe\",\"id\":18,\"notification_type\":6,\"post_number\":1,\"read\":false,\"slug\":\"hello-im-jane-doe\",\"topic_id\":26}]"
62+
"[{\"created_at\":\"2019-03-24T10:24:32.495Z\",\"data\":{\"display_username\":\"janedoe\",\"original_post_id\":41,\"original_post_type\":1,\"original_username\":\"janedoe\",\"revision_number\":null,\"topic_title\":\"Hello, I'm Jane Doe\"},\"fancy_title\":\"Hello, I&rsquo;m Jane Doe\",\"id\":18,\"notification_type\":6,\"post_number\":1,\"read\":false,\"slug\":\"hello-im-jane-doe\",\"topic_id\":26},{\"created_at\":\"2019-03-24T11:24:32.495Z\",\"data\":{\"display_username\":\"janedoe\",\"original_post_id\":42,\"original_post_type\":1,\"original_username\":\"janedoe\",\"revision_number\":null,\"topic_title\":\"Elephant\"},\"fancy_title\":\"Elephant\",\"id\":19,\"notification_type\":5,\"post_number\":1,\"read\":false,\"slug\":\"hello-im-jane-doe\",\"topic_id\":26}]"
5763
);
5864
});
5965

@@ -67,7 +73,7 @@ describe("<Notifications />", () => {
6773
const messagesElem = await waitForElement(() => getByTestId("messages"));
6874

6975
expect(NotificationService.getNotifications).toBeCalledTimes(0);
70-
expect(normaliseUserNotifications).toHaveBeenCalledTimes(0);
76+
expect(spyOnNormaliseUserNotifications).toHaveBeenCalledTimes(0);
7177
expect(alertsElem.firstChild).toMatchInlineSnapshot("[]");
7278
expect(messagesElem.firstChild).toMatchInlineSnapshot("[]");
7379
});
Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
1-
export default {
2-
notifications: [
3-
{
4-
created_at: "2019-02-16T14:37:18.891Z",
5-
data: {
6-
display_username: "system",
7-
group_id: 1,
8-
topic_title: "Backup failed",
9-
},
10-
fancy_title: "Backup failed",
11-
id: 1,
12-
notification_type: 7,
13-
post_number: 1,
14-
read: false,
15-
slug: "backup-failed",
16-
topic_id: 11,
1+
export const notifications = [
2+
{
3+
created_at: "2019-03-24T10:24:32.495Z",
4+
data: {
5+
display_username: "janedoe",
6+
original_post_id: 41,
7+
original_post_type: 1,
8+
original_username: "janedoe",
9+
revision_number: null,
10+
topic_title: "Hello, I'm Jane Doe",
1711
},
18-
{
19-
created_at: "2019-03-24T10:24:32.495Z",
20-
data: {
21-
display_username: "janedoe",
22-
original_post_id: 41,
23-
original_post_type: 1,
24-
original_username: "janedoe",
25-
revision_number: null,
26-
topic_title: "Hello, I'm Jane Doe",
27-
},
28-
fancy_title: "Hello, I&rsquo;m Jane Doe",
29-
id: 18,
30-
notification_type: 6,
31-
post_number: 1,
32-
read: false,
33-
slug: "hello-im-jane-doe",
34-
topic_id: 26,
12+
fancy_title: "Hello, I&rsquo;m Jane Doe",
13+
id: 18,
14+
notification_type: 6,
15+
post_number: 1,
16+
read: false,
17+
slug: "hello-im-jane-doe",
18+
topic_id: 26,
19+
},
20+
{
21+
created_at: "2019-03-24T10:37:18.891Z",
22+
data: {
23+
display_username: "system",
24+
group_id: 1,
25+
topic_title: "Backup failed",
3526
},
36-
],
37-
};
27+
fancy_title: "Backup failed",
28+
id: 1,
29+
notification_type: 7,
30+
post_number: 1,
31+
read: false,
32+
slug: "backup-failed",
33+
topic_id: 11,
34+
},
35+
{
36+
created_at: "2019-03-24T11:24:32.495Z",
37+
data: {
38+
display_username: "janedoe",
39+
original_post_id: 42,
40+
original_post_type: 1,
41+
original_username: "janedoe",
42+
revision_number: null,
43+
topic_title: "Elephant",
44+
},
45+
fancy_title: "Elephant",
46+
id: 19,
47+
notification_type: 5,
48+
post_number: 1,
49+
read: false,
50+
slug: "hello-im-jane-doe",
51+
topic_id: 26,
52+
},
53+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @flow
2+
3+
import { normaliseUserNotifications } from "../normaliser";
4+
import { notifications } from "./__fixtures__/notifications";
5+
6+
describe("normaliser", () => {
7+
it("returns categorised notifications by alerts and messages", () => {
8+
const result = normaliseUserNotifications(notifications);
9+
10+
expect(result.alerts).toEqual([notifications[1]]);
11+
expect(result.messages).toEqual([notifications[0], notifications[2]]);
12+
});
13+
});

src/notifications/normaliser.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,47 @@
1-
export const normaliseUserNotifications = notifications => {
2-
return notifications;
1+
// @flow
2+
3+
/**
4+
* check discourse notification types at: https://goo.gl/Lcyhp3
5+
* besides, keep into account that Discourse return `notification_type`
6+
* as a number which map to the below indexes
7+
*/
8+
const notificationTypes = [
9+
"mentioned",
10+
"replied",
11+
"quoted",
12+
"edited",
13+
"liked",
14+
"privateMessage",
15+
"invitedToPrivateMessage",
16+
"inviteeAccepted",
17+
"posted",
18+
"movedPost",
19+
"linked",
20+
"grantedBadge",
21+
"invitedToTopic",
22+
"custom",
23+
"groupMentioned",
24+
"groupMessageSummary",
25+
"watchingFirstPost",
26+
"topicReminder",
27+
];
28+
29+
const getMessages = notifications => {
30+
const messagesTypes = [notificationTypes[5], notificationTypes[6]];
31+
32+
return notifications.filter(
33+
n => messagesTypes.indexOf(notificationTypes[n.notification_type]) > -1
34+
);
35+
};
36+
37+
export const normaliseUserNotifications = (
38+
notifications: Array<Notification>
39+
): { alerts: Array<Alert>, messages: Array<Message> } => {
40+
const messages = getMessages(notifications);
41+
const alerts = notifications.filter(n => !messages.includes(n));
42+
43+
return {
44+
alerts,
45+
messages,
46+
};
347
};

0 commit comments

Comments
 (0)