UNPKG

tsoid

Version:

Typed functional library to deal with async operations.

53 lines (52 loc) 2.83 kB
"use strict"; 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"); const utils_1 = require("../../../tests/utils"); const index_1 = require("../../index"); describe('liftP', () => { const addOne = (x) => x + 1; const getUserID = (user) => user.uid; const filterEvens = (users) => users.filter((_, index) => index % 2 === 0); const mapID = (users) => users.map((user) => user.uid); const joinIds = (ids) => ids.join(' -> '); it('Should lift a function into a promise', () => { const px = () => Promise.resolve(1); const result = lift_1.default(addOne, px); result.then((value) => expect(value).toBe(2)); }); it('Lift a function that get the user id from Promise<User>', () => __awaiter(void 0, void 0, void 0, function* () { const uid = yield lift_1.default(getUserID, () => utils_1.getUser('123')); expect(uid).toBe('123'); })); it('Should handle promise failure', () => __awaiter(void 0, void 0, void 0, function* () { const uid = yield lift_1.default(getUserID, () => utils_1.getUserWithError('123')); expect(uid).toBeInstanceOf(Error); expect(uid.message).toBe('Error getting 123'); })); it('Should return a promise of an error if any error occur when lifting', () => { const maybeGetTen = () => Promise.reject(new Error('reason test')); const result = lift_1.default(addOne, maybeGetTen); result.catch((err) => { expect(err).toBeInstanceOf(Error); }); }); it('Should lift a composed function', () => __awaiter(void 0, void 0, void 0, function* () { const composedFn = index_1.compose(joinIds, mapID, filterEvens); const result = yield lift_1.default(composedFn, utils_1.getUsers); expect(result).toBe('01 -> 03'); })); it('Should lift a piped function', () => __awaiter(void 0, void 0, void 0, function* () { const pipedFn = index_1.pipe(filterEvens, mapID, joinIds); const result = yield lift_1.default(pipedFn, utils_1.getUsers); expect(result).toBe('01 -> 03'); })); });