|
| 1 | +import { isInstanceOfClass } from "./isInstanceOfClass"; |
| 2 | + |
| 3 | +// Test classes |
| 4 | +class TestClass { |
| 5 | + constructor(public value: string) {} |
| 6 | +} |
| 7 | + |
| 8 | +class AnotherClass { |
| 9 | + constructor(public number: number) {} |
| 10 | +} |
| 11 | + |
| 12 | +class SubClass extends TestClass { |
| 13 | + constructor(value: string, public extra: boolean) { |
| 14 | + super(value); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe("isInstanceOfClass", () => { |
| 19 | + describe("with specific class constructor", () => { |
| 20 | + it("returns true for instances of the specified class", () => { |
| 21 | + const instance = new TestClass("test"); |
| 22 | + expect(isInstanceOfClass(instance, TestClass)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns true for instances of subclasses", () => { |
| 26 | + const subInstance = new SubClass("test", true); |
| 27 | + expect(isInstanceOfClass(subInstance, TestClass)).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns false for instances of different classes", () => { |
| 31 | + const instance = new TestClass("test"); |
| 32 | + expect(isInstanceOfClass(instance, AnotherClass)).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns false for plain objects", () => { |
| 36 | + const plainObj = { value: "test" }; |
| 37 | + expect(isInstanceOfClass(plainObj, TestClass)).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns false for primitives", () => { |
| 41 | + expect(isInstanceOfClass("string", TestClass)).toBe(false); |
| 42 | + expect(isInstanceOfClass(123, TestClass)).toBe(false); |
| 43 | + expect(isInstanceOfClass(true, TestClass)).toBe(false); |
| 44 | + expect(isInstanceOfClass(Symbol(), TestClass)).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns false for null and undefined", () => { |
| 48 | + expect(isInstanceOfClass(null, TestClass)).toBe(false); |
| 49 | + expect(isInstanceOfClass(undefined, TestClass)).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns false for arrays", () => { |
| 53 | + expect(isInstanceOfClass([], TestClass)).toBe(false); |
| 54 | + expect(isInstanceOfClass([1, 2, 3], TestClass)).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it("returns false for functions", () => { |
| 58 | + expect(isInstanceOfClass(() => {}, TestClass)).toBe(false); |
| 59 | + expect(isInstanceOfClass(function () {}, TestClass)).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it("works as a type guard", () => { |
| 63 | + const items = [ |
| 64 | + new TestClass("a"), |
| 65 | + { value: "b" }, // plain object |
| 66 | + new AnotherClass(1), |
| 67 | + new TestClass("c") |
| 68 | + ]; |
| 69 | + |
| 70 | + const testClassInstances = items.filter((item): item is TestClass => isInstanceOfClass(item, TestClass)); |
| 71 | + |
| 72 | + expect(testClassInstances).toHaveLength(2); |
| 73 | + expect(testClassInstances[0]).toBeInstanceOf(TestClass); |
| 74 | + expect(testClassInstances[1]).toBeInstanceOf(TestClass); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("without class constructor (checking for any class instance)", () => { |
| 79 | + it("returns true for class instances", () => { |
| 80 | + const testInstance = new TestClass("test"); |
| 81 | + const anotherInstance = new AnotherClass(123); |
| 82 | + const subInstance = new SubClass("test", true); |
| 83 | + |
| 84 | + expect(isInstanceOfClass(testInstance)).toBe(true); |
| 85 | + expect(isInstanceOfClass(anotherInstance)).toBe(true); |
| 86 | + expect(isInstanceOfClass(subInstance)).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it("returns false for plain objects", () => { |
| 90 | + const plainObj = { value: "test" }; |
| 91 | + const emptyObj = {}; |
| 92 | + |
| 93 | + expect(isInstanceOfClass(plainObj)).toBe(false); |
| 94 | + expect(isInstanceOfClass(emptyObj)).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it("returns false for primitives", () => { |
| 98 | + expect(isInstanceOfClass("string")).toBe(false); |
| 99 | + expect(isInstanceOfClass(123)).toBe(false); |
| 100 | + expect(isInstanceOfClass(true)).toBe(false); |
| 101 | + expect(isInstanceOfClass(Symbol())).toBe(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it("returns false for null and undefined", () => { |
| 105 | + expect(isInstanceOfClass(null)).toBe(false); |
| 106 | + expect(isInstanceOfClass(undefined)).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it("returns false for arrays", () => { |
| 110 | + expect(isInstanceOfClass([])).toBe(false); |
| 111 | + expect(isInstanceOfClass([1, 2, 3])).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + it("returns false for functions", () => { |
| 115 | + expect(isInstanceOfClass(() => {})).toBe(false); |
| 116 | + expect(isInstanceOfClass(function () {})).toBe(false); |
| 117 | + }); |
| 118 | + |
| 119 | + it("works as a type guard for any class instance", () => { |
| 120 | + const items = [ |
| 121 | + new TestClass("a"), |
| 122 | + { value: "b" }, // plain object |
| 123 | + new AnotherClass(1), |
| 124 | + "string", // primitive |
| 125 | + new SubClass("c", true) |
| 126 | + ]; |
| 127 | + |
| 128 | + const classInstances = items.filter((item): item is TestClass | AnotherClass | SubClass => isInstanceOfClass(item)); |
| 129 | + |
| 130 | + expect(classInstances).toHaveLength(3); |
| 131 | + expect(classInstances[0]).toBeInstanceOf(TestClass); |
| 132 | + expect(classInstances[1]).toBeInstanceOf(AnotherClass); |
| 133 | + expect(classInstances[2]).toBeInstanceOf(SubClass); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe("edge cases", () => { |
| 138 | + it("handles objects created with Object.create(null)", () => { |
| 139 | + const nullProtoObj = Object.create(null); |
| 140 | + expect(isInstanceOfClass(nullProtoObj)).toBe(false); // Not a plain object |
| 141 | + expect(isInstanceOfClass(nullProtoObj, TestClass)).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + it("handles objects with modified prototypes", () => { |
| 145 | + const obj = {}; |
| 146 | + Object.setPrototypeOf(obj, null); |
| 147 | + expect(isInstanceOfClass(obj)).toBe(false); // Not a plain object |
| 148 | + }); |
| 149 | + |
| 150 | + it("handles built-in objects", () => { |
| 151 | + expect(isInstanceOfClass(new Date())).toBe(true); |
| 152 | + expect(isInstanceOfClass(new Date(), Date)).toBe(true); |
| 153 | + expect(isInstanceOfClass(new Date(), TestClass)).toBe(false); |
| 154 | + |
| 155 | + expect(isInstanceOfClass(new RegExp("test"))).toBe(true); |
| 156 | + expect(isInstanceOfClass(new RegExp("test"), RegExp)).toBe(true); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments