import {entity, isZentity} from "./zentity"; import {z, ZodDate, ZodError, ZodNumber, ZodObject, ZodString} from "zod"; describe("zentity", () => { const TestClass = entity({ name:'test', shape: { firstname: z.string(), lastname: z.string(), amount: z.number(), }, extend: (data) => ({ fullname: `${data.firstname} ${data.lastname}` }), }) it("class definition should be correct", () => { expect(TestClass).toBeDefined() expect(TestClass.create).toBeDefined() expect(TestClass.new).toBeDefined() expect(TestClass.unsafe).toBeDefined() expect(TestClass.shape).toBeDefined() expect(TestClass.shape.firstname).toBeDefined() expect(TestClass.shape.lastname).toBeDefined() expect(TestClass.shape.amount).toBeDefined() expect(TestClass.shape.id).toBeDefined() expect(TestClass.shape.createdAt).toBeDefined() expect(TestClass.shape.updatedAt).toBeDefined() }) it("create with new fn should work properly", () => { const e = TestClass.new({ id:"random-id", firstname: "John", lastname: "Doe", amount: 6, }) expect(isZentity(e)).toBe(true) expect(e.id).toBe("random-id") expect(e.createdAt).toBeInstanceOf(Date) expect(e.updatedAt).toBeInstanceOf(Date) expect(e.firstname).toBe("John") expect(e.lastname).toBe("Doe") expect(e.amount).toBe(6) expect(e.fullname).toBe("John Doe") expect(e.copy).toBeDefined() expect(e.validate).toBeDefined() expect(e.validate).toBeDefined() }) it("create with create fn should work properly", () => { const e = TestClass.create({ firstname: "John", lastname: "Doe", amount: 6, id: 'some id', createdAt: new Date(), updatedAt: new Date() }) expect(isZentity(e)).toBe(true) expect(e.id).toBe("some id") expect(e.createdAt).toBeInstanceOf(Date) expect(e.updatedAt).toBeInstanceOf(Date) expect(e.firstname).toBe("John") expect(e.lastname).toBe("Doe") expect(e.amount).toBe(6) expect(e.fullname).toBe("John Doe") expect(e.copy).toBeDefined() expect(e.validate).toBeDefined() }) it("create with unsafe fn should work properly", () => { const unsafe = TestClass.unsafe({} as any) expect(isZentity(unsafe)).toBe(false) expect(() => unsafe.validate()).toThrowError(ZodError) const validCopy = unsafe.copy({ id: 'some', createdAt: new Date(), updatedAt: new Date(), firstname: 'John', lastname: 'Doe', amount: 5 }).copy({amount: 6}) expect(validCopy.id).toBe("some") expect(validCopy.createdAt).toBeInstanceOf(Date) expect(validCopy.updatedAt).toBeInstanceOf(Date) expect(validCopy.firstname).toBe("John") expect(validCopy.lastname).toBe("Doe") expect(validCopy.amount).toBe(6) expect(validCopy.fullname).toBe("John Doe") expect(validCopy.copy).toBeDefined() expect(validCopy.validate).toBeDefined() }) it("copy should work properly", () => { const e = TestClass.new({ id:"random-id", firstname: "John", lastname: "Doe", amount: 6, }) const e2 = e.copy({amount: 10, firstname: "Jane", lastname: "Doey"}) expect(isZentity(e)).toBe(true) expect(isZentity(e2)).toBe(true) expect(e.id).toBe(e2.id) expect(e.createdAt).toStrictEqual(e2.createdAt) expect(e.updatedAt).toStrictEqual(e2.updatedAt) expect(e.firstname).toBe("John") expect(e.lastname).toBe("Doe") expect(e.amount).toBe(6) expect(e.fullname).toBe("John Doe") expect(e2.firstname).toBe("Jane") expect(e2.lastname).toBe("Doey") expect(e2.amount).toBe(10) expect(e2.fullname).toBe("Jane Doey") }) it("should have proper shape", () => { const shape = TestClass.shape expect(Object.keys(shape)).toStrictEqual(["id", "createdAt", "updatedAt", "firstname", "lastname", "amount"]) expect(shape.id).toBeInstanceOf(ZodString) expect(shape.createdAt).toBeInstanceOf(ZodDate) expect(shape.updatedAt).toBeInstanceOf(ZodDate) expect(shape.firstname).toBeInstanceOf(ZodString) expect(shape.lastname).toBeInstanceOf(ZodString) expect(shape.amount).toBeInstanceOf(ZodNumber) }) it("should have proper schema", ()=>{ const schema = TestClass.schema expect(schema).toBeDefined() expect(schema).toBeInstanceOf(ZodObject) const shape = schema.shape expect(Object.keys(shape)).toStrictEqual(["id", "createdAt", "updatedAt", "firstname", "lastname", "amount"]) expect(shape.id).toBeInstanceOf(ZodString) expect(shape.createdAt).toBeInstanceOf(ZodDate) expect(shape.updatedAt).toBeInstanceOf(ZodDate) expect(shape.firstname).toBeInstanceOf(ZodString) expect(shape.lastname).toBeInstanceOf(ZodString) expect(shape.amount).toBeInstanceOf(ZodNumber) }) it("toPlain should work properly", () => { const e = TestClass.new({ id:"random-id", firstname: "John", lastname: "Doe", amount: 6, }) const plain = e.toPlain() expect(plain.id).toBe(e.id) expect(plain.createdAt).toStrictEqual(e.createdAt) expect(plain.updatedAt).toStrictEqual(e.updatedAt) expect(plain.firstname).toBe("John") expect(plain.lastname).toBe("Doe") expect(plain.amount).toBe(6) expect((plain as any).fullname).toBeUndefined() }) })