You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.9 KiB
75 lines
1.9 KiB
4 years ago
|
import assert from 'assert';
|
||
4 years ago
|
import nodeify from './nodeify';
|
||
8 years ago
|
|
||
8 years ago
|
describe('nodeify', function () {
|
||
6 years ago
|
const obj = {
|
||
8 years ago
|
foo: 'bar',
|
||
4 years ago
|
promiseFunc(a) {
|
||
4 years ago
|
const solution = this.foo + a;
|
||
|
return Promise.resolve(solution);
|
||
8 years ago
|
},
|
||
4 years ago
|
};
|
||
8 years ago
|
|
||
8 years ago
|
it('should retain original context', function (done) {
|
||
4 years ago
|
const nodified = nodeify(obj.promiseFunc, obj);
|
||
5 years ago
|
nodified('baz', (err, res) => {
|
||
7 years ago
|
if (!err) {
|
||
4 years ago
|
assert.equal(res, 'barbaz');
|
||
|
done();
|
||
|
return;
|
||
7 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
done(new Error(err.toString()));
|
||
|
});
|
||
|
});
|
||
7 years ago
|
|
||
6 years ago
|
it('no callback - should allow the last argument to not be a function', function (done) {
|
||
4 years ago
|
const nodified = nodeify(obj.promiseFunc, obj);
|
||
7 years ago
|
try {
|
||
4 years ago
|
nodified('baz');
|
||
|
done();
|
||
7 years ago
|
} catch (err) {
|
||
4 years ago
|
done(
|
||
|
new Error(
|
||
|
'should not have thrown if the last argument is not a function',
|
||
|
),
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
|
it('sync functions - returns value', function (done) {
|
||
4 years ago
|
const nodified = nodeify(() => 42);
|
||
6 years ago
|
try {
|
||
|
nodified((err, result) => {
|
||
5 years ago
|
if (err) {
|
||
4 years ago
|
done(new Error(`should not have thrown any error: ${err.message}`));
|
||
|
return;
|
||
5 years ago
|
}
|
||
4 years ago
|
assert.equal(42, result, 'got expected result');
|
||
|
});
|
||
|
done();
|
||
6 years ago
|
} catch (err) {
|
||
4 years ago
|
done(new Error(`should not have thrown any error: ${err.message}`));
|
||
6 years ago
|
}
|
||
4 years ago
|
});
|
||
6 years ago
|
|
||
|
it('sync functions - handles errors', function (done) {
|
||
5 years ago
|
const nodified = nodeify(() => {
|
||
4 years ago
|
throw new Error('boom!');
|
||
|
});
|
||
6 years ago
|
try {
|
||
|
nodified((err, result) => {
|
||
5 years ago
|
if (result) {
|
||
4 years ago
|
done(new Error('should not have returned any result'));
|
||
|
return;
|
||
5 years ago
|
}
|
||
4 years ago
|
assert.ok(err, 'got expected error');
|
||
|
assert.ok(err.message.includes('boom!'), 'got expected error message');
|
||
|
});
|
||
|
done();
|
||
6 years ago
|
} catch (err) {
|
||
4 years ago
|
done(new Error(`should not have thrown any error: ${err.message}`));
|
||
6 years ago
|
}
|
||
4 years ago
|
});
|
||
|
});
|