-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
78 lines (68 loc) · 2.12 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*!
* then-callback <https://github.com/hybridables/then-callback>
*
* Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var test = require('assertit')
var thenCallback = require('./index')
var NativePromise = require('native-or-another')()
test('should throw TypeError if `promise` not a promise', function (done) {
function fixture () {
thenCallback(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `promise` to be promise/)
done()
})
test('should accept callback-style function', function (done) {
var promise = NativePromise.resolve(123)
thenCallback(promise).then(function (err, res) {
test.ifError(err)
test.strictEqual(err, null)
test.strictEqual(res, 123)
done()
})
})
test('should works as normal `.then` if second argument is not given', function (done) {
var promise = NativePromise.resolve(123)
thenCallback(promise).then(function (res) {
test.strictEqual(res, 123)
done()
}, done)
})
test('should works as normal `.then` and handle errors', function (done) {
var promise = NativePromise.reject(new Error('foobar'))
thenCallback(promise).then(null, function (err) {
test.ifError(!err)
test.strictEqual(err.message, 'foobar')
done()
})
})
test('should handle errors when callback-style function given', function (done) {
var promise = NativePromise.reject(new Error('foobar'))
thenCallback(promise).then(function (err, res) {
test.ifError(!err)
test.strictEqual(err.message, 'foobar')
test.strictEqual(res, undefined)
done()
})
})
test('should catch errors as normal', function (done) {
var promise = NativePromise.reject(new Error('foobaz'))
thenCallback(promise).catch(function (err) {
test.ifError(!err)
test.strictEqual(err.message, 'foobaz')
done()
})
})
test('should work for arrays (fixes #4)', function (done) {
var promise = NativePromise.resolve([1, 2, 3, 4])
thenCallback(promise).then(function (err, res) {
test.ifError(err)
test.deepEqual(res, [1, 2, 3, 4])
done()
})
})