forked from CodeYourFuture/Module-Data-Groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.test.js
More file actions
83 lines (70 loc) · 2.47 KB
/
script.test.js
File metadata and controls
83 lines (70 loc) · 2.47 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
80
81
82
83
require("@testing-library/jest-dom");
const path = require("path");
const { JSDOM } = require("jsdom");
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;
},
});
return new Promise((res) => {
page.window.document.addEventListener("load", res);
});
});
afterEach(() => {
page = null;
});
describe("Reading list", () => {
test("renders a list of books with author and title", () => {
const readingList = page.window.document.querySelector("#reading-list");
expect(readingList).toHaveTextContent("The Design of Everyday Things");
expect(readingList).toHaveTextContent("Don Norman");
expect(readingList).toHaveTextContent("The Most Human Human");
expect(readingList).toHaveTextContent("Brian Christian");
expect(readingList).toHaveTextContent("The Pragmatic Programmer");
expect(readingList).toHaveTextContent("Andrew Hunt");
});
test("each book in the list has an image", () => {
const firstLi = page.window.document.querySelector(
"#reading-list > :first-child"
);
expect(firstLi).toContainHTML(
`<img src="https://blackwells.co.uk/jacket/l/9780465050659.jpg" />`
);
const secondLi = page.window.document.querySelector(
"#reading-list > :nth-child(2)"
);
expect(secondLi).toContainHTML(
`<img src="https://images-na.ssl-images-amazon.com/images/I/41m1rQjm5tL._SX322_BO1,204,203,200_.jpg" />`
);
const thirdLi = page.window.document.querySelector(
"#reading-list > :nth-child(3)"
);
expect(thirdLi).toContainHTML(
`<img src="https://blackwells.co.uk/jacket/l/9780135957059.jpg" />`
);
});
test("background color changes depending on whether book has been read", () => {
const firstLi = page.window.document.querySelector(
"#reading-list > :first-child"
);
expect(firstLi).toHaveClass("notRead");
const secondLi = page.window.document.querySelector(
"#reading-list > :nth-child(2)"
);
expect(secondLi).toHaveClass("read");
const thirdLi = page.window.document.querySelector(
"#reading-list > :nth-child(3)"
);
expect(thirdLi).toHaveClass("read");
});
});