|
| 1 | +/* ======= TESTS - READ CAREFULLY ===== |
| 2 | +There are some Tests in this file that will help you work out if your code is working. |
| 3 | +*/ |
| 4 | + |
| 5 | +const path = require("path"); |
| 6 | +const { JSDOM } = require("jsdom"); |
| 7 | + |
| 8 | +let page = null; |
| 9 | + |
| 10 | +beforeEach(async () => { |
| 11 | + page = await JSDOM.fromFile(path.join(__dirname, "index.html"), { |
| 12 | + resources: "usable", |
| 13 | + runScripts: "dangerously", |
| 14 | + }); |
| 15 | + |
| 16 | + jest.useFakeTimers(); |
| 17 | + |
| 18 | + // do this so students can use element.innerText which jsdom does not implement |
| 19 | + Object.defineProperty(page.window.HTMLElement.prototype, "innerText", { |
| 20 | + get() { |
| 21 | + return this.textContent; |
| 22 | + }, |
| 23 | + set(value) { |
| 24 | + this.textContent = value; |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + return new Promise((res) => { |
| 29 | + page.window.document.addEventListener("load", res); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +afterEach(() => { |
| 34 | + jest.useRealTimers(); |
| 35 | + page = null; |
| 36 | +}); |
| 37 | + |
| 38 | +test("should set heading when button is clicked", () => { |
| 39 | + const heading = page.window.document.querySelector("#timeRemaining"); |
| 40 | + const input = page.window.document.querySelector("#alarmSet"); |
| 41 | + const button = page.window.document.querySelector("#set"); |
| 42 | + |
| 43 | + input.value = "19"; |
| 44 | + button.click(); |
| 45 | + |
| 46 | + expect(heading).toHaveTextContent("Time Remaining: 00:19"); |
| 47 | +}); |
| 48 | + |
| 49 | +test("should split values over 60 seconds into minutes and seconds", () => { |
| 50 | + const heading = page.window.document.querySelector("#timeRemaining"); |
| 51 | + const input = page.window.document.querySelector("#alarmSet"); |
| 52 | + const button = page.window.document.querySelector("#set"); |
| 53 | + |
| 54 | + input.value = "119"; |
| 55 | + button.click(); |
| 56 | + |
| 57 | + expect(heading).toHaveTextContent("Time Remaining: 01:59"); |
| 58 | +}); |
| 59 | + |
| 60 | +test("should update the heading while counting down", () => { |
| 61 | + const heading = page.window.document.querySelector("#timeRemaining"); |
| 62 | + const input = page.window.document.querySelector("#alarmSet"); |
| 63 | + const button = page.window.document.querySelector("#set"); |
| 64 | + |
| 65 | + input.value = "19"; |
| 66 | + button.click(); |
| 67 | + |
| 68 | + for (let i = 18; i > 0; i--) { |
| 69 | + jest.runOnlyPendingTimers(); |
| 70 | + const seconds = `${i}`.padStart(2, "0"); |
| 71 | + expect(heading).toHaveTextContent(`Time Remaining: 00:${seconds}`); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +test("should count down every 1000 ms", () => { |
| 76 | + const input = page.window.document.querySelector("#alarmSet"); |
| 77 | + const button = page.window.document.querySelector("#set"); |
| 78 | + |
| 79 | + const mockTimer = jest.fn(); |
| 80 | + page.window.setTimeout = mockTimer; |
| 81 | + page.window.setInterval = mockTimer; |
| 82 | + |
| 83 | + input.value = "19"; |
| 84 | + button.click(); |
| 85 | + |
| 86 | + expect(mockTimer).toHaveBeenCalledTimes(1); |
| 87 | + expect(mockTimer).toHaveBeenLastCalledWith(expect.any(Function), 1000); |
| 88 | +}); |
| 89 | + |
| 90 | +test("should play audio when the timer reaches zero", () => { |
| 91 | + const input = page.window.document.querySelector("#alarmSet"); |
| 92 | + const button = page.window.document.querySelector("#set"); |
| 93 | + const mockPlayAlarm = jest.fn(); |
| 94 | + |
| 95 | + page.window.playAlarm = mockPlayAlarm; |
| 96 | + input.value = "10"; |
| 97 | + button.click(); |
| 98 | + |
| 99 | + expect(mockPlayAlarm).toHaveBeenCalledTimes(0); |
| 100 | + |
| 101 | + jest.runAllTimers(); |
| 102 | + |
| 103 | + expect(mockPlayAlarm).toHaveBeenCalledTimes(1); |
| 104 | +}); |
0 commit comments