-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
87 lines (73 loc) · 2.43 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
79
80
81
82
83
84
85
86
87
/*!
* hybridify <https://github.com/hybridables/hybridify>
*
* Copyright (c) 2014-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var fs = require('fs')
var test = require('mukla')
var hybridify = require('./index')
var isPromise = require('is-promise')
var isBuffer = require('is-buffer')
var isArray = require('isarray')
test('hybridify(fn, ...args, cb) returns a promise', function () {
var promise = hybridify(fs.readFile, './package.json', function (err, buf) {
test.ifError(err)
test.strictEqual(isBuffer(buf), true)
})
test.strictEqual(isPromise(promise), true)
return promise.then(function (value) {
test.strictEqual(isBuffer(value), true)
})
})
test('hybridify.hybridify(fn) returns a function', function () {
var statFile = hybridify.hybridify(fs.stat)
test.strictEqual(typeof statFile, 'function')
var count = 1
var promise = statFile('./index.js', function (err, stats) {
test.ifError(err)
test.strictEqual(typeof stats, 'object')
test.ok(stats.mtime)
count++
})
test.strictEqual(isPromise(promise), true)
return promise.then(function (value) {
test.ok(value.mode)
test.strictEqual(typeof value === 'object', true)
test.strictEqual(count, 2)
})
})
test('hybridify.hybridify(fn) work without final callback', function () {
var readFile = hybridify.hybridify(fs.readFile)
test.strictEqual(typeof readFile, 'function')
var promise = readFile('./index.js')
test.strictEqual(isPromise(promise), true)
return promise.then(function (buf) {
test.strictEqual(isBuffer(buf), true)
})
})
test('hybridify.hybridify(fn) with only callback', function (done) {
var readdir = hybridify.hybridify(fs.readdir)
test.strictEqual(typeof readdir, 'function')
readdir('./', function (err, files) {
test.ifError(err)
test.strictEqual(isArray(files), true)
test.strictEqual(files.length > 1, true)
done()
})
})
test('hybridify.promisify(fn) return a function (alias for letta.promisify)', function () {
var called = false
var readdirPromised = hybridify.promisify(fs.readdir)
var promise = readdirPromised('./', function callback () {
/* istanbul ignore next */
called = true
})
test.strictEqual(isPromise(promise), true)
return promise.then(function (val) {
test.strictEqual(isArray(val), true)
test.strictEqual(val.length > 1, true)
test.strictEqual(called, false)
})
})