|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const vm = require("vm"); |
| 4 | + |
| 5 | +const script = fs.readFileSync(path.join(__dirname, "script.js"), "utf8"); |
| 6 | + |
| 7 | +function createElement(tagName) { |
| 8 | + return { |
| 9 | + tagName, |
| 10 | + cells: [], |
| 11 | + children: [], |
| 12 | + listeners: {}, |
| 13 | + addEventListener(event, handler) { |
| 14 | + this.listeners[event] = handler; |
| 15 | + }, |
| 16 | + appendChild(child) { |
| 17 | + this.children.push(child); |
| 18 | + return child; |
| 19 | + }, |
| 20 | + click() { |
| 21 | + if (this.listeners.click) { |
| 22 | + this.listeners.click(); |
| 23 | + } |
| 24 | + }, |
| 25 | + insertCell(index) { |
| 26 | + const cell = createElement("td"); |
| 27 | + this.cells.splice(index, 0, cell); |
| 28 | + return cell; |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function createApp() { |
| 34 | + const header = createElement("tr"); |
| 35 | + const placeholder = createElement("tr"); |
| 36 | + const table = { |
| 37 | + rows: [header, placeholder], |
| 38 | + deleteRow(index) { |
| 39 | + this.rows.splice(index, 1); |
| 40 | + }, |
| 41 | + insertRow(index) { |
| 42 | + const row = createElement("tr"); |
| 43 | + this.rows.splice(index, 0, row); |
| 44 | + return row; |
| 45 | + }, |
| 46 | + }; |
| 47 | + const inputs = { |
| 48 | + title: { value: "" }, |
| 49 | + author: { value: "" }, |
| 50 | + pages: { value: "" }, |
| 51 | + check: { checked: false }, |
| 52 | + }; |
| 53 | + const alerts = []; |
| 54 | + const windowListeners = {}; |
| 55 | + const document = { |
| 56 | + createElement, |
| 57 | + getElementById(id) { |
| 58 | + return id === "display" ? table : inputs[id]; |
| 59 | + }, |
| 60 | + }; |
| 61 | + const window = { |
| 62 | + addEventListener(event, handler) { |
| 63 | + windowListeners[event] = handler; |
| 64 | + }, |
| 65 | + }; |
| 66 | + const context = vm.createContext({ |
| 67 | + alert(message) { |
| 68 | + alerts.push(message); |
| 69 | + }, |
| 70 | + document, |
| 71 | + window, |
| 72 | + }); |
| 73 | + |
| 74 | + vm.runInContext(script, context); |
| 75 | + windowListeners.load(); |
| 76 | + |
| 77 | + return { alerts, context, inputs, table }; |
| 78 | +} |
| 79 | + |
| 80 | +function findRow(table, title) { |
| 81 | + return table.rows.find( |
| 82 | + (row) => row.cells[0] && row.cells[0].textContent === title |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +test("renders the two sample books on page load", () => { |
| 87 | + const { table } = createApp(); |
| 88 | + |
| 89 | + expect(table.rows).toHaveLength(3); |
| 90 | + expect(table.rows[1].cells[0].textContent).toBe("The Old Man and the Sea"); |
| 91 | + expect(table.rows[2].cells[0].textContent).toBe("Robison Crusoe"); |
| 92 | +}); |
| 93 | + |
| 94 | +test.each([ |
| 95 | + ["title", ""], |
| 96 | + ["author", ""], |
| 97 | + ["pages", ""], |
| 98 | +])("rejects a book with a missing %s", (field, value) => { |
| 99 | + const { alerts, context, inputs, table } = createApp(); |
| 100 | + inputs.title.value = "Dune"; |
| 101 | + inputs.author.value = "Frank Herbert"; |
| 102 | + inputs.pages.value = "412"; |
| 103 | + inputs[field].value = value; |
| 104 | + |
| 105 | + context.submit(); |
| 106 | + |
| 107 | + expect(alerts).toHaveLength(1); |
| 108 | + expect(table.rows).toHaveLength(3); |
| 109 | +}); |
| 110 | + |
| 111 | +test("stores the entered author and read status", () => { |
| 112 | + const { context, inputs, table } = createApp(); |
| 113 | + inputs.title.value = "Dune"; |
| 114 | + inputs.author.value = "Frank Herbert"; |
| 115 | + inputs.pages.value = "412"; |
| 116 | + inputs.check.checked = true; |
| 117 | + |
| 118 | + context.submit(); |
| 119 | + |
| 120 | + const row = findRow(table, "Dune"); |
| 121 | + expect(row.cells[1].textContent).toBe("Frank Herbert"); |
| 122 | + expect(row.cells[2].textContent).toBe("412"); |
| 123 | + expect(row.cells[3].children[0].textContent).toBe("Yes"); |
| 124 | +}); |
| 125 | + |
| 126 | +test("toggles the displayed read status", () => { |
| 127 | + const { table } = createApp(); |
| 128 | + const row = findRow(table, "Robison Crusoe"); |
| 129 | + expect(row.cells[3].children[0].textContent).toBe("Yes"); |
| 130 | + |
| 131 | + row.cells[3].children[0].click(); |
| 132 | + |
| 133 | + expect( |
| 134 | + findRow(table, "Robison Crusoe").cells[3].children[0].textContent |
| 135 | + ).toBe("No"); |
| 136 | +}); |
| 137 | + |
| 138 | +test("renders user-provided book details as text", () => { |
| 139 | + const { context, inputs, table } = createApp(); |
| 140 | + const title = "<img src=x onerror=alert(1)>"; |
| 141 | + inputs.title.value = title; |
| 142 | + inputs.author.value = "<script>alert(1)</script>"; |
| 143 | + inputs.pages.value = "12"; |
| 144 | + |
| 145 | + context.submit(); |
| 146 | + |
| 147 | + const row = findRow(table, title); |
| 148 | + expect(row.cells[0].textContent).toBe(title); |
| 149 | + expect(row.cells[1].textContent).toBe("<script>alert(1)</script>"); |
| 150 | +}); |
| 151 | + |
| 152 | +test("deletes only the selected book", () => { |
| 153 | + const { table } = createApp(); |
| 154 | + const row = findRow(table, "The Old Man and the Sea"); |
| 155 | + |
| 156 | + row.cells[4].children[0].click(); |
| 157 | + |
| 158 | + expect(table.rows).toHaveLength(2); |
| 159 | + expect(table.rows[1].cells[0].textContent).toBe("Robison Crusoe"); |
| 160 | +}); |
0 commit comments