Commit 266032dc authored by Daniel Shumway's avatar Daniel Shumway

test: Add basic tests for `then`

- not complete, need some more work
- add a small amount of documentation to other tests
parent 73936741
Pipeline #8386005 passed with stage
in 1 minute and 37 seconds
......@@ -8,6 +8,7 @@ Distilled.suite = new Distilled();
console.log('Distilled self-test: ');
require('./distilled/test.js');
require('./distilled/then.js');
require('./distilled/callback.js');
require('./distilled/assertions.js');
require('./distilled/recursion.js');
......
......@@ -29,37 +29,63 @@ function deferred () {
*/
suite.test('basic types', function () {
var def = deferred();
var _suite = new Distilled(function (test) {
if (test === _reject) {
var _suite = new Distilled(function(test) {
if (test === tests.reject) {
def.resolve(assertion);
}
});
var _null = _suite.test('null', null),
_undefined = _suite.test('undefined', undefined),
_true = _suite.test('true', true),
_false = _suite.test('false', false),
_reject = _suite.test('reject', Promise.reject('error'));
var tests = {
null: _suite.test('null', null),
undefined: _suite.test('undefined', undefined),
true: _suite.test('true', true),
false: _suite.test('false', false),
reject: _suite.test('reject', Promise.reject('error'))
};
function assertion () {
/**
* ``undefined`` or ``null``:
*
* ```js
* suite.test('', null); //passes
* suite.test('', undefined); //passes
* suite.test(''); //passes
* ```
*/
this.test('undefined', function () {
assert.strictEqual(_null.status, Distilled.STATUS.PASSED, 'Accept `null`');
assert.strictEqual(_undefined.status, Distilled.STATUS.PASSED, 'Accept `undefined`');
assert.strictEqual(tests.null.status, Distilled.STATUS.PASSED, 'Accept `null`');
assert.strictEqual(tests.undefined.status, Distilled.STATUS.PASSED, 'Accept `undefined`');
});
/**
* A Boolean:
*
* ```js
* suite.test('', true); //passes
* suite.test('', false); //fails
* ```
*/
this.test('boolean', function () {
assert.strictEqual(_true.status, Distilled.STATUS.PASSED, 'Accept `true`');
assert.strictEqual(_false.status, Distilled.STATUS.FAILED, 'Reject `false`');
assert.strictEqual(tests.true.status, Distilled.STATUS.PASSED, 'Accept `true`');
assert.strictEqual(tests.false.status, Distilled.STATUS.FAILED, 'Reject `false`');
});
/**
* A Promise:
*
* ```js
* suite.test('', Promise.reject('error')); //fails (with correct error)
*```
*/
this.test('rejected promises', function () {
assert.strictEqual(_reject.status, Distilled.STATUS.FAILED, 'Reject rejected promises');
assert.strictEqual(_reject.error, 'error', '`test.error` is the rejected promise\'s error');
assert.strictEqual(tests.reject.status, Distilled.STATUS.FAILED, 'Reject rejected promises');
assert.strictEqual(tests.reject.error, 'error', '`test.error` is the rejected promise\'s error');
});
}
return def;
return def.promise;
});
suite.test('advanced recursion', function () {
......
......@@ -29,6 +29,10 @@ suite.test('Tests/suites expose a `test` method', function () {
this.test('Calling `test` returns a new test/suite of the same type', function () {
assert.strictEqual(Object.getPrototypeOf(_suite), Object.getPrototypeOf(_suite.test()));
/*
* Distilled is designed to be extended, so if you extend Distilled, new tests will correctly inherit from your prototype.
* They'll also call custom constructors if you define them on the prototype.
*/
this.test('also works when extending prototype', function () {
function Extension () {
Distilled.apply(this, arguments);
......
var Distilled = require('Distilled');
var assert = require('assert');
var suite = Distilled.suite;
function deferred () {
var result = {};
result.promise = new Promise(function (res, rej) {
result.resolve = res;
result.reject = rej;
});
return result;
}
/**
* The ``then`` method allows you to respond to a test's completion.
*
* Its behaviors closely mirror ``test``:
* - it is chainable
* - ``this`` is set correctly, and a suite is passed in as a parameter
*
* The difference between ``then`` and ``test`` is that ``then`` does not return a new suite.
* It returns the existing suite.
*/
suite = suite.test('Tests/suites expose a `then` method', function () {
var _suite = new Distilled(function () {});
assert.strictEqual(typeof suite.then, 'function');
this.test('Calling `then` returns the same test/suite', function () {
assert.strictEqual(_suite, _suite.then());
});
});
suite.test('`then` is called when a test finishes.', function (test) {
var def = deferred();
var _suite = new Distilled(function () {});
_suite.then(function () {
def.resolve();
this.test('`then` can be called multiple times on the same test.', function () {
var def = deferred();
_suite.then(function () {
def.resolve();
});
return def.promise;
});
});
return def.promise;
});
//`Then` is called for skipped tests?
/*
* This order matters both because:
* - it's predictable, and
* - it allows more advanced extensions.
*
* If you can guarantee that `then` will fire before a child test, you can insert your own behavior into it.
*
* ```js
* function Extension () {
* Distilled.apply(this, arguments);
* }
*
* Extension.prototype = Object.create(Distilled.prototype);
* Extension.prototype.test = function () {
* return Distilled.test.apply(this, arguments).then(function () {
* //Your custom behavior here.
* });
* }
* ```
*/
suite.test('`then` resolves in normal promise order with `test`', function () {
var def = deferred();
var _suite = new Distilled(function () {});
var calls = [];
_suite.test('A', function () {
calls.push('A');
});
_suite.then(function () {
calls.push('B');
});
_suite.test('C', function () {
calls.push('C');
def.resolve(assertions);
});
//Tests
function assertions () {
assert.deepEqual(calls, ['A', 'B', 'C'], 'promises resolved in the correct order');
}
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment