forked from cypress-io/cypress-realworld-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-notifications.spec.ts
102 lines (88 loc) · 2.94 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
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 = {};
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);
});
});
});
});