diff --git a/Sprint-3/quote-generator/index.html b/Sprint-3/quote-generator/index.html index 30b434bcf..ba66359c1 100644 --- a/Sprint-3/quote-generator/index.html +++ b/Sprint-3/quote-generator/index.html @@ -3,11 +3,11 @@ - Title here + Quote generator app -

hello there

+

Quote Generator

diff --git a/Sprint-3/quote-generator/quotes-repeat.test.js b/Sprint-3/quote-generator/quotes-repeat.test.js new file mode 100644 index 000000000..a990d5b25 --- /dev/null +++ b/Sprint-3/quote-generator/quotes-repeat.test.js @@ -0,0 +1,44 @@ +const path = require("path"); +const { JSDOM } = require("jsdom"); + +let page = null; + +afterEach(() => { + if (page) { + page.window.close(); + } + page = null; + jest.restoreAllMocks(); +}); + +test("does not repeat the displayed quote", async () => { + page = await JSDOM.fromFile(path.join(__dirname, "index.html"), { + resources: "usable", + runScripts: "dangerously", + beforeParse(window) { + jest.spyOn(window.Math, "random").mockReturnValue(0.02); + }, + }); + + if (page.window.document.readyState !== "complete") { + await new Promise((resolve) => { + page.window.addEventListener("load", resolve, { once: true }); + }); + } + + const quote = page.window.document.querySelector("#quote"); + const author = page.window.document.querySelector("#author"); + const initialQuote = + "Strive not to be a success, but rather to be of value."; + const initialAuthor = "Albert Einstein"; + + expect(quote).toHaveTextContent(initialQuote); + expect(author).toHaveTextContent(initialAuthor); + + page.window.document.querySelector("#new-quote").click(); + + expect(quote).toHaveTextContent( + "Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference." + ); + expect(author).toHaveTextContent("Robert Frost"); +}); diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index 4a4d04b72..2a99179cb 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -490,4 +490,22 @@ const quotes = [ }, ]; -// call pickFromArray with the quotes array to check you get a random quote +let currentQuote; + +function displayQuote() { + let selectedQuote = pickFromArray(quotes); + if (selectedQuote === currentQuote) { + const currentIndex = quotes.indexOf(currentQuote); + selectedQuote = quotes[(currentIndex + 1) % quotes.length]; + } + currentQuote = selectedQuote; + + document.querySelector("#quote").textContent = selectedQuote.quote; + document.querySelector("#author").textContent = selectedQuote.author; +} + +document + .querySelector("#new-quote") + .addEventListener("click", displayQuote); + +displayQuote();