tsoid
Version:
Typed functional library to deal with async operations.
65 lines (64 loc) • 3 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const lift_1 = require("../lift");
describe('liftP2', () => {
const add = (x, y) => x + y;
const getOne = () => new Promise((resolve) => {
setTimeout(() => {
resolve(1);
}, 2000);
});
it('Should lift a function with 2-arity into two promises', () => {
const getTen = () => Promise.resolve(10);
const getSeven = () => Promise.resolve(7);
const result = lift_1.liftP2(add, getTen, getSeven);
result.then((value) => {
expect(value).toBe(17);
});
});
it('Should lift a function with 2-arity into two delayed promises', () => __awaiter(void 0, void 0, void 0, function* () {
const getTwo = () => new Promise((resolve) => {
setTimeout(() => {
resolve(2);
}, 500);
});
const result = yield lift_1.liftP2(add, getOne, getTwo);
expect(result).toBe(3);
}));
it('Should propagate an Error', () => __awaiter(void 0, void 0, void 0, function* () {
const getTwo = () => new Promise((_, reject) => {
reject(new Error('Some error message'));
});
const result = yield lift_1.liftP2(add, getOne, getTwo);
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('Some error message');
}));
it('Should propagate the Error if first action resolves to an Error', () => __awaiter(void 0, void 0, void 0, function* () {
const getTwo = () => new Promise((resolve) => {
resolve(new Error('Oops'));
});
// @ts-ignore
const result = yield lift_1.liftP2(add, getTwo, getOne);
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('Oops');
}));
it('Should propagate the Error if second action resolves to an Error', () => __awaiter(void 0, void 0, void 0, function* () {
// eslint-disable-next-line sonarjs/no-identical-functions
const getTwo = () => new Promise((resolve) => {
resolve(new Error('Oops'));
});
// @ts-ignore
const result = yield lift_1.liftP2(add, getOne, getTwo);
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('Oops');
}));
});