From c0533dd3d6c65ba34e8d63e59c4e92b0ddfaee9b Mon Sep 17 00:00:00 2001 From: Josh Duff Date: Thu, 19 Apr 2018 17:41:37 -0500 Subject: [PATCH] Adding tests for browser and node JSZip's browser/ESM support is tenuous, but it appears to support browserify. --- .gitignore | 1 + package.json | 18 +++++++++++++++--- rollup.config.test.js | 16 ++++++++++++++++ test/helper.js | 18 ++++++++++++++++++ test/test.browser.js | 13 +++++++++++++ test/test.everywhere.js | 20 ++++++++++++++++++++ test/test.node.js | 13 +++++++++++++ 7 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 rollup.config.test.js create mode 100644 test/helper.js create mode 100644 test/test.browser.js create mode 100644 test/test.everywhere.js create mode 100644 test/test.node.js diff --git a/.gitignore b/.gitignore index e350582..86af879 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /package-lock.json /shrinkwrap.yaml /yarn.lock +test/jszip.js diff --git a/package.json b/package.json index 99eb47a..fe81f70 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,21 @@ }, "homepage": "https://cndtr.io/do-not-zip/", "devDependencies": { - "rollup": "*" + "browserify": "16.2.0", + "jszip": "3.1.5", + "rollup": "*", + "rollup-plugin-commonjs": "9.1.0", + "rollup-plugin-node-resolve": "3.3.0", + "tap-colorize": "1.2.0", + "tape-run": "4.0.0", + "zora": "2.0.1" }, "scripts": { - "build": "rollup -c" - } + "build": "rollup -c", + "build:jszip": "browserify -s jszip node_modules/jszip/lib/index.js > test/jszip.js", + "test:browser": "npm run build:jszip && rollup -c rollup.config.test.js | tape-run | tap-colorize", + "test:node": "node test/test.node.js | tap-colorize", + "test": "npm run build && npm run test:node && npm run test:browser" + }, + "dependencies": {} } diff --git a/rollup.config.test.js b/rollup.config.test.js new file mode 100644 index 0000000..a298b0a --- /dev/null +++ b/rollup.config.test.js @@ -0,0 +1,16 @@ +import commonjs from 'rollup-plugin-commonjs' +import nodeResolve from 'rollup-plugin-node-resolve' + +export default { + input: `./test/test.browser.js`, + output: [ + { + format: `iife`, + name: `tests`, + }, + ], + plugins: [ + nodeResolve(), + commonjs(), + ], +} diff --git a/test/helper.js b/test/helper.js new file mode 100644 index 0000000..77236e9 --- /dev/null +++ b/test/helper.js @@ -0,0 +1,18 @@ +const JSZip = require(`./jszip.js`) + +module.exports = { + loadJzip(data) { + return new JSZip().loadAsync(data) + }, + jzipToEntries(jzip) { + const ary = [] + jzip.forEach((path, file) => ary.push({ path, file })) + return ary + }, + entriesToObject(entries) { + return entries.reduce((acc, { path, file }) => { + acc[path] = file + return acc + }, Object.create(null)) + }, +} diff --git a/test/test.browser.js b/test/test.browser.js new file mode 100644 index 0000000..acd9d36 --- /dev/null +++ b/test/test.browser.js @@ -0,0 +1,13 @@ +const test = require(`zora`) +const doNotZip = require(`../`) + +test(`Creates a Blob in the browser`, t => { + const outputBlob = doNotZip([ + { path: `path/to/file1.txt`, data: `Hello` }, + { path: `another/file2.txt`, data: `World` }, + ]) + + t.ok(outputBlob instanceof Blob, `output is a Blob`) +}) + +require(`./test.everywhere.js`) diff --git a/test/test.everywhere.js b/test/test.everywhere.js new file mode 100644 index 0000000..7f6f693 --- /dev/null +++ b/test/test.everywhere.js @@ -0,0 +1,20 @@ +const test = require(`zora`) +const doNotZip = require(`../`) +const { loadJzip, jzipToEntries, entriesToObject } = require(`./helper.js`) + +test(`Creates a zip file that jszip can read`, async t => { + const outputBlob = doNotZip([ + { path: `path/to/file1.txt`, data: `Hello` }, + { path: `another/file2.txt`, data: `World` }, + ]) + const entries = jzipToEntries(await loadJzip(outputBlob)) + + const expectedPaths = [ `path/to/file1.txt`, `another/file2.txt` ] + + t.equal(entries.length, expectedPaths.length) + + const jzipMap = entriesToObject(entries) + + expectedPaths.forEach(expectedPath => expectedPath in jzipMap) +}) + diff --git a/test/test.node.js b/test/test.node.js new file mode 100644 index 0000000..d416e0e --- /dev/null +++ b/test/test.node.js @@ -0,0 +1,13 @@ +const test = require(`zora`) +const doNotZip = require(`../`) + +test(`Creates a Buffer in node`, t => { + const outputBlob = doNotZip([ + { path: `path/to/file1.txt`, data: `Hello` }, + { path: `another/file2.txt`, data: `World` }, + ]) + + t.ok(outputBlob instanceof Buffer, `output is a Buffer`) +}) + +require(`./test.everywhere.js`)