tsoid
Version:
Typed functional library to deal with async operations.
41 lines (40 loc) • 2.25 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 bind_1 = require("../bind");
const safe_1 = require("../safe");
describe('bind', () => {
it('Should bind three actions', () => __awaiter(void 0, void 0, void 0, function* () {
const initial = () => Promise.resolve(1);
const doubleP = (x) => Promise.resolve(x * 2);
const tripleP = (x) => Promise.resolve(x * 3);
const stringfyP = (x) => Promise.resolve(`${x}`);
const result = yield bind_1.default(initial, doubleP, tripleP, stringfyP);
expect(result).toBe('6');
}));
it('Should propagate failure', () => __awaiter(void 0, void 0, void 0, function* () {
const initial = () => Promise.resolve(1);
const doubleP = (x) => Promise.resolve(x * 2);
const tripleP = () => Promise.reject(new Error('X is invalid!'));
const stringfyP = (x) => Promise.resolve(`${x}`);
const result = yield safe_1.default(bind_1.default, initial, doubleP, tripleP, stringfyP);
expect(result).toBeInstanceOf(Error);
expect(result.message).toBe('X is invalid!');
}));
it('Should implement the associative monad law', () => __awaiter(void 0, void 0, void 0, function* () {
const m = () => Promise.resolve(1);
const f = (x) => Promise.resolve(x + 1);
const g = (x) => Promise.resolve(x / 2);
const res1 = yield bind_1.default(m, f, g);
const res2 = yield bind_1.default(m, (x) => bind_1.default(() => x, f, g));
expect(res1).toBe(res2);
}));
});