tsoid
Version:
Typed functional library to deal with async operations.
34 lines (33 loc) • 1.93 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 index_1 = require("../../index");
describe('either', () => {
const successCallback = jest.fn((n) => n + 1);
const errorCallback = jest.fn(() => 9);
it('Should call the success callback', () => __awaiter(void 0, void 0, void 0, function* () {
const action = () => Promise.resolve(0);
const res = yield index_1.either(successCallback, errorCallback, action);
expect(res).toBe(1);
}));
it('Should call the error callback', () => __awaiter(void 0, void 0, void 0, function* () {
// eslint-disable-next-line sonarjs/no-duplicate-string
const action = () => Promise.reject(new Error('Some error'));
const res = yield index_1.either(successCallback, errorCallback, action);
expect(res).toBeInstanceOf(Error);
expect(res.message).toBe('Some error');
}));
it('Should call the error callback if the action resolves to an Error', () => __awaiter(void 0, void 0, void 0, function* () {
const action = () => Promise.resolve(new Error('Some error'));
const res = yield index_1.either(successCallback, errorCallback, action);
expect(res).toBe(9);
}));
});