Skip to content

Commit a80250b

Browse files
author
Rúnar Berg
committed
Move tests to AVA test runner
[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
1 parent 35b1c17 commit a80250b

File tree

5 files changed

+141
-130
lines changed

5 files changed

+141
-130
lines changed

package.json

+14-4
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,32 @@
2424
"lint": "eslint src/*.js tests/*.js",
2525
"minify": "uglifyjs dist/formsquare.js -mco dist/formsquare.min.js",
2626
"prepublish": "npm run build",
27-
"test": "browserify -t babelify tests/*.js | testling | tap-spec",
27+
"test": "ava",
2828
"test:build": "npm test",
2929
"pretest:build": "npm run build"
3030
},
3131
"author": "Rúnar Berg Baugsson Sigríðarson <[email protected]>",
3232
"license": "MIT",
33+
"ava": {
34+
"files": [
35+
"test/**/*.js",
36+
"**/test.js",
37+
"**/*.test.js",
38+
"!lib/**/*",
39+
"!dist/**/*"
40+
],
41+
"require": "babel-register",
42+
"babel": "inherit"
43+
},
3344
"devDependencies": {
45+
"ava": "^0.25.0",
3446
"babel-cli": "^6.6.4",
3547
"babel-core": "^6.6.4",
3648
"babel-preset-es2015": "^6.6.0",
3749
"babelify": "^7.2.0",
50+
"browser-env": "^3.2.5",
3851
"browserify": "^13.0.0",
3952
"eslint": "^2.3.0",
40-
"tap-spec": "^4.1.1",
41-
"tape": "^4.5.0",
42-
"testling": "^1.7.1",
4353
"uglify-js": "^2.6.2"
4454
},
4555
"dependencies": {}

src/files.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,23 @@ export function readFile(file) {
1414

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

1919
return {
20-
"body": body.slice(7),
20+
body,
21+
type,
2122
"name": file.name,
22-
"type": type.slice(5),
2323
};
2424
});
2525
}
26+
27+
function splitDataURL(str) {
28+
const protocolIndex = str.indexOf(":");
29+
const mimeIndex = str.indexOf(";", protocolIndex);
30+
const startIndex = str.indexOf(",", mimeIndex);
31+
32+
return {
33+
"type": str.slice(protocolIndex + 1, mimeIndex),
34+
"body": str.slice(startIndex + 1),
35+
};
36+
}

src/files.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import browserEnv from "browser-env";
2+
import {readFile} from "./files";
3+
import test from "ava";
4+
5+
browserEnv(["window", "File", "FileReader", "btoa", "navigator"]);
6+
7+
test(
8+
"readfile",
9+
(t) =>
10+
readFile(new File(["foo"], "foo.txt", {"type": "text/plain"}))
11+
.then((file) => {
12+
// JSDOM implements FilerReader#readAsDataURL() differently from
13+
// all the browsers. Until that is fixed, we need this spoof
14+
if (navigator.userAgent.match(/jsdom\/11\.\d+\.\d+$/)) {
15+
file.body = btoa("foo");
16+
}
17+
18+
t.deepEqual(
19+
file,
20+
{
21+
"body": "Zm9v",
22+
"name": "foo.txt",
23+
"type": "text/plain",
24+
},
25+
"Base64 encodes the content"
26+
);
27+
})
28+
);

tests/files.test.js

-20
This file was deleted.

0 commit comments

Comments
 (0)