diff --git a/test/async.js b/test/async.js index 1aaf86c..e873693 100644 --- a/test/async.js +++ b/test/async.js @@ -1,14 +1,10 @@ 'use strict'; const metatests = require('metatests'); -const { - toBool, - timeout, - delay, - timeoutify, - throttle, - debounce, -} = require('..'); +const metautil = require('..'); +const { toBool, timeout, delay } = metautil; +const { timeoutify, throttle, debounce } = metautil; +const { callbackify, asyncify, promisify } = metautil; metatests.test('Async: toBool', async (test) => { const success = await Promise.resolve('success').then(...toBool); @@ -175,3 +171,68 @@ metatests.test('Async: debounce without arguments', (test) => { debouncedFn(); test.strictSame(count, 0); }); + +metatests.test('Callbackify: Promise to callback-last', (test) => { + const promiseReturning = () => Promise.resolve('result'); + const asyncFn = callbackify(promiseReturning); + + asyncFn((err, value) => { + if (err) { + test.error(err, 'must not throw'); + } + test.strictSame(value, 'result'); + test.end(); + }); +}); + +metatests.test('Asyncify: sync function to callback-last', (test) => { + const fn = (par) => par; + const asyncFn = asyncify(fn); + + asyncFn('result', (err, value) => { + if (err) { + test.error(err, 'must not throw'); + } + test.strictSame(value, 'result'); + test.end(); + }); +}); + +metatests.test('Promisify: callback-last to Promise', async (test) => { + const id = 100; + const data = { key: 'value' }; + + const getDataAsync = (dataId, callback) => { + test.strictSame(dataId, id); + callback(null, data); + }; + + const getDataPromise = promisify(getDataAsync); + + try { + const result = await getDataPromise(id); + test.strictSame(result, data); + test.end(); + } catch (err) { + test.error(err, 'must not throw'); + } +}); + +metatests.test('Promisify: callback-last to Promise throw', async (test) => { + const id = 100; + + const getDataAsync = (dataId, callback) => { + test.strictSame(dataId, id); + callback(new Error('Data not found')); + }; + + const getDataPromise = promisify(getDataAsync); + + try { + const result = await getDataPromise(id); + test.notOk(result); + } catch (err) { + test.ok(err); + test.end(); + } +});