UNPKG

predictype

Version:

PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.

47 lines (46 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const objectInstanceRelation_js_1 = require("./objectInstanceRelation.js"); (0, vitest_1.describe)('objectInstanceRelation', () => { (0, vitest_1.it)('should return true for instance_of', () => { class Foo { } const foo = new Foo(); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(foo, 'instance_of', Foo)).toBe(true); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(foo, 'instance_of', function Bar() { })).toBe(false); // primitives should always return false (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(42, 'instance_of', Foo)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)('str', 'instance_of', Foo)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(null, 'instance_of', Foo)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(undefined, 'instance_of', Foo)).toBe(false); // target not a function (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(foo, 'instance_of', 42)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(foo, 'instance_of', null)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(foo, 'instance_of', undefined)).toBe(false); }); (0, vitest_1.it)('should return true for prototype_of', () => { class Foo { } const proto = Foo.prototype; const foo = new Foo(); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(proto, 'prototype_of', foo)).toBe(true); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)({}, 'prototype_of', foo)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(proto, 'prototype_of', null)).toBe(false); // v not object/function (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(42, 'prototype_of', foo)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)('str', 'prototype_of', foo)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(undefined, 'prototype_of', foo)).toBe(false); // t not object/function (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(proto, 'prototype_of', 42)).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(proto, 'prototype_of', 'str')).toBe(false); (0, vitest_1.expect)((0, objectInstanceRelation_js_1.objectInstanceRelation)(proto, 'prototype_of', undefined)).toBe(false); }); (0, vitest_1.it)('should throw for unknown operator', () => { class Foo { } const foo = new Foo(); // @ts-expect-error (0, vitest_1.expect)(() => (0, objectInstanceRelation_js_1.objectInstanceRelation)(foo, 'invalid_operator', Foo)).toThrow('Unknown ObjectInstanceRelation operation: invalid_operator'); }); });