Permalink
Browse files

moved unit tests into separate file

  • Loading branch information...
1 parent c0acfa4 commit 476d8daa2b0de155964a04a2e0a04570604e9020 @bahmutov committed Sep 25, 2013
Showing with 86 additions and 87 deletions.
  1. +3 −3 .jshintrc
  2. +1 −1 Gruntfile.js
  3. +3 −83 index.html
  4. +79 −0 tests.js
View
@@ -50,13 +50,13 @@
"supernew" : false,
"validthis" : false,
- "browser" : false,
+ "browser" : true,
"couch" : false,
"devel" : true,
"dojo" : false,
- "jquery" : false,
+ "jquery" : true,
"mootools" : false,
- "node" : true,
+ "node" : false,
"nonstandard" : false,
"prototypejs" : false,
"rhino" : false,
View
@@ -9,7 +9,7 @@ module.exports = function (grunt) {
jshintrc: '.jshintrc',
},
'default': {
- src: [ 'Gruntfile.js', 'qunit-promises.js' ]
+ src: [ '*.js' ]
}
},
qunit: {
View
@@ -15,90 +15,10 @@
<div id="qunit-fixture"></div>
<!-- for deferred -->
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
+
+ <!-- include qunit first, then qunit-promises, then tests -->
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="qunit-promises.js"></script>
-
- <script>
- function delayedHello() {
- var deferred = new $.Deferred();
- setTimeout(function () { deferred.resolve('hello'); }, 100);
- return deferred.promise();
- }
-
- function delayedHelloFail() {
- var deferred = new $.Deferred();
- setTimeout(function () { deferred.reject('bye'); }, 100);
- return deferred.promise();
- }
-
- function delayedOne() {
- var deferred = new $.Deferred();
- setTimeout(function () { deferred.resolve(1); }, 100);
- return deferred.promise();
- }
-
- function delayedOneFail() {
- var deferred = new $.Deferred();
- setTimeout(function () { deferred.reject(1); }, 100);
- return deferred.promise();
- }
-
- // regular custom code testing a successful promise
- QUnit.asyncTest("test successful promise", 1, function (assert) {
- var promise = delayedHello();
- promise.then(function (actual) {
- assert.equal(actual, 'hello', 'promise resolved with "hello"');
- }).always(start);
- });
-
- // qunit-promises assertion
- QUnit.asyncTest("promise will resolve", 1, function (assert) {
- assert.will(delayedHello());
- });
-
- QUnit.asyncTest("promise will resolve with message", 1, function (assert) {
- assert.will(delayedHello(), 'the promise is ok');
- });
-
- QUnit.asyncTest("promise will resolve with value", 1, function (assert) {
- assert.willEqual(delayedHello(), 'hello', 'returns "hello"');
- });
-
- QUnit.asyncTest("compare value using deep equality", 1, function (assert) {
- assert.willEqual(delayedOne(), 1, 'returns 1');
- });
-
- // regular code to test failed promise
- QUnit.asyncTest("test failed promise", 1, function (assert) {
- var promise = delayedHelloFail();
- promise.then(function () {
- assert.ok(false, 'promise failed to fail!');
- }, function (actual) {
- assert.equal(actual, 'bye', 'promise failed');
- }).always(start);
- });
-
- QUnit.asyncTest("promise will reject", 1, function (assert) {
- assert.wont(delayedHelloFail(), 'promise fails');
- });
-
- QUnit.asyncTest("promise will reject with value", 1, function (assert) {
- assert.wontEqual(delayedHelloFail(), 'bye');
- });
-
- QUnit.asyncTest("promise will reject with value + message", 1, function (assert) {
- assert.wontEqual(delayedHelloFail(), 'bye', 'this is failed promise');
- });
-
- QUnit.asyncTest("using deep equality", 1, function (assert) {
- assert.wontEqual(delayedOneFail(), 1, 'returns 1');
- });
-
-
- // advanced
- QUnit.asyncTest("two resolved promises in sequence", 1, function (assert) {
- assert.willEqual(delayedHello().then(delayedHello), 'hello', 'promises chained');
- });
- </script>
+ <script src="tests.js"></script>
</body>
</html>
View
@@ -0,0 +1,79 @@
+function delayedHello() {
+ var deferred = new $.Deferred();
+ setTimeout(function () { deferred.resolve('hello'); }, 100);
+ return deferred.promise();
+}
+
+function delayedHelloFail() {
+ var deferred = new $.Deferred();
+ setTimeout(function () { deferred.reject('bye'); }, 100);
+ return deferred.promise();
+}
+
+function delayedOne() {
+ var deferred = new $.Deferred();
+ setTimeout(function () { deferred.resolve(1); }, 100);
+ return deferred.promise();
+}
+
+function delayedOneFail() {
+ var deferred = new $.Deferred();
+ setTimeout(function () { deferred.reject(1); }, 100);
+ return deferred.promise();
+}
+
+// regular custom code testing a successful promise
+QUnit.asyncTest('test successful promise', 1, function (assert) {
+ var promise = delayedHello();
+ promise.then(function (actual) {
+ assert.equal(actual, 'hello', 'promise resolved with "hello"');
+ }).always(QUnit.start);
+});
+
+// qunit-promises assertion
+QUnit.asyncTest('promise will resolve', 1, function (assert) {
+ assert.will(delayedHello());
+});
+
+QUnit.asyncTest('promise will resolve with message', 1, function (assert) {
+ assert.will(delayedHello(), 'the promise is ok');
+});
+
+QUnit.asyncTest('promise will resolve with value', 1, function (assert) {
+ assert.willEqual(delayedHello(), 'hello', 'returns "hello"');
+});
+
+QUnit.asyncTest('compare value using deep equality', 1, function (assert) {
+ assert.willEqual(delayedOne(), 1, 'returns 1');
+});
+
+// regular code to test failed promise
+QUnit.asyncTest('test failed promise', 1, function (assert) {
+ var promise = delayedHelloFail();
+ promise.then(function () {
+ assert.ok(false, 'promise failed to fail!');
+ }, function (actual) {
+ assert.equal(actual, 'bye', 'promise failed');
+ }).always(QUnit.start);
+});
+
+QUnit.asyncTest('promise will reject', 1, function (assert) {
+ assert.wont(delayedHelloFail(), 'promise fails');
+});
+
+QUnit.asyncTest('promise will reject with value', 1, function (assert) {
+ assert.wontEqual(delayedHelloFail(), 'bye');
+});
+
+QUnit.asyncTest('promise will reject with value + message', 1, function (assert) {
+ assert.wontEqual(delayedHelloFail(), 'bye', 'this is a failed promise');
+});
+
+QUnit.asyncTest('using deep equality', 1, function (assert) {
+ assert.wontEqual(delayedOneFail(), 1, 'returns 1');
+});
+
+// advanced
+QUnit.asyncTest('two resolved promises in sequence', 1, function (assert) {
+ assert.willEqual(delayedHello().then(delayedHello), 'hello', 'promises chained');
+});

0 comments on commit 476d8da

Please sign in to comment.