Skip to content

Commit

Permalink
feat(notifications): Add functionality to normaliser for user notific…
Browse files Browse the repository at this point in the history
…ations

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
  • Loading branch information
duranmla committed Apr 3, 2019
1 parent 4691d5f commit 962839d
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 58 deletions.
10 changes: 2 additions & 8 deletions flow-typed/myLibDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ declare type Notification = {
topic_id: number,
};

declare type Alert = {
...Notification,
notification_type: 5 | 6,
};
declare type Alert = Notification;

declare type Message = {
...Notification,
/* all other types but the Alert ones */
};
declare type Message = Notification;

declare type User = {
admin: boolean,
Expand Down
7 changes: 2 additions & 5 deletions src/notifications/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ export class Notifications extends React.Component<Props, State> {
const { unread_notifications: unreadNotifications } = this.props.user;

if (unreadNotifications > 0) {
const { notifications } = await NotificationService.getNotifications();
const normalisedNotifications = normaliseUserNotifications({
alerts: [notifications[0]],
messages: [notifications[1]],
});
const notifications = await NotificationService.getNotifications();
const normalisedNotifications = normaliseUserNotifications(notifications);

this.setState(normalisedNotifications);

Expand Down
22 changes: 14 additions & 8 deletions src/notifications/__tests__/Notifications.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @flow
jest.mock("../NotificationService");
jest.mock("../normaliser", { normaliseUserNotifications: i => i });

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

describe("<Notifications />", () => {
let spyOnNormaliseUserNotifications;

afterEach(() => {
cleanup();
jest.clearAllMocks();
Expand All @@ -34,7 +35,12 @@ describe("<Notifications />", () => {
beforeAll(() => {
NotificationService.getNotifications = jest
.fn()
.mockResolvedValue(notificationsData);
.mockResolvedValue(notifications);

spyOnNormaliseUserNotifications = jest.spyOn(
normalisers,
"normaliseUserNotifications"
);
});

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

expect(NotificationService.getNotifications).toBeCalledTimes(1);
expect(normaliseUserNotifications).toHaveBeenCalledTimes(1);
expect(spyOnNormaliseUserNotifications).toHaveBeenCalledTimes(1);
expect(alertsElem.firstChild).toMatchInlineSnapshot(
"[{\"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}]"
"[{\"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}]"
);
expect(messagesElem.firstChild).toMatchInlineSnapshot(
"[{\"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-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}]"
);
});

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

expect(NotificationService.getNotifications).toBeCalledTimes(0);
expect(normaliseUserNotifications).toHaveBeenCalledTimes(0);
expect(spyOnNormaliseUserNotifications).toHaveBeenCalledTimes(0);
expect(alertsElem.firstChild).toMatchInlineSnapshot("[]");
expect(messagesElem.firstChild).toMatchInlineSnapshot("[]");
});
Expand Down
86 changes: 51 additions & 35 deletions src/notifications/__tests__/__fixtures__/notifications.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
export default {
notifications: [
{
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,
export const notifications = [
{
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",
},
{
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,
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-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,
},
{
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,
},
];
13 changes: 13 additions & 0 deletions src/notifications/__tests__/normaliser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow

import { normaliseUserNotifications } from "../normaliser";
import { notifications } from "./__fixtures__/notifications";

describe("normaliser", () => {
it("returns categorised notifications by alerts and messages", () => {
const result = normaliseUserNotifications(notifications);

expect(result.alerts).toEqual([notifications[1]]);
expect(result.messages).toEqual([notifications[0], notifications[2]]);
});
});
48 changes: 46 additions & 2 deletions src/notifications/normaliser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
export const normaliseUserNotifications = notifications => {
return notifications;
// @flow

/**
* check discourse notification types at: https://goo.gl/Lcyhp3
* besides, keep into account that Discourse return `notification_type`
* as a number which map to the below indexes
*/
const notificationTypes = [
"mentioned",
"replied",
"quoted",
"edited",
"liked",
"privateMessage",
"invitedToPrivateMessage",
"inviteeAccepted",
"posted",
"movedPost",
"linked",
"grantedBadge",
"invitedToTopic",
"custom",
"groupMentioned",
"groupMessageSummary",
"watchingFirstPost",
"topicReminder",
];

const getMessages = notifications => {
const messagesTypes = [notificationTypes[5], notificationTypes[6]];

return notifications.filter(
n => messagesTypes.indexOf(notificationTypes[n.notification_type]) > -1
);
};

export const normaliseUserNotifications = (
notifications: Array<Notification>
): { alerts: Array<Alert>, messages: Array<Message> } => {
const messages = getMessages(notifications);
const alerts = notifications.filter(n => !messages.includes(n));

return {
alerts,
messages,
};
};

0 comments on commit 962839d

Please sign in to comment.