Skip to content

CYF Glasgow | Iakub Dubachev | Module-Data-Groups | Quote Generator | WEEK 3 #245

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<script defer src="quotes.js"></script>
</head>
<body>
<h1>hello there</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
</body>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quote Generator App</title>
<script defer src="quotes.js"></script>
</head>
<body>
<h1>Quote Generator</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New Quote</button>
<label>
<input type="checkbox" id="auto-play"> Auto-Play
</label>
<p id="auto-play-status">Auto-Play: OFF</p>
</body>
</html>

28 changes: 28 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
// Function to display a random quote
function displayRandomQuote() {
const randomQuote = pickFromArray(quotes);
document.getElementById('quote').innerText = randomQuote.quote;
document.getElementById('author').innerText = `— ${randomQuote.author}`;
}

// Event listener for the "New Quote" button
document.getElementById('new-quote').addEventListener('click', displayRandomQuote);

// Auto-play functionality
let autoPlayInterval = null;
const autoPlayCheckbox = document.getElementById('auto-play');
const autoPlayStatus = document.getElementById('auto-play-status');

autoPlayCheckbox.addEventListener('change', function () {
if (this.checked) {
autoPlayStatus.innerText = 'Auto-Play: ON';
autoPlayInterval = setInterval(displayRandomQuote, 5000); // 5 seconds
} else {
autoPlayStatus.innerText = 'Auto-Play: OFF';
clearInterval(autoPlayInterval);
}
});

// Display an initial random quote when the page loads
displayRandomQuote();

// DO NOT EDIT BELOW HERE

// pickFromArray is a function which will return one item, at
Expand Down
58 changes: 30 additions & 28 deletions Sprint-3/quote-generator/quotes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@ const { default: userEvent } = require("@testing-library/user-event");
let page = null;

beforeEach(async () => {
page = await JSDOM.fromFile(path.join(__dirname, "index.html"), {
resources: "usable",
runScripts: "dangerously",
});
try {
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;
},
});
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]
jest
.spyOn(page.window.Math, "random")
.mockReturnValueOnce(0.02)
.mockReturnValueOnce(0.25)
.mockReturnValueOnce(0.79);

return new Promise((res) => {
page.window.document.addEventListener("load", res);
});
return new Promise((res) => {
page.window.document.addEventListener("load", res);
});
} catch (error) {
console.error("Error loading JSDOM:", error);
}
});

afterEach(() => {
page = null;
jest.restoreAllMocks();
jest.clearAllMocks();
});

describe("Quote generator", () => {
Expand All @@ -50,25 +54,23 @@ describe("Quote generator", () => {
);
expect(authorP).toHaveTextContent("Albert Einstein");
});
test("can change quote to another random quote", () => {

test("can change quote to another random quote", async () => {
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).not.toBeNull();
expect(newQuoteBtn).toHaveTextContent("New quote");

userEvent.click(newQuoteBtn);
await userEvent.click(newQuoteBtn);

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");

userEvent.click(newQuoteBtn);
await userEvent.click(newQuoteBtn);

expect(quoteP).toHaveTextContent(
"I have learned over the years that when one's mind is made up, this diminishes fear."
Expand Down
36 changes: 36 additions & 0 deletions Sprint-3/quote-generator/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
/** Write your CSS in here **/
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}

h1 {
color: #444;
}

#quote {
font-size: 1.5em;
margin: 20px;
}

#author {
font-size: 1em;
color: #888;
}

button {
margin-top: 20px;
padding: 10px 20px;
font-size: 1em;
}

label {
display: block;
margin-top: 20px;
}

#auto-play-status {
margin-top: 10px;
font-style: italic;
}