diff --git a/src/node/File.mjs b/src/node/File.mjs index 33c0dc73..1d0234ae 100644 --- a/src/node/File.mjs +++ b/src/node/File.mjs @@ -42,6 +42,14 @@ class File { get size() { return this.data.length; } + + /** + * Return lastModified as Date + */ + get lastModifiedDate() { + return new Date(this.lastModified); + } + } export default File; diff --git a/src/node/config/excludedOperations.mjs b/src/node/config/excludedOperations.mjs index 53cdfa95..e4ec6390 100644 --- a/src/node/config/excludedOperations.mjs +++ b/src/node/config/excludedOperations.mjs @@ -13,12 +13,6 @@ export default [ "Label", "Comment", - // Exclude file ops until HTML5 File Object can be mimicked - // "Tar", - // "Untar", - "Unzip", - "Zip", - // esprima doesn't work in .mjs "JavaScriptBeautify", "JavaScriptMinify", diff --git a/tests/node/index.mjs b/tests/node/index.mjs index 112525cc..c78a2d9c 100644 --- a/tests/node/index.mjs +++ b/tests/node/index.mjs @@ -30,6 +30,7 @@ global.ENVIRONMENT_IS_WEB = function() { import TestRegister from "../lib/TestRegister"; import "./tests/nodeApi"; import "./tests/ops"; +import "./tests/File"; const testStatus = { allTestsPassing: true, diff --git a/tests/node/tests/File.mjs b/tests/node/tests/File.mjs new file mode 100644 index 00000000..dc9ddffc --- /dev/null +++ b/tests/node/tests/File.mjs @@ -0,0 +1,20 @@ +import assert from "assert"; +import it from "../assertionHandler"; +import TestRegister from "../../lib/TestRegister"; +import File from "../../../src/node/File"; + +TestRegister.addApiTests([ + it("File: should exist", () => { + assert(File); + }), + + it("File: Should have same properties as DOM File object", () => { + const uint8Array = new Uint8Array(Buffer.from("hello")); + const file = new File([uint8Array], "name.txt"); + assert.equal(file.name, "name.txt"); + assert(typeof file.lastModified, "number"); + assert(file.lastModifiedDate instanceof Date); + assert.equal(file.size, uint8Array.length); + assert.equal(file.type, "text/plain"); + }), +]); diff --git a/tests/node/tests/nodeApi.mjs b/tests/node/tests/nodeApi.mjs index 11361893..2bd07231 100644 --- a/tests/node/tests/nodeApi.mjs +++ b/tests/node/tests/nodeApi.mjs @@ -387,7 +387,7 @@ TestRegister.addApiTests([ it("Operation arguments: should be accessible from operation object if op has array arg", () => { assert.ok(chef.toCharcode.argOptions); - assert.equal(chef.unzip.argOptions, undefined); + assert.deepEqual(chef.unzip.argOptions, {}); }), it("Operation arguments: should have key for each array-based argument in operation", () => { diff --git a/tests/node/tests/ops.mjs b/tests/node/tests/ops.mjs index 9f621cec..8952cfde 100644 --- a/tests/node/tests/ops.mjs +++ b/tests/node/tests/ops.mjs @@ -35,6 +35,9 @@ import { } from "../../../src/node/index"; import chef from "../../../src/node/index"; import TestRegister from "../../lib/TestRegister"; +import File from "../../../src/node/File"; + +global.File = File; TestRegister.addApiTests([ @@ -971,5 +974,48 @@ ExifImageWidth: 57 ExifImageHeight: 57`); }), + it("Tar", () => { + const tarred = chef.tar("some file content", { + filename: "test.txt" + }); + assert.strictEqual(tarred.type, 7); + assert.strictEqual(tarred.value.size, 2048); + assert.strictEqual(tarred.value.data.toString().substr(0, 8), "test.txt"); + }), + + it("Untar", () => { + const tarred = chef.tar("some file content", { + filename: "filename.txt", + }); + const untarred = chef.untar(tarred); + assert.strictEqual(untarred.type, 8); + assert.strictEqual(untarred.value.length, 1); + assert.strictEqual(untarred.value[0].name, "filename.txt"); + assert.strictEqual(untarred.value[0].data.toString(), "some file content"); + }), + + it("Zip", () => { + const zipped = chef.zip("some file content", { + filename: "sample.zip", + comment: "added", + operaringSystem: "Unix", + }); + + assert.strictEqual(zipped.type, 7); + assert.equal(zipped.value.data.toString().indexOf("sample.zip"), 30); + assert.equal(zipped.value.data.toString().indexOf("added"), 122); + }), + + // it("Unzip", () => { + // const zipped = chef.zip("some file content", { + // filename: "zipped.zip", + // comment: "zippy", + // }); + // const unzipped = chef.unzip(zipped); + + // assert.equal(unzipped.type, 8); + // assert.equal(unzipped.value = "zipped.zip"); + // }), + ]);