-
-
Notifications
You must be signed in to change notification settings - Fork 283
Glasgow | Jan-26 | Alasdair MacDonald | Sprint 3 | Reading List #1041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MacDonald91
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
MacDonald91:coursework/reading-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,82 +1,46 @@ | ||
| 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).toHaveStyle({ backgroundColor: "red" }); | ||
|
|
||
| const secondLi = page.window.document.querySelector( | ||
| "#reading-list > :nth-child(2)" | ||
| ); | ||
| expect(secondLi).toHaveStyle({ backgroundColor: "green" }); | ||
|
|
||
| const thirdLi = page.window.document.querySelector( | ||
| "#reading-list > :nth-child(3)" | ||
| ); | ||
| expect(thirdLi).toHaveStyle({ backgroundColor: "green" }); | ||
| }); | ||
| }); | ||
| // for the tests, do not modify this array of books | ||
| const books = [ | ||
| { | ||
| title: "The Design of Everyday Things", | ||
| author: "Don Norman", | ||
| alreadyRead: false, | ||
| bookCoverImage: "https://blackwells.co.uk/jacket/l/9780465050659.jpg", | ||
| }, | ||
| { | ||
| title: "The Most Human Human", | ||
| author: "Brian Christian", | ||
| alreadyRead: true, | ||
| bookCoverImage: | ||
| "https://images-na.ssl-images-amazon.com/images/I/41m1rQjm5tL._SX322_BO1,204,203,200_.jpg", | ||
| }, | ||
| { | ||
| title: "The Pragmatic Programmer", | ||
| author: "Andrew Hunt", | ||
| alreadyRead: true, | ||
| bookCoverImage: "https://blackwells.co.uk/jacket/l/9780135957059.jpg", | ||
| }, | ||
| ]; | ||
|
|
||
| const list = document.getElementById("reading-list"); | ||
|
|
||
| books.forEach((book) => { | ||
| const li = document.createElement("li"); | ||
|
|
||
| // Add title + author (needed for tests) | ||
| li.textContent = `${book.title} by ${book.author}`; | ||
|
|
||
| // Create image | ||
| const img = document.createElement("img"); | ||
|
|
||
| // ✅ ONLY ONE way (clean fix) | ||
| img.src = book.bookCoverImage; | ||
|
|
||
| // Add image to list item | ||
| li.appendChild(img); | ||
|
|
||
| // Set background colour based on read status | ||
| li.style.backgroundColor = book.alreadyRead ? "green" : "red"; | ||
|
|
||
| // Add to page | ||
| list.appendChild(li); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
img.src = book.bookCoverImage;
img.setAttribute("src", book.bookCoverImage);
why do twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
I removed the duplicate setAttribute call and kept img.src, as it already sets the image source correctly.
This simplifies the implementation while maintaining the expected HTML output for the tests.