|
1 | | -require("@testing-library/jest-dom"); |
2 | | -const path = require("path"); |
3 | | -const { JSDOM } = require("jsdom"); |
4 | | - |
5 | | -let page = null; |
6 | | - |
7 | | -beforeEach(async () => { |
8 | | - page = await JSDOM.fromFile(path.join(__dirname, "index.html"), { |
9 | | - resources: "usable", |
10 | | - runScripts: "dangerously", |
11 | | - }); |
12 | | - |
13 | | - // do this so students can use element.innerText which jsdom does not implement |
14 | | - Object.defineProperty(page.window.HTMLElement.prototype, "innerText", { |
15 | | - get() { |
16 | | - return this.textContent; |
17 | | - }, |
18 | | - set(value) { |
19 | | - this.textContent = value; |
20 | | - }, |
21 | | - }); |
22 | | - |
23 | | - return new Promise((res) => { |
24 | | - page.window.document.addEventListener("load", res); |
25 | | - }); |
26 | | -}); |
27 | | - |
28 | | -afterEach(() => { |
29 | | - page = null; |
30 | | -}); |
31 | | - |
32 | | -describe("Reading list", () => { |
33 | | - test("renders a list of books with author and title", () => { |
34 | | - const readingList = page.window.document.querySelector("#reading-list"); |
35 | | - |
36 | | - expect(readingList).toHaveTextContent("The Design of Everyday Things"); |
37 | | - expect(readingList).toHaveTextContent("Don Norman"); |
38 | | - |
39 | | - expect(readingList).toHaveTextContent("The Most Human Human"); |
40 | | - expect(readingList).toHaveTextContent("Brian Christian"); |
41 | | - |
42 | | - expect(readingList).toHaveTextContent("The Pragmatic Programmer"); |
43 | | - expect(readingList).toHaveTextContent("Andrew Hunt"); |
44 | | - }); |
45 | | - test("each book in the list has an image", () => { |
46 | | - const firstLi = page.window.document.querySelector( |
47 | | - "#reading-list > :first-child" |
48 | | - ); |
49 | | - expect(firstLi).toContainHTML( |
50 | | - `<img src="https://blackwells.co.uk/jacket/l/9780465050659.jpg" />` |
51 | | - ); |
52 | | - |
53 | | - const secondLi = page.window.document.querySelector( |
54 | | - "#reading-list > :nth-child(2)" |
55 | | - ); |
56 | | - expect(secondLi).toContainHTML( |
57 | | - `<img src="https://images-na.ssl-images-amazon.com/images/I/41m1rQjm5tL._SX322_BO1,204,203,200_.jpg" />` |
58 | | - ); |
59 | | - |
60 | | - const thirdLi = page.window.document.querySelector( |
61 | | - "#reading-list > :nth-child(3)" |
62 | | - ); |
63 | | - expect(thirdLi).toContainHTML( |
64 | | - `<img src="https://blackwells.co.uk/jacket/l/9780135957059.jpg" />` |
65 | | - ); |
66 | | - }); |
67 | | - test("background color changes depending on whether book has been read", () => { |
68 | | - const firstLi = page.window.document.querySelector( |
69 | | - "#reading-list > :first-child" |
70 | | - ); |
71 | | - expect(firstLi).toHaveClass("notRead"); |
72 | | - |
73 | | - const secondLi = page.window.document.querySelector( |
74 | | - "#reading-list > :nth-child(2)" |
75 | | - ); |
76 | | - expect(secondLi).toHaveClass("read"); |
77 | | - |
78 | | - const thirdLi = page.window.document.querySelector( |
79 | | - "#reading-list > :nth-child(3)" |
80 | | - ); |
81 | | - expect(thirdLi).toHaveClass("read"); |
82 | | - }); |
83 | | -}); |
| 1 | +// for the tests, do not modify this array of books |
| 2 | +const books = [ |
| 3 | + { |
| 4 | + title: "The Design of Everyday Things", |
| 5 | + author: "Don Norman", |
| 6 | + alreadyRead: false, |
| 7 | + bookCoverImage: "https://blackwells.co.uk/jacket/l/9780465050659.jpg", |
| 8 | + }, |
| 9 | + { |
| 10 | + title: "The Most Human Human", |
| 11 | + author: "Brian Christian", |
| 12 | + alreadyRead: true, |
| 13 | + bookCoverImage: |
| 14 | + "https://images-na.ssl-images-amazon.com/images/I/41m1rQjm5tL._SX322_BO1,204,203,200_.jpg", |
| 15 | + }, |
| 16 | + { |
| 17 | + title: "The Pragmatic Programmer", |
| 18 | + author: "Andrew Hunt", |
| 19 | + alreadyRead: true, |
| 20 | + bookCoverImage: "https://blackwells.co.uk/jacket/l/9780135957059.jpg", |
| 21 | + }, |
| 22 | +]; |
| 23 | + |
| 24 | +function readingList() { |
| 25 | + const orderedList = document.querySelector("#reading-list"); |
| 26 | + |
| 27 | + // Base condition: check if books array is empty |
| 28 | + if (!books || books.length === 0) { |
| 29 | + const emptyMessage = document.createElement("li"); |
| 30 | + emptyMessage.className = "empty-message"; |
| 31 | + emptyMessage.textContent = |
| 32 | + "No books in your reading list yet. Add some books to get started!"; |
| 33 | + orderedList.appendChild(emptyMessage); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + for (const book of books) { |
| 38 | + const listItem = document.createElement("li"); |
| 39 | + |
| 40 | + const bookImage = document.createElement("img"); |
| 41 | + bookImage.src = book.bookCoverImage; |
| 42 | + bookImage.alt = `Cover of ${book.title}`; |
| 43 | + |
| 44 | + const bookInfo = document.createElement("p"); |
| 45 | + bookInfo.textContent = `${book.title} by ${book.author}`; |
| 46 | + |
| 47 | + listItem.append(bookImage, bookInfo); |
| 48 | + |
| 49 | + if (book.alreadyRead) { |
| 50 | + listItem.classList.add("read"); |
| 51 | + } else { |
| 52 | + listItem.classList.add("notRead"); |
| 53 | + } |
| 54 | + |
| 55 | + orderedList.appendChild(listItem); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +readingList(); |
0 commit comments