2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-03-16 22:00:08 +01:00
|
|
|
import nodeify from './nodeify';
|
2016-11-29 02:27:20 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('nodeify', function () {
|
2019-03-29 05:37:40 +01:00
|
|
|
const obj = {
|
2016-11-29 02:27:20 +01:00
|
|
|
foo: 'bar',
|
2020-11-03 00:41:28 +01:00
|
|
|
promiseFunc(a) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const solution = this.foo + a;
|
|
|
|
return Promise.resolve(solution);
|
2017-05-04 23:35:10 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-11-29 02:27:20 +01:00
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
it('should retain original context', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const nodified = nodeify(obj.promiseFunc, obj);
|
2020-02-11 17:51:13 +01:00
|
|
|
nodified('baz', (err, res) => {
|
2018-07-03 02:21:27 +02:00
|
|
|
if (!err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(res, 'barbaz');
|
|
|
|
done();
|
|
|
|
return;
|
2018-07-03 02:21:27 +02:00
|
|
|
}
|
2020-08-14 13:47:43 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error(err.toString()));
|
|
|
|
});
|
|
|
|
});
|
2017-08-04 04:10:33 +02:00
|
|
|
|
2019-03-29 05:37:40 +01:00
|
|
|
it('no callback - should allow the last argument to not be a function', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const nodified = nodeify(obj.promiseFunc, obj);
|
2017-08-04 04:10:33 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
nodified('baz');
|
|
|
|
done();
|
2017-10-06 22:16:44 +02:00
|
|
|
} catch (err) {
|
2020-11-03 00:41:28 +01:00
|
|
|
done(
|
|
|
|
new Error(
|
|
|
|
'should not have thrown if the last argument is not a function',
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-08-04 04:10:33 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-03-29 05:37:40 +01:00
|
|
|
|
|
|
|
it('sync functions - returns value', function (done) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const nodified = nodeify(() => 42);
|
2019-03-29 05:37:40 +01:00
|
|
|
try {
|
|
|
|
nodified((err, result) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error(`should not have thrown any error: ${err.message}`));
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.equal(42, result, 'got expected result');
|
|
|
|
});
|
|
|
|
done();
|
2019-03-29 05:37:40 +01:00
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error(`should not have thrown any error: ${err.message}`));
|
2019-03-29 05:37:40 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-03-29 05:37:40 +01:00
|
|
|
|
|
|
|
it('sync functions - handles errors', function (done) {
|
2019-11-20 01:03:20 +01:00
|
|
|
const nodified = nodeify(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('boom!');
|
|
|
|
});
|
2019-03-29 05:37:40 +01:00
|
|
|
try {
|
|
|
|
nodified((err, result) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (result) {
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error('should not have returned any result'));
|
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.ok(err, 'got expected error');
|
|
|
|
assert.ok(err.message.includes('boom!'), 'got expected error message');
|
|
|
|
});
|
|
|
|
done();
|
2019-03-29 05:37:40 +01:00
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
done(new Error(`should not have thrown any error: ${err.message}`));
|
2019-03-29 05:37:40 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|