| 1 | import test from "node:test" ; |
| 2 | import assert from "node:assert/strict" ; |
| 3 | import fs from "node:fs" ; |
| 4 | import vm from "node:vm" ; |
| 5 | |
| 6 | const source = fs.readFileSync("tinyclient/components.js" , "utf8" ); |
| 7 | const classSource = source.slice(source.indexOf(" class ViewArtifact" ), source.indexOf(" // ViewForm adds" )); |
| 8 | |
| 9 | class HTMLElement {} |
| 10 | const ViewArtifact = vm.runInNewContext(`${classSource}\nViewArtifact` , {HTMLElement}); |
| 11 | |
| 12 | const setup = ({complete = false, naturalWidth = 100} = {}) => { |
| 13 | const listeners = {}; |
| 14 | const image = {complete, naturalWidth, hidden: false, addEventListener: (name, fn) => { listeners[name] = fn; }}; |
| 15 | const details = {open: false}; |
| 16 | const artifact = new ViewArtifact(); |
| 17 | artifact.querySelector = selector => selector === "img" ? image : details; |
| 18 | artifact.connectedCallback(); |
| 19 | return {image, details, fail: () => listeners.error()}; |
| 20 | }; |
| 21 | |
| 22 | test("view artifact keeps a loaded image and source disclosure collapsed" , () => { |
| 23 | const {image, details} = setup({complete: true}); |
| 24 | assert.equal(image.hidden, false); |
| 25 | assert.equal(details.open, false); |
| 26 | }); |
| 27 | |
| 28 | test("view artifact opens source and hides a failed image" , () => { |
| 29 | const {image, details, fail} = setup(); |
| 30 | fail(); |
| 31 | assert.equal(image.hidden, true); |
| 32 | assert.equal(details.open, true); |
| 33 | }); |
| 34 | |
| 35 | test("view artifact handles an already failed cached image" , () => { |
| 36 | const {image, details} = setup({complete: true, naturalWidth: 0}); |
| 37 | assert.equal(image.hidden, true); |
| 38 | assert.equal(details.open, true); |
| 39 | }); |