-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
89 lines (76 loc) · 2.35 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
// Require the dependencies
var assert = require("assert");
var promisify = require(".");
// Start the tests
describe('Test without arguments', function() {
var asyncMethod = function(callback) {
callback(null, "Hello world!");
};
var fn = promisify(asyncMethod);
it("should assert the string to match the original (callback version)", function(done) {
fn(function(err, str) {
assert.equal(null, err);
assert.equal("Hello world!", str);
done();
});
});
it("should assert the string to match the original (promise version)", function(done) {
fn()
.then(function(str) {
assert.equal("Hello world!", str);
})
.catch(function(err) {
assert.equal(null, str);
})
.then(done);
});
});
describe('Sum two arguments, using the err and value args', function() {
var asyncMethod = function(num1, num2, callback) {
callback(num1, num2);
};
var fn = promisify(asyncMethod);
it("should assert the num to be equally to 15", function(done) {
fn(3, 12, function(err, value) {
assert.equal(15, err + value);
done();
});
});
it("should assert the promise to the exact numbers", function(done) {
fn(3, 12)
.then(function(value) {
assert.equal(12, value);
})
.catch(function(err) {
assert.equal(3, err);
})
.then(done);
});
});
describe('Promisify a native Node module', function() {
var fs = promisify(require("fs"));
var crypto = require("crypto");
it("should assert the hashed content of the given string", function() {
return fs.readFile("./LICENSE")
.then(function(content) {
var hash = crypto
.createHash("sha1")
.update(content)
.digest("hex");
assert.equal("4fe3cbcca15cfa585857288d29d42c0973fc850a", hash);
});
});
});
describe('callback handling', function() {
it('should throw error if using a callback', function(done) {
var failMe = promisify(function(msg, cb) {
throw new Error(msg);
});
try {
failMe('fail', function () {});
} catch (err) {
assert.equal(err.message, 'fail')
done();
}
});
});