-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.spec.ts
152 lines (128 loc) · 4.84 KB
/
general.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
import { addWeeks, addYears, subDays } from "date-fns";
import {
AVERAGE_LIFE_EXPECTANCY_YEARS_MALE,
APPROX_WEEKS_PER_YEAR,
} from "../../../src/life-in-weeks/definitions";
beforeEach(() => {
cy.task("db:reset");
});
describe("life-in-weeks", () => {
const endpoint = "/life-in-weeks";
const user = {
name: "Morty Smith",
email: "test@picklerick.com",
image: "/path/to/butterbot.jpg",
birthdate: "12/02/13",
};
const mortyBirthdate = new Date(user.birthdate);
it("should redirect to sign in without a session cookie", () => {
cy.visit(endpoint);
cy.get("p").should("contain", "Please sign in to access this page");
cy.url().should("include", "/api/auth/signin");
});
it("should display birthdate form given a valid session without a birthdate and submission should work", () => {
const userNoBirthdate = {
name: user.name,
email: user.email,
image: user.image,
};
cy.task("db:seed", userNoBirthdate);
cy.login(userNoBirthdate);
cy.visit(endpoint);
cy.get("[data-cy=birthdate-form-text-field]")
.as("formTextField")
.should("exist")
.then(() => {
cy.log("Cypress login successful");
})
.type("08/24/") // May fail if testing a mobile view since datepicker does not allow typing on mobile
.get("[data-cy=birthdate-form-submit]")
.as("formSubmit")
.should("be.disabled")
.get("@formTextField")
.type("1994")
.get("@formSubmit")
.should("be.enabled")
.click();
cy.get("[data-cy=liw-pie-chart]").should("be.visible");
cy.get("[data-cy=liw-timer]").should("be.visible");
cy.get("[data-cy=liw-display-stats-button]").should("be.disabled");
cy.get("[data-cy=liw-display-table-button]").click();
cy.get("[data-cy=liw-display-table-button]").should("be.disabled");
cy.get("[data-cy=liw-display-stats-button]").should("be.enabled");
});
it("should have proper checked/disabled boxes near end of birth year", () => {
const now = addYears(mortyBirthdate, 1);
const expectedRows = Math.floor(AVERAGE_LIFE_EXPECTANCY_YEARS_MALE) + 1;
const expectedCols = APPROX_WEEKS_PER_YEAR + 1;
// Target date object specifically for this loading bug:
// https://github.com/cypress-io/cypress/issues/7455
cy.clock(now, ["Date"]);
cy.login(user);
cy.visit(endpoint);
cy.get("[data-cy=liw-display-table-button]").click();
cy.get("[data-cy=liw-table]", { timeout: 10000 })
.should("be.visible")
.get("tbody tr")
.as("rows")
.should("have.length", expectedRows)
.eq(0) // Get first row
.children() // Get columns
.should("have.length", expectedCols)
.get("@rows")
.eq(expectedRows - 1)
.children()
.should("have.length.lessThan", expectedCols); // Last row of life should end abruptly
// Before birthday is checked and not clickable
const weekBeforeNow = subDays(now, 8); // last box isn't exactly 1 week away
cy.get(`[data-tip='${weekBeforeNow.toLocaleDateString("en-US")}']`)
.should("be.checked")
.click({ force: true })
.should("be.checked");
// This week is clickable
cy.get(`[data-tip='${now.toLocaleDateString("en-US")}']`)
.should("not.be.checked")
.click()
.should("be.checked");
// After birthday is not checked and not clickable
const weekAfterNow = addWeeks(now, 1);
cy.get(`[data-tip='${weekAfterNow.toLocaleDateString("en-US")}']`)
.should("not.be.checked")
.click({ force: true })
.should("not.be.checked");
});
it("should have proper checked boxes during last day of life", () => {
const lastYearOfLife = addYears(
mortyBirthdate,
AVERAGE_LIFE_EXPECTANCY_YEARS_MALE
);
const remainingWeeksTillLast = 15;
const now = addWeeks(lastYearOfLife, remainingWeeksTillLast);
cy.clock(now, ["Date"]);
cy.login(user);
cy.visit(endpoint);
cy.get("[data-cy=liw-display-table-button]").click();
cy.get(`[data-tip='${now.toLocaleDateString("en-US")}']`)
.should("not.be.checked")
.click()
.should("be.checked");
});
// Note: Due to a weird bug (possibly with cy.tick()) I had to separate this test out into it's own
it("should all be checked after average lifespan", () => {
const lastYearOfLife = addYears(
mortyBirthdate,
AVERAGE_LIFE_EXPECTANCY_YEARS_MALE
);
const remainingWeeksTillLast = 15;
const lastWeekOfLife = addWeeks(lastYearOfLife, remainingWeeksTillLast);
const now = addWeeks(lastWeekOfLife, 1); // Make now after the average last week of life
cy.clock(now, ["Date"]);
cy.login(user);
cy.visit(endpoint);
cy.get("[data-cy=liw-display-table-button]").click();
cy.get(`[data-tip='${lastWeekOfLife.toLocaleDateString("en-US")}']`)
.should("be.checked")
.click({ force: true })
.should("be.checked");
});
});