Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Dec 12, 2024
1 parent 11b9a36 commit 4c28650
Showing 1 changed file with 69 additions and 8 deletions.
77 changes: 69 additions & 8 deletions test/async.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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();
}
});

0 comments on commit 4c28650

Please sign in to comment.