forked from CodeYourFuture/Module-Data-Groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes.test.js
More file actions
79 lines (64 loc) · 2.48 KB
/
quotes.test.js
File metadata and controls
79 lines (64 loc) · 2.48 KB
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
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
*/
const path = require("path");
const { JSDOM } = require("jsdom");
require("@testing-library/jest-dom");
let page = null;
beforeEach(async () => {
page = await JSDOM.fromFile(path.join(__dirname, "index.html"), {
resources: "usable",
runScripts: "dangerously",
});
// do this so students can use element.innerText which jsdom does not implement
Object.defineProperty(page.window.HTMLElement.prototype, "innerText", {
get() {
return this.textContent;
},
set(value) {
this.textContent = value;
},
});
jest
.spyOn(page.window.Math, "random")
.mockReturnValueOnce(0.02) // random number to target Albert Einstein quote [index: 2]
.mockReturnValueOnce(0.25) // random number to target Maya Angelou quote [index: 25]
.mockReturnValueOnce(0.79); // random number to target Rosa Parks quote [index: 80]
return new Promise((res) => {
page.window.document.addEventListener("load", res);
});
});
afterEach(() => {
page = null;
jest.restoreAllMocks();
});
describe("Quote generator", () => {
test("initially displays quote and author", () => {
const quoteP = page.window.document.querySelector("#quote");
const authorP = page.window.document.querySelector("#author");
expect(quoteP).toHaveTextContent(
"Strive not to be a success, but rather to be of value."
);
expect(authorP).toHaveTextContent("Albert Einstein");
});
test("can change quote to another random quote", () => {
const quoteP = page.window.document.querySelector("#quote");
const authorP = page.window.document.querySelector("#author");
const newQuoteBtn = page.window.document.querySelector("#new-quote");
expect(quoteP).toHaveTextContent(
"Strive not to be a success, but rather to be of value."
);
expect(authorP).toHaveTextContent("Albert Einstein");
expect(newQuoteBtn).toHaveTextContent("New quote");
newQuoteBtn.click();
expect(quoteP).toHaveTextContent(
"I've learned that people will forget what you said, people will forget what you did, but people will never forget how you made them feel."
);
expect(authorP).toHaveTextContent("Maya Angelou");
newQuoteBtn.click();
expect(quoteP).toHaveTextContent(
"I have learned over the years that when one's mind is made up, this diminishes fear."
);
expect(authorP).toHaveTextContent("Rosa Parks");
});
});