-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
87 lines (76 loc) · 2.59 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
/*!
* create-callback <https://github.com/tunnckoCore/create-callback>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
/* jshint asi:true */
'use strict'
var fs = require('fs')
var test = require('assertit')
var createCallback = require('./index')
test('create-callback:', function () {
test('should throw TypeError if `fn` not function', function (done) {
function fixture () {
var readFileAsync = createCallback(1234567)
readFileAsync('./package.json')
}
test.throws(fixture, TypeError)
test.throws(fixture, /is-async-function expect a function/)
done()
})
test('should throw TypeError if return function dont have callback', function (done) {
function fixture () {
var readFileAsync = createCallback(fs.readFileSync)
readFileAsync('./package.json')
}
test.throws(fixture, TypeError)
test.throws(fixture, /create-callback: async function expect a callback/)
done()
})
test('should handle errors thrown from given sync function', function (done) {
var parseJsonAsync = createCallback(JSON.parse)
parseJsonAsync('foo', function (err, res) {
test.ifError(!err)
test.equal(res, undefined)
test.equal(err instanceof SyntaxError, true)
test.equal(err.message, 'Unexpected token o')
done()
})
})
test('should handle errors from given async function', function (done) {
var readFileAsync = createCallback(fs.readFile)
readFileAsync('./not-existing-file', 'utf8', function (err, str) {
test.ifError(!err)
test.equal(str, undefined)
test.equal(err.code, 'ENOENT')
done()
})
})
test('should create async function from fs.readFileSync function', function (done) {
var readFile = createCallback(fs.readFileSync)
readFile('./package.json', 'utf8', function (err, str) {
var json = JSON.parse(str)
test.ifError(err)
test.equal(json.name, 'create-callback')
done()
})
})
test('should create async function from JSON.parse function', function (done) {
var parseJsonAsync = createCallback(JSON.parse)
parseJsonAsync('{"foo":"bar"}', function (err, res) {
test.ifError(err)
test.deepEqual(res, {foo: 'bar'})
done()
})
})
test('should directly return the given async function', function (done) {
var readFileAsync = createCallback(fs.readFile)
readFileAsync('./package.json', 'utf8', function (err, str) {
var json = JSON.parse(str)
test.ifError(err)
test.equal(json.name, 'create-callback')
done()
})
})
})