UNPKG

smart-types-ts

Version:

A collection of _Smart Types_ and _Smart Constructors_ which enable you to be more strict when defining your application's important types/interfaces.

51 lines (50 loc) 2.52 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const fc = __importStar(require("fast-check")); const e = __importStar(require("fp-ts/lib/Either")); const withLength_1 = require("./withLength"); describe("WithLength", () => { describe("mkWithLength", () => { it("returns a Left when the input is invalid", () => { const min = 10; const max = 30; const invalidInput = fc.oneof(fc.array(fc.anything(), { minLength: max + 1 }), fc.array(fc.anything(), { maxLength: min - 1 }), fc.string({ maxLength: min - 1 }), fc.string({ minLength: max + 1 })); fc.assert(fc.property(invalidInput, input => { chai_1.assert.deepStrictEqual(withLength_1.mkWithLength(min, max)(input), e.left(`Length not between ${min}-${max}`)); })); }); it("succeeds when the input is an array with length not exceeding the specified amount", () => { const min = 10; const max = 30; const validInput = fc.oneof(fc.array(fc.anything(), { minLength: min, maxLength: max }), fc.string({ minLength: min, maxLength: max })); fc.assert(fc.property(validInput, input => { chai_1.assert.deepStrictEqual(withLength_1.mkWithLength(min, max)(input), e.right(input)); })); }); // length cannot be negative! it("throws an error if min or max is negative", () => { chai_1.assert.throws(() => withLength_1.mkWithLength(-5, 10)); chai_1.assert.throws(() => withLength_1.mkWithLength(0, -20)); }); }); });