-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
157 lines (137 loc) · 4.38 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*!
* letta-value <https://github.com/hybridables/letta-value>
*
* 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 letta = require('letta')
var isBuffer = require('is-buffer')
var lettaValue = require('./index')
var Observable = require('rx').Observable
// see `always-done` for more tests
// this fn is absolute copy of `always-done` module
function alwaysDone (val) {
if (typeof val === 'function') {
return letta.apply(this, arguments).then(lettaValue)
}
return lettaValue(val)
}
test('should accept string and handle it from `.then`', function (done) {
lettaValue('foo bar baz').then(function (res) {
test.strictEqual(res, 'foo bar baz')
done()
}, done)
})
test('should accept array and handle it from `.then`', function (done) {
lettaValue(['foo', 'bar', 'baz']).then(function (res) {
test.deepEqual(res, ['foo', 'bar', 'baz'])
done()
}, done)
})
test('should accept Error instance and catch it from `.catch`', function (done) {
lettaValue(new Error('foo err')).catch(function (err) {
test.strictEqual(err.message, 'foo err')
done()
})
})
test('should accept streams directly and handle successful completion', function (done) {
var readable = fs.createReadStream('package.json')
alwaysDone(readable).then(function (res) {
test.strictEqual(res, undefined)
done()
}, done)
})
test('should accept streams directly and handle failing completion', function (done) {
var readable = fs.createReadStream('foobar.json')
alwaysDone(readable).catch(function (err) {
test.strictEqual(err.code, 'ENOENT')
done()
})
})
test('should accept empty observable and handle completion', function (done) {
var observable = Observable.empty()
alwaysDone(observable).then(function (res) {
test.strictEqual(res, undefined)
done()
}, done)
})
test('should accept successful observable value and handle it', function (done) {
var observable = Observable.return([1, 2, 3])
alwaysDone(observable).then(function (res) {
test.deepEqual(res, [1, 2, 3])
done()
}, done)
})
test('should accept failing observable and `.catch` it', function (done) {
var observable = Observable.throw(new Error('observable error'))
alwaysDone(observable).catch(function (err) {
test.strictEqual(err.message, 'observable error')
done()
})
})
test('should accept sync function which return empty observable', function (done) {
alwaysDone(function success () {
return Observable.empty()
}).then(function (res) {
test.strictEqual(res, undefined)
done()
}, done)
})
test('should accept sync function returning successful observable value', function (done) {
alwaysDone(function successValue () {
return Observable.return(123)
}).then(function (res) {
test.strictEqual(res, 123)
done()
}, done)
})
test('should accept sync function returning failing observable', function (done) {
alwaysDone(function failure () {
return Observable.throw(new Error('observable err'))
}).catch(function (err) {
test.strictEqual(err.message, 'observable err')
done()
})
})
test('should accept sync function which returns stream and handle completion', function (done) {
alwaysDone(function () {
return fs.createReadStream('package.json')
}).then(function (res) {
test.strictEqual(res, undefined)
done()
}, done)
})
test('should accept sync function which returns failing stream completion', function (done) {
alwaysDone(function () {
return fs.createReadStream('foobar.json')
}).catch(function (err) {
test.strictEqual(err.code, 'ENOENT')
done()
})
})
test('should accept sync (fs.readFileSync) function and handle buffer', function (done) {
alwaysDone(fs.readFileSync, 'package.json')
.then(function (buf) {
test.strictEqual(isBuffer(buf), true)
done()
}, done)
})
test('should accept asynchronous function and get buffer', function (done) {
var read = fs.readFile
alwaysDone(read, 'package.json').then(function (res) {
test.strictEqual(isBuffer(res), true)
done()
}, done)
})
test('should accept async (callback) functions and handle utf8 result', function (done) {
alwaysDone(fs.readFile, 'package.json', 'utf8')
.then(JSON.parse)
.then(function (data) {
test.strictEqual(data.name, 'letta-value')
done()
}, done)
})