Skip to content

Commit 970f48f

Browse files
committed
Add more tests
Closes #3.
1 parent 9ec9439 commit 970f48f

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

test/api.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"use strict";
2+
const { describe, it } = require("node:test");
3+
const assert = require("node:assert");
4+
const parseDataURL = require("..");
5+
/*
6+
eslint
7+
array-bracket-newline: ["error", "consistent"]
8+
array-element-newline: "off"
9+
*/
10+
11+
describe("Smoke tests via README examples", () => {
12+
it("should parse no-type as expected", () => {
13+
const textExample = parseDataURL("data:,Hello%2C%20World!");
14+
assert.equal(textExample.mimeType.toString(), "text/plain;charset=US-ASCII");
15+
assert.equal(textExample.body.constructor, Uint8Array);
16+
assert.deepEqual(textExample.body, new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]));
17+
});
18+
19+
it("should parse text/html as expected", () => {
20+
const htmlExample = parseDataURL("data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E");
21+
assert.equal(htmlExample.mimeType.toString(), "text/html");
22+
assert.equal(htmlExample.body.constructor, Uint8Array);
23+
assert.deepEqual(htmlExample.body, new Uint8Array([
24+
60, 104, 49, 62, 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33, 60, 47, 104, 49, 62
25+
]));
26+
});
27+
28+
it("should parse img/png base64 as expected", () => {
29+
const pngExample = parseDataURL("data:image/png;base64,iVBORw0KGgoAAA" +
30+
"ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
31+
"//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
32+
"5ErkJggg==");
33+
assert.equal(pngExample.mimeType.toString(), "image/png");
34+
assert.equal(pngExample.body.constructor, Uint8Array);
35+
assert.deepEqual(pngExample.body, new Uint8Array([
36+
137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13,
37+
73, 72, 68, 82, 0, 0, 0, 5, 0, 0, 0, 5,
38+
8, 6, 0, 0, 0, 141, 111, 38, 229, 0, 0, 0,
39+
28, 73, 68, 65, 84, 8, 215, 99, 248, 255, 255, 63,
40+
195, 127, 6, 32, 5, 195, 32, 18, 132, 208, 49, 241,
41+
130, 88, 205, 4, 0, 14, 245, 53, 203, 209, 142, 14,
42+
31, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96,
43+
130
44+
]));
45+
});
46+
});
47+
48+
describe("Additional coverage", () => {
49+
it("should return null for non-data: URLs", () => {
50+
assert.equal(parseDataURL("https://example.com/"), null);
51+
});
52+
});

0 commit comments

Comments
 (0)