Skip to content

Commit

Permalink
Unit testing initials
Browse files Browse the repository at this point in the history
  • Loading branch information
Ádám Rocska committed Feb 22, 2016
1 parent 5bb1c11 commit 503f5e5
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 4 deletions.
7 changes: 4 additions & 3 deletions .gitignore
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm-debug.log
node_modules/
bower_components
npm-debug.log
node_modules/
bower_components
/nbproject/private/
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"version": "2.0.0",
"description": "A simple javascript image cropper",
"main": "croppie.js",
"dependencies" : {
"mocha": "2.4.5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test/unit"
},
"repository": {
"type": "git",
Expand Down
48 changes: 48 additions & 0 deletions test/unit/Croppie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var assert, Croppie;

assert = require('assert');

require('./stubs/window');
Croppie = require('../../croppie');

describe('Croppie', function () {
var testCroppieObject, stubElement;

beforeEach(function () {
stubElement = new HTMLElement();
testCroppieObject = new Croppie(stubElement);
});

describe('constructor', function () {
it('should expose a reference to its bound element.', function () {
assert.strictEqual(testCroppieObject.element, stubElement);
});

it('should use croppy defaults if no options are provided.', function () {
function matchDefaults(actualOptionGroup, expectedOptionGroup, path) {
path = path || 'options';

Object
.keys(expectedOptionGroup)
.forEach(function (optionName) {
var currentPath;

currentPath = [
path,
optionName
].join('.');

if (typeof expectedOptionGroup[optionName] === 'object') {
matchDefaults(actualOptionGroup[optionName], expectedOptionGroup[optionName], currentPath);
} else {
assert.equal(actualOptionGroup[optionName], expectedOptionGroup[optionName], 'Matching ' + currentPath);
}
});
}

matchDefaults(testCroppieObject.options, Croppie.defaults);
});

});

});
3 changes: 3 additions & 0 deletions test/unit/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--reporter spec
--recursive
--growl
14 changes: 14 additions & 0 deletions test/unit/stubs/DOMTokenList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DOMTokenList = function DOMTokenList() {
this.add = function () {
};
this.remove = function () {
};
this.item = function () {
};
this.toggle = function () {
};
this.contains = function () {
};
};

module.exports = DOMTokenList;
10 changes: 10 additions & 0 deletions test/unit/stubs/EventTarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
EventTarget = function EventTarget() {
this.addEventListener = function () {
};
this.removeEventListener = function () {
};
this.dispatchEvent = function () {
};
};

module.exports = EventTarget;
13 changes: 13 additions & 0 deletions test/unit/stubs/HTMLElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DOMTokenList = require('./DOMTokenList');
EventTarget = require('./EventTarget');

HTMLElement = function HTMLElement() {
EventTarget.apply(this, arguments);
this.style = {};
this.classList = new DOMTokenList();
this.appendChild = function () {
};
};
HTMLElement.prototype = Object.create(EventTarget.prototype);

module.exports = HTMLElement;
11 changes: 11 additions & 0 deletions test/unit/stubs/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var HTMLElement;

HTMLElement = require('./HTMLElement');

document = {
createElement : function () {
return new HTMLElement();
}
};

module.exports = document;
9 changes: 9 additions & 0 deletions test/unit/stubs/window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var document;

document = require('./document');

window = {
document : document
};

module.exports = window;

0 comments on commit 503f5e5

Please sign in to comment.