-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
api-notifications.spec.ts
107 lines (92 loc) · 3.09 KB
/
api-notifications.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { User, NotificationType, Like, Comment, Transaction } from "../../../src/models";
const apiNotifications = `${Cypress.env("apiUrl")}/notifications`;
type TestNotificationsCtx = {
authenticatedUser?: User;
transactionId?: string;
notificationId?: string;
likeId?: string;
commentId?: string;
};
describe("Notifications API", function () {
let ctx: TestNotificationsCtx = {};
before(() => {
// Hacky workaround to have the e2e tests pass when cy.visit('http://localhost:3000') is called
cy.request("GET", "/");
});
beforeEach(function () {
cy.task("db:seed");
cy.database("filter", "users").then((users: User[]) => {
ctx.authenticatedUser = users[0];
return cy.loginByApi(ctx.authenticatedUser.username);
});
cy.database("find", "transactions").then((transaction: Transaction) => {
ctx.transactionId = transaction.id;
});
cy.database("find", "notifications").then((notification: NotificationType) => {
ctx.notificationId = notification.id;
});
cy.database("find", "likes").then((like: Like) => {
ctx.likeId = like.transactionId;
});
cy.database("find", "comments").then((comment: Comment) => {
ctx.commentId = comment.transactionId;
});
});
context("GET /notifications", function () {
it("gets a list of notifications for a user", function () {
cy.request("GET", `${apiNotifications}`).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results.length).to.be.greaterThan(0);
});
});
});
context("POST /notifications", function () {
it("creates notifications for transaction, like and comment", function () {
cy.request("POST", `${apiNotifications}/bulk`, {
items: [
{
type: "payment",
transactionId: ctx.transactionId,
status: "received",
},
{
type: "like",
transactionId: ctx.transactionId,
likeId: ctx.likeId,
},
{
type: "comment",
transactionId: ctx.transactionId,
commentId: ctx.commentId,
},
],
}).then((response) => {
expect(response.status).to.eq(200);
expect(response.body.results.length).to.equal(3);
expect(response.body.results[0].transactionId).to.equal(ctx.transactionId);
});
});
});
context("PATCH /notifications/:notificationId", function () {
it("updates a notification", function () {
cy.request("PATCH", `${apiNotifications}/${ctx.notificationId}`, {
isRead: true,
}).then((response) => {
expect(response.status).to.eq(204);
});
});
it("error when invalid field sent", function () {
cy.request({
method: "PATCH",
url: `${apiNotifications}/${ctx.notificationId}`,
failOnStatusCode: false,
body: {
notANotificationField: "not a notification field",
},
}).then((response) => {
expect(response.status).to.eq(422);
expect(response.body.errors.length).to.eq(1);
});
});
});
});