-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
new-transaction.spec.ts
287 lines (229 loc) · 9.92 KB
/
new-transaction.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import Dinero from "dinero.js";
import { User } from "../../../src/models";
import { isMobile } from "../../support/utils";
type NewTransactionTestCtx = {
allUsers?: User[];
user?: User;
contact?: User;
};
describe("New Transaction", function () {
const ctx: NewTransactionTestCtx = {};
beforeEach(function () {
cy.task("db:seed");
cy.intercept("GET", "/users*").as("allUsers");
cy.intercept("GET", "/users/search*").as("usersSearch");
cy.intercept("POST", "/transactions").as("createTransaction");
cy.intercept("GET", "/notifications").as("notifications");
cy.intercept("GET", "/transactions/public").as("publicTransactions");
cy.intercept("GET", "/transactions").as("personalTransactions");
cy.intercept("PATCH", "/transactions/*").as("updateTransaction");
cy.database("filter", "users").then((users: User[]) => {
ctx.allUsers = users;
ctx.user = users[0];
ctx.contact = users[1];
return cy.loginByXstate(ctx.user.username);
});
});
it("navigates to the new transaction form, selects a user and submits a transaction payment", function () {
const payment = {
amount: "35",
description: "Sushi dinner 🍣",
};
cy.getBySelLike("new-transaction").click();
cy.wait("@allUsers");
cy.getBySel("user-list-search-input").type(ctx.contact!.firstName, { force: true });
cy.wait("@usersSearch");
cy.visualSnapshot("User Search First Name Input");
cy.getBySelLike("user-list-item").contains(ctx.contact!.firstName).click({ force: true });
cy.visualSnapshot("User Search First Name List Item");
cy.getBySelLike("amount-input").type(payment.amount);
cy.getBySelLike("description-input").type(payment.description);
cy.visualSnapshot("Amount and Description Input");
cy.getBySelLike("submit-payment").click();
cy.wait(["@createTransaction", "@getUserProfile"]);
cy.getBySel("alert-bar-success")
.should("be.visible")
.and("have.text", "Transaction Submitted!");
const updatedAccountBalance = Dinero({
amount: ctx.user!.balance - parseInt(payment.amount) * 100,
}).toFormat();
if (isMobile()) {
cy.getBySel("sidenav-toggle").click();
}
cy.getBySelLike("user-balance").should("contain", updatedAccountBalance);
cy.visualSnapshot("Updated User Balance");
if (isMobile()) {
cy.get(".MuiBackdrop-root").click({ force: true });
}
cy.getBySelLike("create-another-transaction").click();
cy.getBySel("app-name-logo").find("a").click();
cy.getBySelLike("personal-tab").click().should("have.class", "Mui-selected");
cy.wait("@personalTransactions");
cy.getBySel("transaction-list").first().should("contain", payment.description);
cy.database("find", "users", { id: ctx.contact!.id })
.its("balance")
.should("equal", ctx.contact!.balance + parseInt(payment.amount) * 100);
cy.getBySel("alert-bar-success").should("not.exist");
cy.visualSnapshot("Personal List Validate Transaction in List");
});
it("navigates to the new transaction form, selects a user and submits a transaction request", function () {
const request = {
amount: "95",
description: "Fancy Hotel 🏨",
};
cy.getBySelLike("new-transaction").click();
cy.wait("@allUsers");
cy.getBySelLike("user-list-item").contains(ctx.contact!.firstName).click({ force: true });
cy.visualSnapshot("User Search First Name Input");
cy.getBySelLike("amount-input").type(request.amount);
cy.getBySelLike("description-input").type(request.description);
cy.visualSnapshot("Amount and Description Input");
cy.getBySelLike("submit-request").click();
cy.wait("@createTransaction");
cy.getBySel("alert-bar-success")
.should("be.visible")
.and("have.text", "Transaction Submitted!");
cy.visualSnapshot("Transaction Request Submitted Notification");
cy.getBySelLike("return-to-transactions").click();
cy.getBySelLike("personal-tab").click().should("have.class", "Mui-selected");
cy.getBySelLike("transaction-item").should("contain", request.description);
cy.visualSnapshot("Transaction Item Description in List");
});
it("displays new transaction errors", function () {
cy.getBySelLike("new-transaction").click();
cy.wait("@allUsers");
cy.getBySelLike("user-list-item").contains(ctx.contact!.firstName).click({ force: true });
cy.getBySelLike("amount-input").type("43").find("input").clear().blur();
cy.get("#transaction-create-amount-input-helper-text")
.should("be.visible")
.and("contain", "Please enter a valid amount");
cy.getBySelLike("description-input").type("Fun").find("input").clear().blur();
cy.get("#transaction-create-description-input-helper-text")
.should("be.visible")
.and("contain", "Please enter a note");
cy.getBySelLike("submit-request").should("be.disabled");
cy.getBySelLike("submit-payment").should("be.disabled");
cy.visualSnapshot("New Transaction Errors with Submit Payment/Request Buttons Disabled");
});
it("submits a transaction payment and verifies the deposit for the receiver", function () {
cy.getBySel("nav-top-new-transaction").click();
const transactionPayload = {
transactionType: "payment",
amount: 25,
description: "Indian Food",
sender: ctx.user,
receiver: ctx.contact,
};
// first let's grab the current balance from the UI
let startBalance: string;
if (!isMobile()) {
// only check the balance display in desktop resolution
// as it is NOT shown on mobile screen
cy.get("[data-test=sidenav-user-balance]")
.invoke("text")
.then((x) => {
startBalance = x; // something like "$1,484.81"
expect(startBalance).to.match(/\$\d/);
});
}
cy.createTransaction(transactionPayload);
cy.wait("@createTransaction");
cy.getBySel("new-transaction-create-another-transaction").should("be.visible");
if (!isMobile()) {
// make sure the new balance is displayed
cy.get("[data-test=sidenav-user-balance]").should(($el) => {
// here we only make sure the text has changed
// we could also convert the balance to actual number
// and confirm the new balance is the start balance - amount
expect($el.text()).to.not.equal(startBalance);
});
}
cy.visualSnapshot("Transaction Payment Submitted Notification");
cy.switchUserByXstate(ctx.contact!.username);
const updatedAccountBalance = Dinero({
amount: ctx.contact!.balance + transactionPayload.amount * 100,
}).toFormat();
if (isMobile()) {
cy.getBySel("sidenav-toggle").click();
}
cy.getBySelLike("user-balance").should("contain", updatedAccountBalance);
cy.visualSnapshot("Verify Updated Sender Account Balance");
});
it("submits a transaction request and accepts the request for the receiver", function () {
const transactionPayload = {
transactionType: "request",
amount: 100,
description: "Fancy Hotel",
sender: ctx.user,
receiver: ctx.contact,
};
cy.getBySelLike("new-transaction").click();
cy.createTransaction(transactionPayload);
cy.wait("@createTransaction");
cy.getBySel("new-transaction-create-another-transaction").should("be.visible");
cy.visualSnapshot("receiver - Transaction Payment Submitted Notification");
cy.switchUserByXstate(ctx.contact!.username);
cy.getBySelLike("personal-tab").click();
cy.wait("@personalTransactions");
cy.getBySelLike("transaction-item")
.first()
.should("contain", transactionPayload.description)
.click({ force: true });
cy.getBySel("transaction-detail-header").should("exist");
cy.visualSnapshot("Navigate to Transaction Item");
cy.getBySelLike("accept-request").click();
cy.wait("@updateTransaction").its("response.statusCode").should("eq", 204);
cy.getBySelLike("transaction-detail-header").should("be.visible");
cy.getBySelLike("transaction-amount").should("be.visible");
cy.getBySelLike("sender-avatar").should("be.visible");
cy.getBySelLike("receiver-avatar").should("be.visible");
cy.getBySelLike("transaction-description").should("be.visible");
cy.visualSnapshot("Accept Transaction Request");
cy.switchUserByXstate(ctx.user!.username);
const updatedAccountBalance = Dinero({
amount: ctx.user!.balance + transactionPayload.amount * 100,
}).toFormat();
if (isMobile()) {
cy.getBySel("sidenav-toggle").click();
}
cy.getBySelLike("user-balance").should("contain", updatedAccountBalance);
cy.visualSnapshot("Verify Updated Sender Account Balance");
});
context("searches for a user by attribute", function () {
const searchAttrs: (keyof User)[] = [
"firstName",
"lastName",
"username",
"email",
"phoneNumber",
];
beforeEach(function () {
cy.getBySelLike("new-transaction").click();
cy.wait("@allUsers");
});
searchAttrs.forEach((attr: keyof User) => {
it(attr, function () {
const targetUser = ctx.allUsers![2];
cy.log(`Searching by **${attr}**`);
cy.getBySel("user-list-search-input").type(targetUser[attr] as string, { force: true });
cy.wait("@usersSearch")
// make sure the backend returns some results
.its("response.body.results")
.should("have.length.gt", 0)
.its("length")
.then((resultsN) => {
cy.getBySelLike("user-list-item")
// make sure the list of results is fully updated
// and shows the number of results returned from the backend
.should("have.length", resultsN)
.first()
.contains(targetUser[attr] as string);
});
cy.visualSnapshot(`User List for Search: ${attr} = ${targetUser[attr]}`);
cy.focused().clear();
cy.getBySel("users-list").should("be.empty");
cy.visualSnapshot("User List Clear Search");
});
});
});
});