-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
transaction-feeds.spec.ts
433 lines (361 loc) · 16.8 KB
/
transaction-feeds.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import Dinero from "dinero.js";
import {
User,
Transaction,
TransactionRequestStatus,
TransactionResponseItem,
Contact,
TransactionStatus,
} from "../../../src/models";
import { addDays, isWithinInterval, startOfDay } from "date-fns";
import { startOfDayUTC, endOfDayUTC } from "../../../src/utils/transactionUtils";
import { isMobile } from "../../support/utils";
const { _ } = Cypress;
type TransactionFeedsCtx = {
allUsers?: User[];
user?: User;
contactIds?: string[];
};
describe("Transaction Feed", function () {
const ctx: TransactionFeedsCtx = {};
const feedViews = {
public: {
tab: "public-tab",
tabLabel: "everyone",
routeAlias: "publicTransactions",
service: "publicTransactionService",
},
contacts: {
tab: "contacts-tab",
tabLabel: "friends",
routeAlias: "contactsTransactions",
service: "contactTransactionService",
},
personal: {
tab: "personal-tab",
tabLabel: "mine",
routeAlias: "personalTransactions",
service: "personalTransactionService",
},
};
beforeEach(function () {
cy.task("db:seed");
cy.intercept("GET", "/notifications").as("notifications");
cy.intercept("GET", "/transactions*").as(feedViews.personal.routeAlias);
cy.intercept("GET", "/transactions/public*").as(feedViews.public.routeAlias);
cy.intercept("GET", "/transactions/contacts*").as(feedViews.contacts.routeAlias);
cy.database("filter", "users").then((users: User[]) => {
ctx.user = users[0];
ctx.allUsers = users;
cy.loginByXstate(ctx.user.username);
});
});
describe("app layout and responsiveness", function () {
it("toggles the navigation drawer", function () {
cy.wait("@notifications");
cy.wait("@publicTransactions");
if (isMobile()) {
cy.getBySel("sidenav-home").should("not.exist");
cy.visualSnapshot("Mobile Initial Side Navigation Not Visible");
cy.getBySel("sidenav-toggle").click();
cy.getBySel("sidenav-home").should("be.visible");
cy.visualSnapshot("Mobile Toggle Side Navigation Visible");
cy.get(".MuiBackdrop-root").click({ force: true });
cy.getBySel("sidenav-home").should("not.exist");
cy.visualSnapshot("Mobile Home Link Side Navigation Not Visible");
cy.getBySel("sidenav-toggle").click();
cy.getBySel("sidenav-home").click().should("not.exist");
cy.visualSnapshot("Mobile Toggle Side Navigation Not Visible");
} else {
cy.getBySel("sidenav-home").should("be.visible");
cy.visualSnapshot("Desktop Side Navigation Visible");
cy.getBySel("sidenav-toggle").click();
cy.getBySel("sidenav-home").should("not.be.visible");
cy.visualSnapshot("Desktop Side Navigation Not Visible");
}
});
});
describe("renders and paginates all transaction feeds", function () {
it("renders transactions item variations in feed", function () {
cy.intercept("GET", "/transactions/public*", {
headers: {
"X-Powered-By": "Express",
Date: new Date().toString(),
},
fixture: "public-transactions.json",
}).as("mockedPublicTransactions");
// Visit page again to trigger call to /transactions/public
cy.visit("/");
cy.wait("@notifications");
cy.wait("@mockedPublicTransactions")
.its("response.body.results")
.then((transactions) => {
const getTransactionFromEl = ($el: JQuery<Element>): TransactionResponseItem => {
const transactionId = $el.data("test").split("transaction-item-")[1];
return _.find(transactions, (transaction) => {
return transaction.id === transactionId;
})!;
};
cy.log("🚩Testing a paid payment transaction item");
cy.contains("[data-test*='transaction-item']", "paid").within(($el) => {
const transaction = getTransactionFromEl($el);
const formattedAmount = Dinero({
amount: transaction.amount,
}).toFormat();
expect([TransactionStatus.pending, TransactionStatus.complete]).to.include(
transaction.status
);
expect(transaction.requestStatus).to.be.empty;
cy.getBySelLike("like-count").should("have.text", `${transaction.likes.length}`);
cy.getBySelLike("comment-count").should("have.text", `${transaction.comments.length}`);
cy.getBySelLike("sender").should("contain", transaction.senderName);
cy.getBySelLike("receiver").should("contain", transaction.receiverName);
cy.getBySelLike("amount")
.should("contain", `-${formattedAmount}`)
.should("have.css", "color", "rgb(255, 0, 0)");
});
cy.log("🚩Testing a charged payment transaction item");
cy.contains("[data-test*='transaction-item']", "charged").within(($el) => {
const transaction = getTransactionFromEl($el);
const formattedAmount = Dinero({
amount: transaction.amount,
}).toFormat();
expect(TransactionStatus.complete).to.equal(transaction.status);
expect(transaction.requestStatus).to.equal(TransactionRequestStatus.accepted);
cy.getBySelLike("amount")
.should("contain", `+${formattedAmount}`)
.should("have.css", "color", "rgb(76, 175, 80)");
});
cy.log("🚩Testing a requested payment transaction item");
cy.contains("[data-test*='transaction-item']", "requested").within(($el) => {
const transaction = getTransactionFromEl($el);
const formattedAmount = Dinero({
amount: transaction.amount,
}).toFormat();
expect([TransactionStatus.pending, TransactionStatus.complete]).to.include(
transaction.status
);
expect([
TransactionRequestStatus.pending,
TransactionRequestStatus.rejected,
]).to.include(transaction.requestStatus);
cy.getBySelLike("amount")
.should("contain", `+${formattedAmount}`)
.should("have.css", "color", "rgb(76, 175, 80)");
});
cy.visualSnapshot("Transaction Item");
});
});
_.each(feedViews, (feed, feedName) => {
it(`paginates ${feedName} transaction feed`, function () {
cy.getBySelLike(feed.tab)
.click()
.should("have.class", "Mui-selected")
.contains(feed.tabLabel, { matchCase: false })
.should("have.css", { "text-transform": "uppercase" });
cy.getBySel("list-skeleton").should("not.exist");
cy.visualSnapshot(`Paginate ${feedName}`);
cy.wait(`@${feed.routeAlias}`)
.its("response.body.results")
.should("have.length", Cypress.env("paginationPageSize"));
// Temporary fix: https://github.com/cypress-io/cypress-realworld-app/issues/338
if (isMobile()) {
cy.wait(10);
}
cy.log("📃 Scroll to next page");
cy.getBySel("transaction-list").children().scrollTo("bottom");
cy.wait(`@${feed.routeAlias}`)
.its("response.body")
.then(({ results, pageData }) => {
expect(results).have.length(Cypress.env("paginationPageSize"));
expect(pageData.page).to.equal(2);
cy.visualSnapshot(`Paginate ${feedName} Next Page`);
cy.nextTransactionFeedPage(feed.service, pageData.totalPages);
});
cy.wait(`@${feed.routeAlias}`)
.its("response.body")
.then(({ results, pageData }) => {
expect(results).to.have.length.least(1);
expect(pageData.page).to.equal(pageData.totalPages);
expect(pageData.hasNextPages).to.equal(false);
cy.visualSnapshot(`Paginate ${feedName} Last Page`);
});
});
});
});
describe("filters transaction feeds by date range", function () {
if (isMobile()) {
it("closes date range picker modal", () => {
cy.getBySelLike("filter-date-range-button").click({ force: true });
cy.get(".Cal__Header__root").should("be.visible");
cy.visualSnapshot("Mobile Open Date Range Picker");
cy.getBySel("date-range-filter-drawer-close").click();
cy.get(".Cal__Header__root").should("not.exist");
cy.visualSnapshot("Mobile Close Date Range Picker");
});
}
_.each(feedViews, (feed, feedName) => {
it(`filters ${feedName} transaction feed by date range`, function () {
cy.database("find", "transactions").then((transaction: Transaction) => {
const dateRangeStart = startOfDay(new Date(transaction.createdAt));
const dateRangeEnd = endOfDayUTC(addDays(dateRangeStart, 1));
cy.getBySelLike(feed.tab).click().should("have.class", "Mui-selected");
cy.wait(`@${feed.routeAlias}`).its("response.body.results").as("unfilteredResults");
cy.pickDateRange(dateRangeStart, dateRangeEnd);
cy.wait(`@${feed.routeAlias}`)
.its("response.body.results")
.then((transactions: Transaction[]) => {
cy.getBySelLike("transaction-item").should("have.length", transactions.length);
transactions.forEach(({ createdAt }) => {
const createdAtDate = startOfDayUTC(new Date(createdAt));
expect(
isWithinInterval(createdAtDate, {
start: startOfDayUTC(dateRangeStart),
end: dateRangeEnd,
}),
`transaction created date (${createdAtDate.toISOString()})
is within ${dateRangeStart.toISOString()}
and ${dateRangeEnd.toISOString()}`
).to.equal(true);
});
cy.visualSnapshot("Date Range Filtered Transactions");
});
cy.log("Clearing date range filter. Data set should revert");
cy.getBySelLike("filter-date-clear-button").click({
force: true,
});
cy.getBySelLike("filter-date-range-button").should("contain", "ALL");
cy.get("@unfilteredResults").then((unfilteredResults) => {
cy.wait(`@${feed.routeAlias}`)
.its("response.body.results")
.should("deep.equal", unfilteredResults);
cy.visualSnapshot("Unfiltered Transactions");
});
});
});
it(`does not show ${feedName} transactions for out of range date limits`, function () {
const dateRangeStart = startOfDay(new Date(2014, 1, 1));
const dateRangeEnd = endOfDayUTC(addDays(dateRangeStart, 1));
cy.getBySelLike(feed.tab).click();
cy.wait(`@${feed.routeAlias}`);
cy.pickDateRange(dateRangeStart, dateRangeEnd);
cy.wait(`@${feed.routeAlias}`);
cy.getBySelLike("transaction-item").should("have.length", 0);
cy.getBySel("empty-list-header").should("contain", "No Transactions");
cy.getBySelLike("empty-create-transaction-button")
.should("have.attr", "href", "/transaction/new")
.contains("create a transaction", { matchCase: false })
.should("have.css", { "text-transform": "uppercase" });
cy.visualSnapshot("No Transactions");
});
});
});
describe("filters transaction feeds by amount range", function () {
const dollarAmountRange = {
min: 200,
max: 800,
};
_.each(feedViews, (feed, feedName) => {
it(`filters ${feedName} transaction feed by amount range`, function () {
cy.getBySelLike(feed.tab).click({ force: true }).should("have.class", "Mui-selected");
cy.wait(`@${feed.routeAlias}`).its("response.body.results").as("unfilteredResults");
cy.setTransactionAmountRange(dollarAmountRange.min, dollarAmountRange.max);
cy.getBySelLike("filter-amount-range-text").should(
"contain",
`$${dollarAmountRange.min} - $${dollarAmountRange.max}`
);
// @ts-ignore
cy.wait(`@${feed.routeAlias}`).then(({ response: { body, url } }) => {
const transactions = body.results as TransactionResponseItem[];
const urlParams = new URLSearchParams(_.last(url.split("?")));
const rawAmountMin = dollarAmountRange.min * 100;
const rawAmountMax = dollarAmountRange.max * 100;
expect(urlParams.get("amountMin")).to.equal(`${rawAmountMin}`);
expect(urlParams.get("amountMax")).to.equal(`${rawAmountMax}`);
cy.visualSnapshot("Amount Range Filtered Transactions");
transactions.forEach(({ amount }) => {
expect(amount).to.be.within(rawAmountMin, rawAmountMax);
});
});
cy.getBySelLike("amount-clear-button").click();
if (isMobile()) {
cy.getBySelLike("amount-range-filter-drawer-close").click();
cy.getBySel("amount-range-filter-drawer").should("not.exist");
} else {
cy.getBySel("transaction-list-filter-amount-clear-button").click();
cy.getBySel("main").scrollTo("top");
cy.getBySel("transaction-list-filter-date-range-button").click({ force: true });
cy.getBySel("transaction-list-filter-amount-range").should("not.be.visible");
}
cy.get("@unfilteredResults").then((unfilteredResults) => {
cy.wait(`@${feed.routeAlias}`)
.its("response.body.results")
.should("deep.equal", unfilteredResults);
cy.visualSnapshot("Unfiltered Transactions");
});
});
it(`does not show ${feedName} transactions for out of range amount limits`, function () {
cy.getBySelLike(feed.tab).click();
cy.wait(`@${feed.routeAlias}`);
cy.setTransactionAmountRange(550, 1000);
cy.getBySelLike("filter-amount-range-text").should("contain", "$550 - $1,000");
cy.wait(`@${feed.routeAlias}`);
cy.getBySelLike("transaction-item").should("have.length", 0);
cy.getBySel("empty-list-header").should("contain", "No Transactions");
cy.getBySelLike("empty-create-transaction-button")
.should("have.attr", "href", "/transaction/new")
.contains("create a transaction", { matchCase: false })
.should("have.css", { "text-transform": "uppercase" });
cy.visualSnapshot("No Transactions");
});
});
});
describe("Feed Item Visibility", () => {
it("mine feed only shows personal transactions", function () {
cy.database("filter", "contacts", { userId: ctx.user!.id }).then((contacts: Contact[]) => {
ctx.contactIds = contacts.map((contact) => contact.contactUserId);
});
cy.getBySelLike(feedViews.personal.tab).click();
cy.wait("@personalTransactions")
.its("response.body.results")
.each((transaction: Transaction) => {
const transactionParticipants = [transaction.senderId, transaction.receiverId];
expect(transactionParticipants).to.include(ctx.user!.id);
});
cy.getBySel("list-skeleton").should("not.exist");
cy.visualSnapshot("Personal Transactions");
});
it("first five items belong to contacts in public feed", function () {
cy.database("filter", "contacts", { userId: ctx.user!.id }).then((contacts: Contact[]) => {
ctx.contactIds = contacts.map((contact) => contact.contactUserId);
});
cy.wait("@publicTransactions")
.its("response.body.results")
.invoke("slice", 0, 5)
.each((transaction: Transaction) => {
const transactionParticipants = [transaction.senderId, transaction.receiverId];
const contactsInTransaction = _.intersection(transactionParticipants, ctx.contactIds!);
const message = `"${contactsInTransaction}" are contacts of ${ctx.user!.id}`;
expect(contactsInTransaction, message).to.not.be.empty;
});
cy.getBySel("list-skeleton").should("not.exist");
cy.visualSnapshot("First 5 Transaction Items belong to contacts");
});
it("friends feed only shows contact transactions", function () {
cy.database("filter", "contacts", { userId: ctx.user!.id }).then((contacts: Contact[]) => {
ctx.contactIds = contacts.map((contact) => contact.contactUserId);
});
cy.getBySelLike(feedViews.contacts.tab).click();
cy.wait("@contactsTransactions")
.its("response.body.results")
.each((transaction: Transaction) => {
const transactionParticipants = [transaction.senderId, transaction.receiverId];
const contactsInTransaction = _.intersection(ctx.contactIds!, transactionParticipants);
const message = `"${contactsInTransaction}" are contacts of ${ctx.user!.id}`;
expect(contactsInTransaction, message).to.not.be.empty;
});
cy.getBySel("list-skeleton").should("not.exist");
cy.visualSnapshot("Friends Feed only shows contacts transactions");
});
});
});