Skip to content

Commit

Permalink
feat(notifications): Add logic for service to get recent notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
duranmla committed Apr 3, 2019
1 parent f144b5a commit 59d35ea
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/notifications/NotificationService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const getNotifications = async () => null;
// @flow

import DiscourseService from "../common/DiscourseService";
import get from "lodash/get";

const getNotifications = async (
{ limit }: { limit: number } = { limit: 20 }
): Promise<Array<$NonMaybeType<Notification>>> => {
try {
const response = await DiscourseService.get(
`notifications.json?recent=true&limit=${limit}`
);

return get(response, "notifications", []);
} catch (e) {
return [];
}
};

export default {
getNotifications,
Expand Down
36 changes: 36 additions & 0 deletions src/notifications/__tests__/NotificationService.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import DiscourseService from "../../common/DiscourseService";
import { notifications } from "./__fixtures__/notifications";
import NotificationService from "../NotificationService";

describe("NotificationService", () => {
describe("getNotifications", () => {
it("allows to send a request to get user recent notifications", () => {
DiscourseService.get = jest.fn().mockResolvedValueOnce(null);
NotificationService.getNotifications();

expect(DiscourseService.get).toHaveBeenNthCalledWith(
1,
"notifications.json?recent=true&limit=20"
);
});

it("returns the notifications when request is successful", done => {
const response = { notifications };

DiscourseService.get = jest.fn().mockResolvedValueOnce(response);
NotificationService.getNotifications().then(result => {
expect(result).toEqual(notifications);
done();
});
});

it("returns null when request is unsuccessful", done => {
DiscourseService.get = jest.fn().mockRejectedValueOnce("Network Error");

NotificationService.getNotifications().then(result => {
expect(result).toEqual([]);
done();
});
});
});
});

0 comments on commit 59d35ea

Please sign in to comment.