Skip to content

Commit 1a0c6df

Browse files
committed
Use chrome and a local webserver for the unit tests.
1 parent 857038d commit 1a0c6df

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222
"license": "MIT",
2323
"main": "./js/canvas-to-blob.js",
2424
"devDependencies": {
25+
"concurrently": "^3.5.0",
2526
"eslint": "^4.5.0",
2627
"eslint-config-standard": "^10.2.1",
2728
"eslint-plugin-import": "^2.7.0",
2829
"eslint-plugin-node": "^5.1.1",
2930
"eslint-plugin-promise": "^3.5.0",
3031
"eslint-plugin-standard": "^3.0.1",
31-
"mocha-phantomjs-core": "1.3.1",
32-
"phantomjs-prebuilt": "2.1.13",
32+
"get-port-cli": "^1.1.0",
33+
"http-server": "^0.10.0",
34+
"mocha-chrome": "^0.1.1",
3335
"uglify-js": "^3.0.28"
3436
},
3537
"scripts": {
3638
"lint": "eslint .",
37-
"unit": "phantomjs node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js test/index.html",
39+
"serve": "http-server . -a 127.0.0.1 -p $PORT -s",
40+
"mocha": "mocha-chrome http://127.0.0.1:$PORT/test",
41+
"unit": "PORT=$(get-port) concurrently -k 'npm run serve' 'npm run mocha'",
3842
"test": "npm run lint && npm run unit",
3943
"build": "cd js && uglifyjs canvas-to-blob.js -c -m -o canvas-to-blob.min.js --source-map canvas-to-blob.min.js.map",
4044
"preversion": "npm test",

test/test.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,30 @@
99
* https://opensource.org/licenses/MIT
1010
*/
1111

12-
/* global describe, it, Blob */
12+
/* global describe, it, Blob, chai, dataURLtoBlob */
1313

14-
;(function (expect) {
14+
;(function () {
1515
'use strict'
1616

17+
var expect = chai.expect
18+
var canvasToBlob = function (canvas, callback, type, quality) {
19+
setTimeout(function () {
20+
callback(dataURLtoBlob(canvas.toDataURL(type, quality)))
21+
})
22+
}
23+
1724
// 80x60px GIF image (color black, base64 data):
1825
var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
1926
'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
2027
'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7'
2128
var imageUrl = 'data:image/gif;base64,' + b64Data
22-
var blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl)
29+
var blob = dataURLtoBlob(imageUrl)
2330

2431
describe('canvas.toBlob', function () {
2532
it('Converts a canvas element to a blob and passes it to the callback function', function (done) {
2633
window.loadImage(blob, function (canvas) {
27-
canvas.toBlob(
34+
canvasToBlob(
35+
canvas,
2836
function (newBlob) {
2937
expect(newBlob).to.be.a.instanceOf(Blob)
3038
done()
@@ -35,7 +43,8 @@
3543

3644
it('Converts a canvas element to a PNG blob', function (done) {
3745
window.loadImage(blob, function (canvas) {
38-
canvas.toBlob(
46+
canvasToBlob(
47+
canvas,
3948
function (newBlob) {
4049
expect(newBlob.type).to.equal('image/png')
4150
done()
@@ -47,7 +56,8 @@
4756

4857
it('Converts a canvas element to a JPG blob', function (done) {
4958
window.loadImage(blob, function (canvas) {
50-
canvas.toBlob(
59+
canvasToBlob(
60+
canvas,
5161
function (newBlob) {
5262
expect(newBlob.type).to.equal('image/jpeg')
5363
done()
@@ -59,7 +69,8 @@
5969

6070
it('Keeps the aspect ratio of the canvas image', function (done) {
6171
window.loadImage(blob, function (canvas) {
62-
canvas.toBlob(
72+
canvasToBlob(
73+
canvas,
6374
function (newBlob) {
6475
window.loadImage(newBlob, function (img) {
6576
expect(img.width).to.equal(canvas.width)
@@ -73,7 +84,8 @@
7384

7485
it('Keeps the image data of the canvas image', function (done) {
7586
window.loadImage(blob, function (canvas) {
76-
canvas.toBlob(
87+
canvasToBlob(
88+
canvas,
7789
function (newBlob) {
7890
window.loadImage(newBlob, function (newCanvas) {
7991
var canvasData = canvas.getContext('2d')
@@ -89,4 +101,4 @@
89101
}, {canvas: true})
90102
})
91103
})
92-
}(this.chai.expect))
104+
}())

0 commit comments

Comments
 (0)