generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 195
West Midlands | 25-ITP-Sep | Baba Yusuf | Sprint 3 | Reading List #901
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
Baba05206
wants to merge
9
commits into
CodeYourFuture:main
Choose a base branch
from
Baba05206: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
9 commits
Select commit
Hold shift + click to select a range
11e30f5
Add initial implementation of mean calculation and tests
Baba05206 3cb29a0
Refactor mean calculation and tests for clarity and functionality
Baba05206 3d4c407
Update mean.js
Baba05206 17dea8e
deleted prep
805fee7
Merge branch 'main' of https://github.com/Baba05206/Module-Data-Groups
Baba05206 d71a842
Refactor reading list functionality and update styles for read/unread…
Baba05206 2bbaaf3
Remove unnecessary comment in readingList function
Baba05206 e813e11
handled null or undefined book variable. returns helpful message
Baba05206 038c26a
improved the look and feel of the reading-list page. modified contras…
Baba05206 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
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,59 @@ | ||
| 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", | ||
| }, | ||
| ]; | ||
|
|
||
| function readingList() { | ||
| const orderedList = document.querySelector("#reading-list"); | ||
|
|
||
| // Base condition: check if books array is empty | ||
| if (!books || books.length === 0) { | ||
| const emptyMessage = document.createElement("li"); | ||
| emptyMessage.className = "empty-message"; | ||
| emptyMessage.textContent = | ||
| "No books in your reading list yet. Add some books to get started!"; | ||
| orderedList.appendChild(emptyMessage); | ||
| return; | ||
| } | ||
|
|
||
| for (const book of books) { | ||
| const listItem = document.createElement("li"); | ||
|
|
||
| const bookImage = document.createElement("img"); | ||
| bookImage.src = book.bookCoverImage; | ||
| bookImage.alt = `Cover of ${book.title}`; | ||
|
|
||
| const bookInfo = document.createElement("p"); | ||
| bookInfo.textContent = `${book.title} by ${book.author}`; | ||
|
|
||
| listItem.append(bookImage, bookInfo); | ||
|
|
||
| if (book.alreadyRead) { | ||
| listItem.classList.add("read"); | ||
| } else { | ||
| listItem.classList.add("notRead"); | ||
| } | ||
|
|
||
| orderedList.appendChild(listItem); | ||
| } | ||
| } | ||
|
|
||
| readingList(); |
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.
Make a base condition, if book is empty, so your application does not crash.
Uh oh!
There was an error while loading. Please reload this page.
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.
Many thanks @JaypeeLan. I have now updated the script to check if the books variable is null or undefined, or if the array is empty. Will return message telling the user their reading list is empty
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.
@Baba05206 Please go through the conversations, I made a comment regarding the design.