Skip to content

Commit

Permalink
Upgrade tests syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Oct 6, 2024
1 parent 0e2c2eb commit 8ca1359
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 116 deletions.
7 changes: 3 additions & 4 deletions test/interactive.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@


describe("Interactive DOM", function() {
describe("Interactive DOM", () => {
var undef
, DOM = require("../interactive")
, document = DOM.document
, { document } = require("../interactive")
, it = describe.it

it("have focus and blur", function (assert) {
it("have focus and blur", assert => {
var el = document.createElement("h1")

assert.type(el.focus, "function")
Expand Down
71 changes: 31 additions & 40 deletions test/net.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@

describe("XMLHttpRequest", function() {
describe("XMLHttpRequest", () => {
var { XMLHttpRequest, protocolHandler } = require("../net.js")

it("throws on invalid protocol", function(assert) {
assert.throws(function() {
it("throws", assert => {
assert.throws(() => {
var xhr = new XMLHttpRequest()
xhr.open("GET", "foo://litejs.com")
xhr.send()
})
assert.end()
})
}, "on invalid protocol")

it("throws on sync request", function(assert) {
assert.throws(function() {
assert.throws(() => {
var xhr = new XMLHttpRequest()
xhr.open("GET", "https://litejs.com", false)
xhr.send()
})
assert.end()
})
}, "on sync request")

it("throws on abort", function(assert) {
assert.throws(function() {
assert.throws(() => {
var xhr = new XMLHttpRequest()
xhr.abort()
})
assert.end()
})
}, "on abort")

it("throws on reuse", function(assert) {
assert.throws(function() {
assert.throws(() => {
var xhr = new XMLHttpRequest()
xhr.open("GET", "https://litejs.com")
xhr.open("GET", "https://litejs.com")
})
}, "on reuse")
assert.end()
})

it("make request", function (assert) {
it("make request", assert => {
assert.setTimeout(5000)
var xhr = new XMLHttpRequest()
xhr.setRequestHeader("Accept", "text/html")
xhr.open("GET", "https://litejs.com")
xhr.responseType = "document"
xhr.onload = function() {
xhr.onload = () => {
var doc = xhr.responseXML
assert.equal(doc.documentElement.nodeName, "HTML")
assert.equal(doc.querySelector("title").textContent, "LiteJS")
Expand All @@ -53,12 +44,12 @@ describe("XMLHttpRequest", function() {
xhr.send()
})

it("calls onreadystatechange", function (assert, mock) {
it("calls onreadystatechange", (assert, mock) => {
assert.setTimeout(5000)
var xhr = new XMLHttpRequest()
xhr.open("GET", "https://litejs.com")
xhr.onreadystatechange = mock.fn()
xhr.onload = function() {
xhr.onload = () => {
assert.equal(xhr.status, 200)
assert.equal(xhr.statusText, "OK")
assert.equal(xhr.responseXML, null)
Expand All @@ -72,11 +63,11 @@ describe("XMLHttpRequest", function() {
xhr.send()
})

it("calls onerror", function (assert, mock) {
it("calls onerror", (assert, mock) => {
assert.setTimeout(5000)
var xhr = new XMLHttpRequest()
xhr.open("GET", "http://0.0.0.0:0")
xhr.onerror = function() {
xhr.onerror = () => {
assert.equal(xhr.status, 0)
assert.equal(xhr.statusText, "")
assert.equal(xhr.responseXML, null)
Expand All @@ -86,41 +77,41 @@ describe("XMLHttpRequest", function() {
xhr.send()
})

describe("Data URLs", function() {
describe("Data URLs", () => {
var table = [
[ "data:,Hello%2C%20World%21", "text/plain", "Hello, World!" ],
[ "data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E", "text/html", "<h1>Hello, World!</h1>" ],
[ "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", "text/plain", "Hello, World!" ],
[ "data:text/javascript,console.log('a')", "text/javascript", "console.log('a')" ]
]

it("handles invalid Data URL", function(assert) {
it("handles invalid Data URL", assert => {
assert.plan(2)
assert.throws(function() {
assert.throws(() => {
var xhr = new XMLHttpRequest()
xhr.open("GET", "data:Hello")
xhr.send()
})
var xhr = new XMLHttpRequest()
xhr.onerror = function(err) {
xhr.onerror = err => {
assert.ok(err)
}
xhr.open("GET", "data:Hello")
xhr.send()
})

it("handles async request {i}", table, function(url, mime, body, assert) {
it("handles async request {i}", table, (url, mime, body, assert) => {
var xhr = new XMLHttpRequest()
xhr.open("GET", url)
assert.equal(xhr.responseText, "")
xhr.onload = function() {
xhr.onload = () => {
assert.equal(xhr.getResponseHeader("content-type"), mime)
assert.equal(xhr.responseText, body)
assert.end()
}
xhr.send()
})
it("handles sync request {i}", table, function(url, mime, body, assert) {
it("handles sync request {i}", table, (url, mime, body, assert) => {
var xhr = new XMLHttpRequest()
xhr.open("GET", url, false)
xhr.send()
Expand All @@ -130,15 +121,15 @@ describe("XMLHttpRequest", function() {
assert.end()
})
})
describe("File URLs", function() {
describe("File URLs", () => {
it("reads {0}", [
[ "./README.md", 200, "text/plain" ],
[ "./bad-file.txt", 404, null ]
], function(file, statusCode, type, assert, mock) {
], (file, statusCode, type, assert, mock) => {
mock.swap(XMLHttpRequest, "base", "file://" + process.cwd() + "/")
var xhr = new XMLHttpRequest()
xhr.open("GET", file)
xhr.onload = function() {
xhr.onload = () => {
assert.equal(xhr.status, statusCode)
assert.equal(xhr.getResponseHeader("content-type"), type)
//assert.equal(xhr.responseText, data[2])
Expand All @@ -149,10 +140,10 @@ describe("XMLHttpRequest", function() {
})

// Blob was added in Node18
it("has a protocolHandler", parseInt(process.versions.node) >= 18 && function(assert) {
protocolHandler.blob = function(url, head, fillBody, done) {
it("has a protocolHandler", parseInt(process.versions.node) >= 18 ? assert => {
protocolHandler.blob = (url, head, fillBody, done) => {
var blob = require("buffer").resolveObjectURL("" + url)
if (blob) blob.text().then(function(text) {
if (blob) blob.text().then(text => {
head(200, "OK", { "content-type": blob.type, "content-length": blob.length })
fillBody(text)
done()
Expand All @@ -162,13 +153,13 @@ describe("XMLHttpRequest", function() {
var url = URL.createObjectURL(new Blob(['<q id="a"><span id="b">hey!</span></q>'], { type: "text/html" }))
var xhr = new XMLHttpRequest()
xhr.open("GET", url)
xhr.onload = function() {
xhr.onload = () => {
assert.equal(xhr.getResponseHeader("content-type"), "text/html")
assert.equal(xhr.responseText, '<q id="a"><span id="b">hey!</span></q>')
assert.equal(xhr.readyState, 4)
assert.end()
}
xhr.send()
})
} : null)
})

107 changes: 45 additions & 62 deletions test/parser.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

describe("parser", function() {
describe("parser", () => {
require("@litejs/cli/snapshot")

var DOM = require("../")
, parser = new DOM.DOMParser()
, serializer = new DOM.XMLSerializer()
var { Document, DOMParser, XMLSerializer } = require("../")
, parser = new DOMParser()
, serializer = new XMLSerializer()
, fs = require("fs")
, path = require("path")
, test = describe.test

test("MDN DOMParser example", function (assert) {
test("MDN DOMParser example", assert => {
var xmlStr = '<q id="a"><span id="b">hey!</span></q>'
, doc = parser.parseFromString(xmlStr, "application/xml")

Expand All @@ -24,64 +24,49 @@ describe("parser", function() {
assert.end()
})

test("parse and stringify", function (assert) {
assert.matchSnapshot("./test/samp1.html", function(str) {
var document = new DOM.Document()
test("parse and stringify", assert => {
assert.matchSnapshot("./test/samp1.html", str => {
var document = new Document()
document.documentElement.outerHTML = str
assert.equal(document.querySelector("span").textContent, "&<>'\"")
assert.equal(document.querySelector("span").title, "&<>'\"")
return document.toString().replace(/--!>/g, "-->").replace(/=""/g, "")
})
assert.end()
})
test("minify samp1.html", function (assert) {
assert.matchSnapshot("./test/samp1.html", function(str) {
var document = new DOM.Document()
document.documentElement.outerHTML = str
var script = document.querySelector("script")
, ul = document.getElementsByClassName("gr")[0]
, result = document.toString(true)
assert.equal(
script.textContent,
"\ndocument.write('<p>ETAGO delimiter</p><script>console.log(\"A\")<\\/script><script>console.log(\"B\")<\\/script>')\n"
)
script.textContent = "document.write('<script>alert(\"<!--\")</script><script>alert(\"--!>\")</script>')"
assert.equal(
script.textContent,
"document.write('<script>alert(\"<\\!--\")<\\/script><script>alert(\"--!>\")<\\/script>')"
)
assert.equal(
ul.style.backgroundImage,
"url(https://1.1.1.1:80/i.png)"
)
return result
})
assert.end()
})
test("parse and reminify samp1.html.snap1", function (assert) {

test("minify {0}", [
[ "samp1.html", "" ],
[ "samp2.html", "text/html" ],
[ "atom.xml", "application/xml" ],
], (file, mime, assert) => assert.matchSnapshot("./test/" + file, str => parser.parseFromString(str, mime).toString(true)).end())

test("parse and reminify samp1.html.snap1", assert => {
var src = fs.readFileSync(path.resolve("./test/samp1.html.snap1"), "utf8").trim()
assert.equal(parser.parseFromString(src).toString(true), src)
assert.end()
})
test("minify samp2.html", function (assert) {
assert.matchSnapshot("./test/samp2.html", function(str) {
var document = new DOM.Document()
document.documentElement.outerHTML = str
return document.toString(true)
})
assert.end()
})
test("minify atom.xml", function (assert) {
assert.matchSnapshot("./test/atom.xml", function(str) {
return parser.parseFromString(str, "application/xml").toString(true)
})
, document = parser.parseFromString(src)
, script = document.querySelector("script")
, ul = document.getElementsByClassName("gr")[0]

assert.equal(document.toString(true), src)
assert.equal(
script.textContent,
"\ndocument.write('<p>ETAGO delimiter</p><script>console.log(\"A\")<\\/script><script>console.log(\"B\")<\\/script>')\n"
)
script.textContent = "document.write('<script>alert(\"<!--\")</script><script>alert(\"--!>\")</script>')"
assert.equal(
script.textContent,
"document.write('<script>alert(\"<\\!--\")<\\/script><script>alert(\"--!>\")<\\/script>')"
)
assert.equal(
ul.style.backgroundImage,
"url(https://1.1.1.1:80/i.png)"
)
assert.end()
})

test("replace document", function (assert) {
test("replace document", assert => {
var document = readDom("./test/samp1.html")

var header = document.getElementById("header")
, header = document.getElementById("header")
, comment = header.firstChild
, comment2 = document.body.firstChild
assert.equal(comment.nodeType, 8)
Expand All @@ -95,7 +80,7 @@ describe("parser", function() {
assert.end()
})

test("document.title", function(assert) {
test("document.title", assert => {
var document = parser.parseFromString("<h1>Hi</h1>")
assert.equal(document.querySelector("title"), null)
assert.equal(document.title, "")
Expand All @@ -105,13 +90,12 @@ describe("parser", function() {
assert.end()
})

test("unclosed style", function(assert) {
test("unclosed style", assert => {
var document = parser.parseFromString("<head><style>a<style>b")
assert.equal(document.querySelectorAll("style").length, 1)
assert.end()
assert.equal(document.querySelectorAll("style").length, 1).end()
})

test("atom", function (assert) {
test("atom", assert => {
var document = readDom("./test/atom.xml")

assert.equal(document.querySelectorAll("feed").length, 1)
Expand All @@ -124,7 +108,7 @@ describe("parser", function() {


/*
test("rdf", function (assert) {
test("rdf", assert => {
var document = readDom("./test/rdf.xml")
//assert.equal(document.querySelectorAll("rdf\\:Description").length, 4)
Expand All @@ -133,9 +117,9 @@ describe("parser", function() {
assert.end()
})
test("rss", function (assert) {
test("rss", assert => {
var src = readDom("./test/rss.xml")
, document = new DOM.Document()
, document = new Document()
document.documentElement.outerHTML = src
Expand All @@ -151,11 +135,10 @@ describe("parser", function() {

function readDom(fileName) {
var src = fs.readFileSync(path.resolve(fileName.split("?")[0]), "utf8").trim()
, document = new DOM.Document()
, document = new Document()
document.documentElement.outerHTML = src

return document
}


})

Loading

0 comments on commit 8ca1359

Please sign in to comment.