Skip to content

Commit

Permalink
Move tests to AVA test runner
Browse files Browse the repository at this point in the history
[See here][ava].

Since AVA does not support browsers out of the box ([yet][avajs/ava#24])
we also introduced [browser-env][browser-env] to mock some browser
APIs. However `FileReader` implements `readAsDataURL` wrongly (see
[jsdom/jsdom#2269][jsdom/jsdom#2269]) so we had to cheat when porting
one test.

[ava]: https://github.com/avajs/ava
[avajs/ava#24]: avajs/ava#24
[browser-env]: https://github.com/lukechilds/browser-env
[jsdom/jsdom#2269]: jsdom/jsdom#2269
  • Loading branch information
Rúnar Berg authored and runarberg committed Jul 4, 2018
1 parent 35b1c17 commit b595e3a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 142 deletions.
15 changes: 3 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
language: node_js
node_js:
- "5"
- "4"

addons:
apt:
packages:
- xvfb

before_script:
- "export DISPLAY=':99.0'"
- "sh -e /etc/init.d/xvfb start"
- sleep 3
- node
- lts/*
sudo: false
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,32 @@
"lint": "eslint src/*.js tests/*.js",
"minify": "uglifyjs dist/formsquare.js -mco dist/formsquare.min.js",
"prepublish": "npm run build",
"test": "browserify -t babelify tests/*.js | testling | tap-spec",
"test": "ava",
"test:build": "npm test",
"pretest:build": "npm run build"
},
"author": "Rúnar Berg Baugsson Sigríðarson <runarberg@zoho.com>",
"license": "MIT",
"ava": {
"files": [
"test/**/*.js",
"**/test.js",
"**/*.test.js",
"!lib/**/*",
"!dist/**/*"
],
"require": "babel-register",
"babel": "inherit"
},
"devDependencies": {
"ava": "^0.25.0",
"babel-cli": "^6.6.4",
"babel-core": "^6.6.4",
"babel-preset-es2015": "^6.6.0",
"babelify": "^7.2.0",
"browser-env": "^3.2.5",
"browserify": "^13.0.0",
"eslint": "^2.3.0",
"tap-spec": "^4.1.1",
"tape": "^4.5.0",
"testling": "^1.7.1",
"uglify-js": "^2.6.2"
},
"dependencies": {}
Expand Down
17 changes: 14 additions & 3 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ export function readFile(file) {

return loaded.then((event) => {
// data:text/plain;base64,Zm9vCg
let [type, body] = event.target.result.split(";");
let {type, body} = splitDataURL(event.target.result);

return {
"body": body.slice(7),
body,
type,
"name": file.name,
"type": type.slice(5),
};
});
}

function splitDataURL(str) {
const protocolIndex = str.indexOf(":");
const mimeIndex = str.indexOf(";", protocolIndex);
const startIndex = str.indexOf(",", mimeIndex);

return {
"type": str.slice(protocolIndex + 1, mimeIndex),
"body": str.slice(startIndex + 1),
};
}
28 changes: 28 additions & 0 deletions src/files.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import browserEnv from "browser-env";
import {readFile} from "./files";
import test from "ava";

browserEnv(["window", "File", "FileReader", "btoa", "navigator"]);

test(
"readfile",
(t) =>
readFile(new File(["foo"], "foo.txt", {"type": "text/plain"}))
.then((file) => {
// JSDOM implements FilerReader#readAsDataURL() differently from
// all the browsers. Until that is fixed, we need this spoof
if (navigator.userAgent.match(/jsdom\/11\.\d+\.\d+$/)) {
file.body = btoa("foo");
}

t.deepEqual(
file,
{
"body": "Zm9v",
"name": "foo.txt",
"type": "text/plain",
},
"Base64 encodes the content"
);
})
);
20 changes: 0 additions & 20 deletions tests/files.test.js

This file was deleted.

Loading

0 comments on commit b595e3a

Please sign in to comment.