From a4c1c88138596366952fcd9cee6225ca95c3e015 Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sat, 28 Jun 2025 22:45:56 +0200 Subject: [PATCH] Refactor types tests. --- tests/model/types/array.test.ts | 490 +++++++++++++++------------ tests/model/types/boolean.test.ts | 210 +++++++----- tests/model/types/date.test.ts | 314 ++++++++++------- tests/model/types/decimal.test.ts | 214 +++++++----- tests/model/types/map.test.ts | 357 +++++++++++--------- tests/model/types/model.test.ts | 541 +++++++++++++++++------------- tests/model/types/numeric.test.ts | 219 +++++++----- tests/model/types/object.test.ts | 380 ++++++++++++--------- tests/model/types/string.test.ts | 217 +++++++----- 9 files changed, 1706 insertions(+), 1236 deletions(-) diff --git a/tests/model/types/array.test.ts b/tests/model/types/array.test.ts index 4acfc9d..46d3f18 100644 --- a/tests/model/types/array.test.ts +++ b/tests/model/types/array.test.ts @@ -18,218 +18,294 @@ describe("array type", () => { identifier: "id", }); - test("array type definition", () => { + test("definition", () => { const arrayType = s.property.array(s.property.model(testModel)); expect(arrayType.type).toBeInstanceOf(ArrayType); }); const testProperty = s.property.array(s.property.decimal()); - test("array type functions", () => { - expect(testProperty.type.serialize([12.547, 8, -52.11])).toEqual([ - "12.547", - "8", - "-52.11", - ]); - expect(testProperty.type.deserialize(["12.547", "8", "-52.11"])).toEqual([ - 12.547, 8, -52.11, - ]); - - { - // Try to serialize the difference of an array with one changed model. - const propertyValue = [ - testModel.model( - Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), - ).instance, - testModel.model( - Object.assign(new TestModel(), { - id: 2, - name: "another", - price: 12.55, - }), - ).instance, - ]; - propertyValue[0].name = "new"; - expect( - s.property - .array(s.property.model(testModel)) - .type.serializeDiff(propertyValue), - ).toEqual([{id: 1, name: "new"}, {id: 2}]); - } - - expect(testProperty.type.serialize(null)).toBe(null); - expect(testProperty.type.deserialize(null)).toBe(null); - expect(testProperty.type.serializeDiff(null)).toBe(null); - - expect(testProperty.type.serialize(undefined)).toBe(undefined); - expect(testProperty.type.deserialize(undefined)).toBe(undefined); - expect(testProperty.type.serializeDiff(undefined)).toBe(undefined); - - expect( - testProperty.type.hasChanged([12.547, 8, -52.11], [12.547, 8, -52.11]), - ).toBeFalsy(); - expect(testProperty.type.hasChanged(null, null)).toBeFalsy(); - expect(testProperty.type.hasChanged(undefined, undefined)).toBeFalsy(); - expect(testProperty.type.hasChanged(null, undefined)).toBeTruthy(); - expect(testProperty.type.hasChanged(undefined, null)).toBeTruthy(); - expect( - testProperty.type.hasChanged(null, [12.547, 8, -52.11]), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged(undefined, [12.547, 8, -52.11]), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged([12.547, 8, -52.11], null), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged([12.547, 8, -52.11], undefined), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged([12.547, 8, -52.11], [12.547, -52.11, 8]), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged([12.547, -52.11, 8], [12.547, 8, -52.11]), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged([12.547, 8, -52.11], [12.547, 8]), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged([12.547, 8], [12.547, 8, -52.11]), - ).toBeTruthy(); - - expect( - testProperty.type.serializedHasChanged( - ["12.547", "8", "-52.11"], - ["12.547", "8", "-52.11"], - ), - ).toBeFalsy(); - expect(testProperty.type.serializedHasChanged(null, null)).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(null, ["12.547", "8", "-52.11"]), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(undefined, [ + describe("serialize", () => { + test("serialize", () => { + expect(testProperty.type.serialize([12.547, 8, -52.11])).toEqual([ "12.547", "8", "-52.11", - ]), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(["12.547", "8", "-52.11"], null), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - ["12.547", "8", "-52.11"], - undefined, - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - ["12.547", "8", "-52.11"], - ["12.547", "-52.11", "8"], - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - ["12.547", "-52.11", "8"], - ["12.547", "8", "-52.11"], - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - ["12.547", "8", "-52.11"], - ["12.547", "8"], - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - ["12.547", "8"], - ["12.547", "8", "-52.11"], - ), - ).toBeTruthy(); + ]); - { - // Try to reset the difference of an array with one changed model. - const propertyValue = [ - testModel.model( - Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), - ).instance, - testModel.model( - Object.assign(new TestModel(), { - id: 2, - name: "another", - price: 12.55, - }), - ).instance, - ]; - propertyValue[0].name = "new"; + expect(testProperty.type.serialize(null)).toBe(null); + expect(testProperty.type.serialize(undefined)).toBe(undefined); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.serialize({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + describe("deserialize", () => { + test("deserialize", () => { + expect(testProperty.type.deserialize(["12.547", "8", "-52.11"])).toEqual([ + 12.547, 8, -52.11, + ]); + + expect(testProperty.type.deserialize(null)).toBe(null); + expect(testProperty.type.deserialize(undefined)).toBe(undefined); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.deserialize({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + describe("serializeDiff", () => { + test("serializeDiff", () => { + { + // Try to serialize the difference of an array with one changed model. + const propertyValue = [ + testModel.model( + Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), + ).instance, + testModel.model( + Object.assign(new TestModel(), { + id: 2, + name: "another", + price: 12.55, + }), + ).instance, + ]; + propertyValue[0].name = "new"; + expect( + s.property + .array(s.property.model(testModel)) + .type.serializeDiff(propertyValue), + ).toEqual([{id: 1, name: "new"}, {id: 2}]); + } + + expect(testProperty.type.serializeDiff(null)).toBe(null); + expect(testProperty.type.serializeDiff(undefined)).toBe(undefined); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.serializeDiff({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + describe("hasChanged", () => { + test("hasChanged", () => { expect( + testProperty.type.hasChanged([12.547, 8, -52.11], [12.547, 8, -52.11]), + ).toBeFalsy(); + expect(testProperty.type.hasChanged(null, null)).toBeFalsy(); + expect(testProperty.type.hasChanged(undefined, undefined)).toBeFalsy(); + expect(testProperty.type.hasChanged(null, undefined)).toBeTruthy(); + expect(testProperty.type.hasChanged(undefined, null)).toBeTruthy(); + expect( + testProperty.type.hasChanged(null, [12.547, 8, -52.11]), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged(undefined, [12.547, 8, -52.11]), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged([12.547, 8, -52.11], null), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged([12.547, 8, -52.11], undefined), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged([12.547, 8, -52.11], [12.547, -52.11, 8]), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged([12.547, -52.11, 8], [12.547, 8, -52.11]), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged([12.547, 8, -52.11], [12.547, 8]), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged([12.547, 8], [12.547, 8, -52.11]), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect(testProperty.type.hasChanged({} as any, {} as any)).toBeTruthy(); + expect( + testProperty.type.hasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + testProperty.type.serializedHasChanged( + ["12.547", "8", "-52.11"], + ["12.547", "8", "-52.11"], + ), + ).toBeFalsy(); + expect(testProperty.type.serializedHasChanged(null, null)).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(null, ["12.547", "8", "-52.11"]), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(undefined, [ + "12.547", + "8", + "-52.11", + ]), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(["12.547", "8", "-52.11"], null), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + ["12.547", "8", "-52.11"], + undefined, + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + ["12.547", "8", "-52.11"], + ["12.547", "-52.11", "8"], + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + ["12.547", "-52.11", "8"], + ["12.547", "8", "-52.11"], + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + ["12.547", "8", "-52.11"], + ["12.547", "8"], + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + ["12.547", "8"], + ["12.547", "8", "-52.11"], + ), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + testProperty.type.serializedHasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + { + // Try to reset the difference of an array with one changed model. + const propertyValue = [ + testModel.model( + Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), + ).instance, + testModel.model( + Object.assign(new TestModel(), { + id: 2, + name: "another", + price: 12.55, + }), + ).instance, + ]; + propertyValue[0].name = "new"; + expect( + s.property + .array(s.property.model(testModel)) + .type.serializeDiff(propertyValue), + ).toEqual([{id: 1, name: "new"}, {id: 2}]); s.property .array(s.property.model(testModel)) - .type.serializeDiff(propertyValue), - ).toEqual([{id: 1, name: "new"}, {id: 2}]); - s.property - .array(s.property.model(testModel)) - .type.resetDiff(propertyValue); - expect( - s.property + .type.resetDiff(propertyValue); + expect( + s.property + .array(s.property.model(testModel)) + .type.serializeDiff(propertyValue), + ).toEqual([{id: 1}, {id: 2}]); + } + + testProperty.type.resetDiff(undefined); + testProperty.type.resetDiff(null); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.resetDiff({} as any)).not.toThrow(); + }); + }); + + describe("clone", () => { + test("clone", () => { + { + // Test that values are cloned in a different array. + const propertyValue = [12.547, 8, -52.11]; + const clonedPropertyValue = testProperty.type.clone(propertyValue); + expect(clonedPropertyValue).not.toBe(propertyValue); + expect(clonedPropertyValue).toEqual(propertyValue); + } + { + // Test that values are cloned recursively. + const propertyValue = [ + testModel.model( + Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), + ).instance, + testModel.model( + Object.assign(new TestModel(), { + id: 2, + name: "another", + price: 12.55, + }), + ).instance, + ]; + + // The arrays are different. + const clonedPropertyValue = s.property .array(s.property.model(testModel)) - .type.serializeDiff(propertyValue), - ).toEqual([{id: 1}, {id: 2}]); - } - testProperty.type.resetDiff(undefined); - testProperty.type.resetDiff(null); + .type.clone(propertyValue); + expect(clonedPropertyValue).not.toBe(propertyValue); - { - // Test that values are cloned in a different array. - const propertyValue = [12.547, 8, -52.11]; - const clonedPropertyValue = testProperty.type.clone(propertyValue); - expect(clonedPropertyValue).not.toBe(propertyValue); - expect(clonedPropertyValue).toEqual(propertyValue); - } - { - // Test that values are cloned recursively. - const propertyValue = [ - testModel.model( - Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), - ).instance, - testModel.model( - Object.assign(new TestModel(), { - id: 2, - name: "another", - price: 12.55, - }), - ).instance, - ]; + // Array values must be different objects but have the same values. + expect(clonedPropertyValue[0]).not.toBe(propertyValue[0]); + expect(clonedPropertyValue[1]).not.toBe(propertyValue[1]); + expect( + testModel.model(clonedPropertyValue[0]).getInstanceProperties(), + ).toEqual(testModel.model(propertyValue[0]).getInstanceProperties()); + expect( + testModel.model(clonedPropertyValue[1]).getInstanceProperties(), + ).toEqual(testModel.model(propertyValue[1]).getInstanceProperties()); + } - // The arrays are different. - const clonedPropertyValue = s.property - .array(s.property.model(testModel)) - .type.clone(propertyValue); - expect(clonedPropertyValue).not.toBe(propertyValue); + expect(testProperty.type.clone(undefined)).toBe(undefined); + expect(testProperty.type.clone(null)).toBe(null); + }); - // Array values must be different objects but have the same values. - expect(clonedPropertyValue[0]).not.toBe(propertyValue[0]); - expect(clonedPropertyValue[1]).not.toBe(propertyValue[1]); - expect( - testModel.model(clonedPropertyValue[0]).getInstanceProperties(), - ).toEqual(testModel.model(propertyValue[0]).getInstanceProperties()); - expect( - testModel.model(clonedPropertyValue[1]).getInstanceProperties(), - ).toEqual(testModel.model(propertyValue[1]).getInstanceProperties()); - } - expect(testProperty.type.clone(undefined)).toBe(undefined); - expect(testProperty.type.clone(null)).toBe(null); + test("invalid parameters", () => { + expect(() => testProperty.type.clone({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + test("applyPatch", () => { { // Test simple patch. expect( @@ -361,30 +437,4 @@ describe("array type", () => { }); } }); - - test("invalid parameters types", () => { - expect(() => testProperty.type.serialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.deserialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.serializeDiff({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.resetDiff({} as any)).not.toThrow(); - expect(testProperty.type.hasChanged({} as any, {} as any)).toBeTruthy(); - expect( - testProperty.type.hasChanged(false as any, false as any), - ).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(false as any, false as any), - ).toBeFalsy(); - expect(() => testProperty.type.clone({} as any)).toThrowError( - InvalidTypeValueError, - ); - }); }); diff --git a/tests/model/types/boolean.test.ts b/tests/model/types/boolean.test.ts index 9e14d65..39f3509 100644 --- a/tests/model/types/boolean.test.ts +++ b/tests/model/types/boolean.test.ts @@ -2,7 +2,7 @@ import {describe, expect, test} from "vitest"; import {BooleanType, s} from "../../../src/library"; describe("boolean type", () => { - test("boolean type definition", () => { + test("definition", () => { { const booleanType = s.property.boolean(); expect(booleanType.type).toBeInstanceOf(BooleanType); @@ -13,63 +13,144 @@ describe("boolean type", () => { } }); - test("boolean type functions", () => { - expect(s.property.boolean().type.serialize(false)).toBe(false); - expect(s.property.boolean().type.deserialize(false)).toBe(false); - expect(s.property.boolean().type.serializeDiff(true)).toBe(true); + describe("serialize", () => { + test("serialize", () => { + expect(s.property.boolean().type.serialize(false)).toBe(false); + expect(s.property.boolean().type.serialize(null)).toBe(null); + expect(s.property.boolean().type.serialize(undefined)).toBe(undefined); + }); - expect(s.property.boolean().type.serialize(null)).toBe(null); - expect(s.property.boolean().type.deserialize(null)).toBe(null); - expect(s.property.boolean().type.serializeDiff(null)).toBe(null); + test("invalid parameters", () => { + expect(s.property.boolean().type.serialize(1 as any)).toBeTruthy(); + expect(s.property.boolean().type.serialize(0 as any)).toBeFalsy(); + }); + }); - expect(s.property.boolean().type.serialize(undefined)).toBe(undefined); - expect(s.property.boolean().type.deserialize(undefined)).toBe(undefined); - expect(s.property.boolean().type.serializeDiff(undefined)).toBe(undefined); + describe("deserialize", () => { + test("deserialize", () => { + expect(s.property.boolean().type.deserialize(false)).toBe(false); + expect(s.property.boolean().type.deserialize(null)).toBe(null); + expect(s.property.boolean().type.deserialize(undefined)).toBe(undefined); + }); - expect(s.property.boolean().type.hasChanged(true, true)).toBeFalsy(); - expect(s.property.boolean().type.hasChanged(null, null)).toBeFalsy(); - expect( - s.property.boolean().type.hasChanged(undefined, undefined), - ).toBeFalsy(); - expect(s.property.boolean().type.hasChanged(null, undefined)).toBeTruthy(); - expect(s.property.boolean().type.hasChanged(undefined, null)).toBeTruthy(); - expect(s.property.boolean().type.hasChanged(null, false)).toBeTruthy(); - expect(s.property.boolean().type.hasChanged(undefined, false)).toBeTruthy(); - expect(s.property.boolean().type.hasChanged(false, null)).toBeTruthy(); - expect(s.property.boolean().type.hasChanged(false, undefined)).toBeTruthy(); + test("invalid parameters", () => { + expect(s.property.boolean().type.deserialize(1 as any)).toBeTruthy(); + expect(s.property.boolean().type.deserialize(0 as any)).toBeFalsy(); + }); + }); - expect( - s.property.boolean().type.serializedHasChanged(false, false), - ).toBeFalsy(); - expect( - s.property.boolean().type.serializedHasChanged(null, null), - ).toBeFalsy(); - expect( - s.property.boolean().type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.boolean().type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.boolean().type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.boolean().type.serializedHasChanged(null, false), - ).toBeTruthy(); - expect( - s.property.boolean().type.serializedHasChanged(undefined, false), - ).toBeTruthy(); - expect( - s.property.boolean().type.serializedHasChanged(false, null), - ).toBeTruthy(); - expect( - s.property.boolean().type.serializedHasChanged(false, undefined), - ).toBeTruthy(); + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect(s.property.boolean().type.serializeDiff(true)).toBe(true); + expect(s.property.boolean().type.serializeDiff(null)).toBe(null); + expect(s.property.boolean().type.serializeDiff(undefined)).toBe( + undefined, + ); + }); - s.property.boolean().type.resetDiff(false); - s.property.boolean().type.resetDiff(undefined); - s.property.boolean().type.resetDiff(null); + test("invalid parameters", () => { + expect(s.property.boolean().type.serializeDiff(1 as any)).toBeTruthy(); + expect(s.property.boolean().type.serializeDiff(0 as any)).toBeFalsy(); + }); + }); + describe("hasChanged", () => { + test("hasChanged", () => { + expect(s.property.boolean().type.hasChanged(true, true)).toBeFalsy(); + expect(s.property.boolean().type.hasChanged(null, null)).toBeFalsy(); + expect( + s.property.boolean().type.hasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.boolean().type.hasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.boolean().type.hasChanged(undefined, null), + ).toBeTruthy(); + expect(s.property.boolean().type.hasChanged(null, false)).toBeTruthy(); + expect( + s.property.boolean().type.hasChanged(undefined, false), + ).toBeTruthy(); + expect(s.property.boolean().type.hasChanged(false, null)).toBeTruthy(); + expect( + s.property.boolean().type.hasChanged(false, undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.boolean().type.hasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property.boolean().type.hasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + s.property.boolean().type.serializedHasChanged(false, false), + ).toBeFalsy(); + expect( + s.property.boolean().type.serializedHasChanged(null, null), + ).toBeFalsy(); + expect( + s.property.boolean().type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.boolean().type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.boolean().type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property.boolean().type.serializedHasChanged(null, false), + ).toBeTruthy(); + expect( + s.property.boolean().type.serializedHasChanged(undefined, false), + ).toBeTruthy(); + expect( + s.property.boolean().type.serializedHasChanged(false, null), + ).toBeTruthy(); + expect( + s.property.boolean().type.serializedHasChanged(false, undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.boolean().type.serializedHasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property + .boolean() + .type.serializedHasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + s.property.boolean().type.resetDiff(false); + s.property.boolean().type.resetDiff(undefined); + s.property.boolean().type.resetDiff(null); + }); + + test("resetDiff", () => { + expect(() => + s.property.boolean().type.resetDiff({} as any), + ).not.toThrow(); + }); + }); + + describe("clone", () => { + test("invalid parameters", () => { + expect(s.property.boolean().type.clone({} as any)).toStrictEqual({}); + }); + }); + + test("applyPatch", () => { expect( s.property.boolean().type.applyPatch(false, true, true), ).toBeTruthy(); @@ -91,29 +172,4 @@ describe("boolean type", () => { s.property.boolean().type.applyPatch(null, false, false), ).toBeFalsy(); }); - - test("invalid parameters types", () => { - expect(s.property.boolean().type.serialize(1 as any)).toBeTruthy(); - expect(s.property.boolean().type.serialize(0 as any)).toBeFalsy(); - expect(s.property.boolean().type.deserialize(1 as any)).toBeTruthy(); - expect(s.property.boolean().type.deserialize(0 as any)).toBeFalsy(); - expect(s.property.boolean().type.serializeDiff(1 as any)).toBeTruthy(); - expect(s.property.boolean().type.serializeDiff(0 as any)).toBeFalsy(); - expect(() => s.property.boolean().type.resetDiff({} as any)).not.toThrow(); - expect( - s.property.boolean().type.hasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.boolean().type.hasChanged(false as any, false as any), - ).toBeFalsy(); - expect( - s.property.boolean().type.serializedHasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property - .boolean() - .type.serializedHasChanged(false as any, false as any), - ).toBeFalsy(); - expect(s.property.boolean().type.clone({} as any)).toStrictEqual({}); - }); }); diff --git a/tests/model/types/date.test.ts b/tests/model/types/date.test.ts index 6ff31be..49ae45c 100644 --- a/tests/model/types/date.test.ts +++ b/tests/model/types/date.test.ts @@ -4,117 +4,202 @@ import {DateType, InvalidTypeValueError, s} from "../../../src/library"; describe("date type", () => { const testDate = new Date(); - test("date type definition", () => { + test("definition", () => { const dateType = s.property.date(); expect(dateType.type).toBeInstanceOf(DateType); }); - test("date type functions", () => { - expect(s.property.date().type.serialize(testDate)).toBe( - testDate.toISOString(), - ); - expect( - s.property.date().type.deserialize(testDate.toISOString())?.getTime(), - ).toBe(testDate.getTime()); - expect(s.property.date().type.serializeDiff(new Date(testDate))).toBe( - testDate.toISOString(), - ); - expect( - s.property - .date() - .type.deserialize("2565152-2156121-256123121 5121544175:21515612") - .valueOf(), - ).toBeNaN(); - expect(s.property.date().type.serialize(new Date(NaN))).toBe( - new Date(NaN).toString(), - ); + describe("serialize", () => { + test("serialize", () => { + expect(s.property.date().type.serialize(testDate)).toBe( + testDate.toISOString(), + ); - expect(s.property.date().type.serialize(null)).toBe(null); - expect(s.property.date().type.deserialize(null)).toBe(null); - expect(s.property.date().type.serializeDiff(null)).toBe(null); + expect(s.property.date().type.serialize(new Date(NaN))).toBe( + new Date(NaN).toString(), + ); - expect(s.property.date().type.serialize(undefined)).toBe(undefined); - expect(s.property.date().type.deserialize(undefined)).toBe(undefined); - expect(s.property.date().type.serializeDiff(undefined)).toBe(undefined); + expect(s.property.date().type.serialize(null)).toBe(null); + expect(s.property.date().type.serialize(undefined)).toBe(undefined); + }); - expect( - s.property.date().type.hasChanged(testDate, new Date(testDate)), - ).toBeFalsy(); - expect(s.property.date().type.hasChanged(null, null)).toBeFalsy(); - expect(s.property.date().type.hasChanged(undefined, undefined)).toBeFalsy(); - expect(s.property.date().type.hasChanged(null, undefined)).toBeTruthy(); - expect(s.property.date().type.hasChanged(undefined, null)).toBeTruthy(); - expect(s.property.date().type.hasChanged(null, testDate)).toBeTruthy(); - expect(s.property.date().type.hasChanged(undefined, testDate)).toBeTruthy(); - expect(s.property.date().type.hasChanged(testDate, null)).toBeTruthy(); - expect(s.property.date().type.hasChanged(new Date(NaN), null)).toBeTruthy(); - expect( - s.property.date().type.hasChanged(new Date(NaN), undefined), - ).toBeTruthy(); - expect( - s.property.date().type.hasChanged(new Date(NaN), new Date(NaN)), - ).toBeFalsy(); + test("invalid parameters", () => { + expect(() => s.property.date().type.serialize({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); - expect( - s.property - .date() - .type.serializedHasChanged( - testDate.toISOString(), - new Date(testDate).toISOString(), - ), - ).toBeFalsy(); - expect(s.property.date().type.serializedHasChanged(null, null)).toBeFalsy(); - expect( - s.property.date().type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.date().type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.date().type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.date().type.serializedHasChanged(null, testDate.toISOString()), - ).toBeTruthy(); - expect( - s.property - .date() - .type.serializedHasChanged(undefined, testDate.toISOString()), - ).toBeTruthy(); - expect( - s.property.date().type.serializedHasChanged(testDate.toISOString(), null), - ).toBeTruthy(); - expect( - s.property - .date() - .type.serializedHasChanged(new Date(NaN).toString(), null), - ).toBeTruthy(); - expect( - s.property - .date() - .type.serializedHasChanged(new Date(NaN).toString(), undefined), - ).toBeTruthy(); - expect( - s.property - .date() - .type.serializedHasChanged( - new Date(NaN).toString(), - new Date(NaN).toString(), - ), - ).toBeFalsy(); + describe("deserialize", () => { + test("deserialize", () => { + expect( + s.property.date().type.deserialize(testDate.toISOString())?.getTime(), + ).toBe(testDate.getTime()); - s.property.date().type.resetDiff(testDate); - s.property.date().type.resetDiff(undefined); - s.property.date().type.resetDiff(null); + expect( + s.property + .date() + .type.deserialize("2565152-2156121-256123121 5121544175:21515612") + .valueOf(), + ).toBeNaN(); - { - // Test that the date is cloned in a different object. - const propertyValue = new Date(); - const clonedPropertyValue = s.property.date().type.clone(propertyValue); - expect(clonedPropertyValue).not.toBe(propertyValue); - expect(clonedPropertyValue).toEqual(propertyValue); - } + expect(s.property.date().type.deserialize(null)).toBe(null); + expect(s.property.date().type.deserialize(undefined)).toBe(undefined); + }); + test("invalid parameters", () => { + expect( + s.property + .date() + .type.deserialize({} as any) + .getTime(), + ).toBe(NaN); + }); + }); + + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect(s.property.date().type.serializeDiff(new Date(testDate))).toBe( + testDate.toISOString(), + ); + + expect(s.property.date().type.serializeDiff(null)).toBe(null); + expect(s.property.date().type.serializeDiff(undefined)).toBe(undefined); + }); + + test("invalid parameters", () => { + expect(() => + s.property.date().type.serializeDiff({} as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("hasChanged", () => { + test("hasChanged", () => { + expect( + s.property.date().type.hasChanged(testDate, new Date(testDate)), + ).toBeFalsy(); + expect(s.property.date().type.hasChanged(null, null)).toBeFalsy(); + expect( + s.property.date().type.hasChanged(undefined, undefined), + ).toBeFalsy(); + expect(s.property.date().type.hasChanged(null, undefined)).toBeTruthy(); + expect(s.property.date().type.hasChanged(undefined, null)).toBeTruthy(); + expect(s.property.date().type.hasChanged(null, testDate)).toBeTruthy(); + expect( + s.property.date().type.hasChanged(undefined, testDate), + ).toBeTruthy(); + expect(s.property.date().type.hasChanged(testDate, null)).toBeTruthy(); + expect( + s.property.date().type.hasChanged(new Date(NaN), null), + ).toBeTruthy(); + expect( + s.property.date().type.hasChanged(new Date(NaN), undefined), + ).toBeTruthy(); + expect( + s.property.date().type.hasChanged(new Date(NaN), new Date(NaN)), + ).toBeFalsy(); + }); + + test("invalid parameters", () => { + expect( + s.property.date().type.hasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property.date().type.hasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + s.property + .date() + .type.serializedHasChanged( + testDate.toISOString(), + new Date(testDate).toISOString(), + ), + ).toBeFalsy(); + expect( + s.property.date().type.serializedHasChanged(null, null), + ).toBeFalsy(); + expect( + s.property.date().type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.date().type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.date().type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property + .date() + .type.serializedHasChanged(null, testDate.toISOString()), + ).toBeTruthy(); + expect( + s.property + .date() + .type.serializedHasChanged(undefined, testDate.toISOString()), + ).toBeTruthy(); + expect( + s.property + .date() + .type.serializedHasChanged(testDate.toISOString(), null), + ).toBeTruthy(); + expect( + s.property + .date() + .type.serializedHasChanged(new Date(NaN).toString(), null), + ).toBeTruthy(); + expect( + s.property + .date() + .type.serializedHasChanged(new Date(NaN).toString(), undefined), + ).toBeTruthy(); + expect( + s.property + .date() + .type.serializedHasChanged( + new Date(NaN).toString(), + new Date(NaN).toString(), + ), + ).toBeFalsy(); + }); + + test("invalid parameters", () => { + expect( + s.property.date().type.serializedHasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property.date().type.serializedHasChanged(false as any, false as any), + ).toBeFalsy(); + expect(s.property.date().type.clone({} as any)).toStrictEqual({}); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + s.property.date().type.resetDiff(testDate); + s.property.date().type.resetDiff(undefined); + s.property.date().type.resetDiff(null); + }); + + test("invalid parameters", () => { + expect(() => s.property.date().type.resetDiff({} as any)).not.toThrow(); + }); + }); + + test("clone", () => { + // Test that the date is cloned in a different object. + const propertyValue = new Date(); + const clonedPropertyValue = s.property.date().type.clone(propertyValue); + expect(clonedPropertyValue).not.toBe(propertyValue); + expect(clonedPropertyValue).toEqual(propertyValue); + }); + + test("applyPatch", () => { expect( s.property .date() @@ -144,33 +229,4 @@ describe("date type", () => { s.property.date().type.applyPatch(new Date(), null, false), ).toBeNull(); }); - - test("invalid parameters types", () => { - expect(() => s.property.date().type.serialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect( - s.property - .date() - .type.deserialize({} as any) - .getTime(), - ).toBe(NaN); - expect(() => s.property.date().type.serializeDiff({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => s.property.date().type.resetDiff({} as any)).not.toThrow(); - expect( - s.property.date().type.hasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.date().type.hasChanged(false as any, false as any), - ).toBeFalsy(); - expect( - s.property.date().type.serializedHasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.date().type.serializedHasChanged(false as any, false as any), - ).toBeFalsy(); - expect(s.property.date().type.clone({} as any)).toStrictEqual({}); - }); }); diff --git a/tests/model/types/decimal.test.ts b/tests/model/types/decimal.test.ts index 97fddd7..8aac650 100644 --- a/tests/model/types/decimal.test.ts +++ b/tests/model/types/decimal.test.ts @@ -7,63 +7,148 @@ describe("decimal type", () => { expect(decimalType.type).toBeInstanceOf(DecimalType); }); - test("decimal type functions", () => { - expect(s.property.decimal().type.serialize(5.257)).toBe("5.257"); - expect(s.property.decimal().type.deserialize("5.257")).toBe(5.257); - expect(s.property.decimal().type.serializeDiff(542)).toBe("542"); + describe("serialize", () => { + test("serialize", () => { + expect(s.property.decimal().type.serialize(5.257)).toBe("5.257"); - expect(s.property.decimal().type.serialize(null)).toBe(null); - expect(s.property.decimal().type.deserialize(null)).toBe(null); - expect(s.property.decimal().type.serializeDiff(null)).toBe(null); + expect(s.property.decimal().type.serialize(null)).toBe(null); + expect(s.property.decimal().type.serialize(undefined)).toBe(undefined); + }); - expect(s.property.decimal().type.serialize(undefined)).toBe(undefined); - expect(s.property.decimal().type.deserialize(undefined)).toBe(undefined); - expect(s.property.decimal().type.serializeDiff(undefined)).toBe(undefined); + test("invalid parameters", () => { + expect(() => s.property.decimal().type.serialize({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); - expect(s.property.decimal().type.hasChanged(5.257, 5.257)).toBeFalsy(); - expect(s.property.decimal().type.hasChanged(null, null)).toBeFalsy(); - expect( - s.property.decimal().type.hasChanged(undefined, undefined), - ).toBeFalsy(); - expect(s.property.decimal().type.hasChanged(null, undefined)).toBeTruthy(); - expect(s.property.decimal().type.hasChanged(undefined, null)).toBeTruthy(); - expect(s.property.decimal().type.hasChanged(null, 5.257)).toBeTruthy(); - expect(s.property.decimal().type.hasChanged(undefined, 5.257)).toBeTruthy(); - expect(s.property.decimal().type.hasChanged(5.257, null)).toBeTruthy(); - expect(s.property.decimal().type.hasChanged(5.257, undefined)).toBeTruthy(); + describe("deserialize", () => { + test("deserialize", () => { + expect(s.property.decimal().type.deserialize("5.257")).toBe(5.257); + expect(s.property.decimal().type.deserialize(null)).toBe(null); + expect(s.property.decimal().type.deserialize(undefined)).toBe(undefined); + }); - expect( - s.property.decimal().type.serializedHasChanged("5.257", "5.257"), - ).toBeFalsy(); - expect( - s.property.decimal().type.serializedHasChanged(null, null), - ).toBeFalsy(); - expect( - s.property.decimal().type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.decimal().type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.decimal().type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.decimal().type.serializedHasChanged(null, "5.257"), - ).toBeTruthy(); - expect( - s.property.decimal().type.serializedHasChanged(undefined, "5.257"), - ).toBeTruthy(); - expect( - s.property.decimal().type.serializedHasChanged("5.257", null), - ).toBeTruthy(); - expect( - s.property.decimal().type.serializedHasChanged("5.257", undefined), - ).toBeTruthy(); + test("invalid parameters", () => { + expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN); + expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN); + }); + }); - s.property.decimal().type.resetDiff(5.257); - s.property.decimal().type.resetDiff(undefined); - s.property.decimal().type.resetDiff(null); + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect(s.property.decimal().type.serializeDiff(542)).toBe("542"); + expect(s.property.decimal().type.serializeDiff(null)).toBe(null); + expect(s.property.decimal().type.serializeDiff(undefined)).toBe( + undefined, + ); + }); + + test("invalid parameters", () => { + expect(() => + s.property.decimal().type.serializeDiff({} as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + s.property.decimal().type.resetDiff(5.257); + s.property.decimal().type.resetDiff(undefined); + s.property.decimal().type.resetDiff(null); + }); + + test("invalid parameters", () => { + expect(() => + s.property.decimal().type.resetDiff({} as any), + ).not.toThrow(); + }); + }); + + describe("hasChanged", () => { + test("hasChanged", () => { + expect(s.property.decimal().type.hasChanged(5.257, 5.257)).toBeFalsy(); + expect(s.property.decimal().type.hasChanged(null, null)).toBeFalsy(); + expect( + s.property.decimal().type.hasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.decimal().type.hasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.decimal().type.hasChanged(undefined, null), + ).toBeTruthy(); + expect(s.property.decimal().type.hasChanged(null, 5.257)).toBeTruthy(); + expect( + s.property.decimal().type.hasChanged(undefined, 5.257), + ).toBeTruthy(); + expect(s.property.decimal().type.hasChanged(5.257, null)).toBeTruthy(); + expect( + s.property.decimal().type.hasChanged(5.257, undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.decimal().type.hasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property.decimal().type.hasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + s.property.decimal().type.serializedHasChanged("5.257", "5.257"), + ).toBeFalsy(); + expect( + s.property.decimal().type.serializedHasChanged(null, null), + ).toBeFalsy(); + expect( + s.property.decimal().type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.decimal().type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.decimal().type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property.decimal().type.serializedHasChanged(null, "5.257"), + ).toBeTruthy(); + expect( + s.property.decimal().type.serializedHasChanged(undefined, "5.257"), + ).toBeTruthy(); + expect( + s.property.decimal().type.serializedHasChanged("5.257", null), + ).toBeTruthy(); + expect( + s.property.decimal().type.serializedHasChanged("5.257", undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.decimal().type.serializedHasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property + .decimal() + .type.serializedHasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("clone", () => { + test("invalid parameters", () => { + expect(s.property.decimal().type.clone({} as any)).toStrictEqual({}); + }); + }); + + test("applyPatch", () => { expect(s.property.decimal().type.applyPatch(1, "5.257", false)).toBe(5.257); expect(s.property.decimal().type.applyPatch(undefined, "5.257", true)).toBe( 5.257, @@ -76,31 +161,4 @@ describe("decimal type", () => { ).toBeUndefined(); expect(s.property.decimal().type.applyPatch(5.257, null, false)).toBeNull(); }); - - test("invalid parameters types", () => { - expect(() => s.property.decimal().type.serialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN); - expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN); - expect(() => - s.property.decimal().type.serializeDiff({} as any), - ).toThrowError(InvalidTypeValueError); - expect(() => s.property.decimal().type.resetDiff({} as any)).not.toThrow(); - expect( - s.property.decimal().type.hasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.decimal().type.hasChanged(false as any, false as any), - ).toBeFalsy(); - expect( - s.property.decimal().type.serializedHasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property - .decimal() - .type.serializedHasChanged(false as any, false as any), - ).toBeFalsy(); - expect(s.property.decimal().type.clone({} as any)).toStrictEqual({}); - }); }); diff --git a/tests/model/types/map.test.ts b/tests/model/types/map.test.ts index 1e021d9..8a1dc85 100644 --- a/tests/model/types/map.test.ts +++ b/tests/model/types/map.test.ts @@ -1,14 +1,9 @@ import {describe, expect, test} from "vitest"; -import { - InvalidTypeValueError, - NumericType, - s, - StringType, -} from "../../../src/library"; +import {InvalidTypeValueError, s} from "../../../src/library"; import {MapType} from "../../../src/model/types/map"; describe("map type", () => { - test("map type definition", () => { + test("definition", () => { const mapType = s.property.map(s.property.string(), s.property.numeric()); expect(mapType.type).toBeInstanceOf(MapType); }); @@ -21,135 +16,215 @@ describe("map type", () => { testMapValue.set("test", 1.52); testMapValue.set("another", 55); - test("object type functions", () => { - expect(testProperty.type.serialize(testMapValue)).toEqual({ - test: "1.52", - another: "55", - }); - expect( - testProperty.type.deserialize({ + describe("serialize", () => { + test("serialize", () => { + expect(testProperty.type.serialize(testMapValue)).toEqual({ test: "1.52", another: "55", - }), - ).toEqual(testMapValue); - expect(testProperty.type.serializeDiff(testMapValue)).toEqual({ - test: "1.52", - another: "55", + }); + + expect(testProperty.type.serialize(null)).toEqual(null); + expect(testProperty.type.serialize(undefined)).toEqual(undefined); }); - expect(testProperty.type.serialize(null)).toEqual(null); - expect(testProperty.type.deserialize(null)).toEqual(null); - expect(testProperty.type.serializeDiff(null)).toEqual(null); + test("invalid parameters", () => { + expect(() => testProperty.type.serialize(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.serialize([] as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.serialize({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); - expect(testProperty.type.serialize(undefined)).toEqual(undefined); - expect(testProperty.type.deserialize(undefined)).toEqual(undefined); - expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined); + describe("deserialize", () => { + test("deserialize", () => { + expect( + testProperty.type.deserialize({ + test: "1.52", + another: "55", + }), + ).toEqual(testMapValue); - const anotherTestMapValue = new Map(); - anotherTestMapValue.set("test", 1.52); - anotherTestMapValue.set("another", 55); - expect( - testProperty.type.hasChanged(testMapValue, anotherTestMapValue), - ).toBeFalsy(); - anotherTestMapValue.set("test", 1.521); - expect( - testProperty.type.hasChanged(testMapValue, anotherTestMapValue), - ).toBeTruthy(); - anotherTestMapValue.delete("test"); - expect( - testProperty.type.hasChanged(testMapValue, anotherTestMapValue), - ).toBeTruthy(); - expect(testProperty.type.hasChanged(null, null)).toBeFalsy(); - expect(testProperty.type.hasChanged(undefined, undefined)).toBeFalsy(); - expect(testProperty.type.hasChanged(null, undefined)).toBeTruthy(); - expect(testProperty.type.hasChanged(undefined, null)).toBeTruthy(); - expect(testProperty.type.hasChanged(null, testMapValue)).toBeTruthy(); - expect(testProperty.type.hasChanged(undefined, testMapValue)).toBeTruthy(); - expect(testProperty.type.hasChanged(testMapValue, null)).toBeTruthy(); - expect(testProperty.type.hasChanged(testMapValue, undefined)).toBeTruthy(); + expect(testProperty.type.deserialize(null)).toEqual(null); + expect(testProperty.type.deserialize(undefined)).toEqual(undefined); + }); - expect( - testProperty.type.serializedHasChanged( - {test: "1.52", another: "55"}, - {test: "1.52", another: "55"}, - ), - ).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged( - {test: "1.52", another: "55"}, - {test: "1.521", another: "55"}, - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - {test: "1.52", another: "55"}, - {another: "55"}, - ), - ).toBeTruthy(); - expect(testProperty.type.serializedHasChanged(null, null)).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(null, { + test("invalid parameters", () => { + expect(() => testProperty.type.deserialize(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.deserialize([] as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect(testProperty.type.serializeDiff(testMapValue)).toEqual({ test: "1.52", another: "55", - }), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(undefined, { - test: "1.52", - another: "55", - }), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - {test: "1.52", another: "55"}, - null, - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - {test: "1.52", another: "55"}, - undefined, - ), - ).toBeTruthy(); + }); - testProperty.type.resetDiff(testMapValue); - testProperty.type.resetDiff(undefined); - testProperty.type.resetDiff(null); + expect(testProperty.type.serializeDiff(null)).toEqual(null); + expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined); + }); - { - // Test that keys and values are cloned in a different map. - const clonedTestMapValue = testProperty.type.clone(testMapValue); - expect(clonedTestMapValue).not.toBe(testMapValue); - expect(clonedTestMapValue).toEqual(testMapValue); - } - { - // Test that values are cloned in a different object. - const propertyValue = new Map(); - propertyValue.set("test", [12, 11]); - const clonedPropertyValue = s.property - .stringMap(s.property.array(s.property.numeric())) - .type.clone(propertyValue); - expect(clonedPropertyValue).not.toBe(propertyValue); - expect(clonedPropertyValue).toEqual(propertyValue); - expect(clonedPropertyValue.get("test")).not.toBe( - propertyValue.get("test"), + test("invalid parameters", () => { + expect(() => testProperty.type.serializeDiff(5 as any)).toThrowError( + InvalidTypeValueError, ); - expect(clonedPropertyValue.get("test")).toEqual( - propertyValue.get("test"), + expect(() => testProperty.type.serializeDiff([] as any)).toThrowError( + InvalidTypeValueError, ); - } - expect(testProperty.type.clone(undefined)).toBe(undefined); - expect(testProperty.type.clone(null)).toBe(null); + expect(() => testProperty.type.serializeDiff({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + describe("hasChanged", () => { + test("hasChanged", () => { + const anotherTestMapValue = new Map(); + anotherTestMapValue.set("test", 1.52); + anotherTestMapValue.set("another", 55); + expect( + testProperty.type.hasChanged(testMapValue, anotherTestMapValue), + ).toBeFalsy(); + anotherTestMapValue.set("test", 1.521); + expect( + testProperty.type.hasChanged(testMapValue, anotherTestMapValue), + ).toBeTruthy(); + anotherTestMapValue.delete("test"); + expect( + testProperty.type.hasChanged(testMapValue, anotherTestMapValue), + ).toBeTruthy(); + expect(testProperty.type.hasChanged(null, null)).toBeFalsy(); + expect(testProperty.type.hasChanged(undefined, undefined)).toBeFalsy(); + expect(testProperty.type.hasChanged(null, undefined)).toBeTruthy(); + expect(testProperty.type.hasChanged(undefined, null)).toBeTruthy(); + expect(testProperty.type.hasChanged(null, testMapValue)).toBeTruthy(); + expect( + testProperty.type.hasChanged(undefined, testMapValue), + ).toBeTruthy(); + expect(testProperty.type.hasChanged(testMapValue, null)).toBeTruthy(); + expect( + testProperty.type.hasChanged(testMapValue, undefined), + ).toBeTruthy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + testProperty.type.serializedHasChanged( + {test: "1.52", another: "55"}, + {test: "1.52", another: "55"}, + ), + ).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged( + {test: "1.52", another: "55"}, + {test: "1.521", another: "55"}, + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + {test: "1.52", another: "55"}, + {another: "55"}, + ), + ).toBeTruthy(); + expect(testProperty.type.serializedHasChanged(null, null)).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(null, { + test: "1.52", + another: "55", + }), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(undefined, { + test: "1.52", + another: "55", + }), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + {test: "1.52", another: "55"}, + null, + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + {test: "1.52", another: "55"}, + undefined, + ), + ).toBeTruthy(); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + testProperty.type.resetDiff(testMapValue); + testProperty.type.resetDiff(undefined); + testProperty.type.resetDiff(null); + }); + }); + + describe("clone", () => { + test("clone", () => { + { + // Test that keys and values are cloned in a different map. + const clonedTestMapValue = testProperty.type.clone(testMapValue); + expect(clonedTestMapValue).not.toBe(testMapValue); + expect(clonedTestMapValue).toEqual(testMapValue); + } + { + // Test that values are cloned in a different object. + const propertyValue = new Map(); + propertyValue.set("test", [12, 11]); + const clonedPropertyValue = s.property + .stringMap(s.property.array(s.property.numeric())) + .type.clone(propertyValue); + expect(clonedPropertyValue).not.toBe(propertyValue); + expect(clonedPropertyValue).toEqual(propertyValue); + expect(clonedPropertyValue.get("test")).not.toBe( + propertyValue.get("test"), + ); + expect(clonedPropertyValue.get("test")).toEqual( + propertyValue.get("test"), + ); + } + expect(testProperty.type.clone(undefined)).toBe(undefined); + expect(testProperty.type.clone(null)).toBe(null); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.clone(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.clone([] as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.clone({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + test("applyPatch", () => { { // Apply a patch with undefined / NULL values. expect( @@ -205,42 +280,4 @@ describe("map type", () => { } } }); - - test("invalid parameters types", () => { - expect(() => testProperty.type.serialize(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.deserialize(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.serializeDiff(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.clone(5 as any)).toThrowError( - InvalidTypeValueError, - ); - - expect(() => testProperty.type.serialize([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.deserialize([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.serializeDiff([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.clone([] as any)).toThrowError( - InvalidTypeValueError, - ); - - expect(() => testProperty.type.serialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.serializeDiff({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.clone({} as any)).toThrowError( - InvalidTypeValueError, - ); - }); }); diff --git a/tests/model/types/model.test.ts b/tests/model/types/model.test.ts index f199401..11a52ff 100644 --- a/tests/model/types/model.test.ts +++ b/tests/model/types/model.test.ts @@ -18,14 +18,13 @@ describe("model type", () => { identifier: "id", }); - test("model type definition", () => { + test("definition", () => { const modelType = s.property.model(testModel); expect(modelType.type).toBeInstanceOf(ModelType); }); - test("model type functions", () => { - { - // Try to serialize / deserialize. + describe("serialize", () => { + test("serialize", () => { const testModelInstance = testModel.model( Object.assign(new TestModel(), { id: 1, @@ -33,9 +32,40 @@ describe("model type", () => { price: 12.548777, }), ).instance; + expect( s.property.model(testModel).type.serialize(testModelInstance), ).toEqual({id: 1, name: "test", price: "12.548777"}); + + expect(s.property.model(testModel).type.serialize(null)).toEqual(null); + expect(s.property.model(testModel).type.serialize(undefined)).toEqual( + undefined, + ); + }); + + test("invalid parameters", () => { + expect(() => + s.property.model(testModel).type.serialize(5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.serialize([] as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.serialize(new (class {})() as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("deserialize", () => { + test("deserialize", () => { + const testModelInstance = testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance; + expect( testModel .model( @@ -45,9 +75,25 @@ describe("model type", () => { ) .getInstanceProperties(), ).toEqual(testModel.model(testModelInstance).getInstanceProperties()); - } - { + expect(s.property.model(testModel).type.deserialize(null)).toEqual(null); + expect(s.property.model(testModel).type.deserialize(undefined)).toEqual( + undefined, + ); + }); + + test("invalid parameters", () => { + expect(() => + s.property.model(testModel).type.deserialize(5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.deserialize([] as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("serializeDiff", () => { + test("serializeDiff", () => { // Try to serialize the difference. const testModelInstance = testModel.model( Object.assign(new TestModel(), { @@ -56,177 +102,234 @@ describe("model type", () => { price: 12.548777, }), ).instance; + testModelInstance.name = "new"; expect( s.property.model(testModel).type.serializeDiff(testModelInstance), ).toEqual({id: 1, name: "new"}); - } - expect(s.property.model(testModel).type.serialize(null)).toEqual(null); - expect(s.property.model(testModel).type.deserialize(null)).toEqual(null); - expect(s.property.model(testModel).type.serializeDiff(null)).toEqual(null); + expect(s.property.model(testModel).type.serializeDiff(null)).toEqual( + null, + ); + expect(s.property.model(testModel).type.serializeDiff(undefined)).toEqual( + undefined, + ); + }); - expect(s.property.model(testModel).type.serialize(undefined)).toEqual( - undefined, - ); - expect(s.property.model(testModel).type.deserialize(undefined)).toEqual( - undefined, - ); - expect(s.property.model(testModel).type.serializeDiff(undefined)).toEqual( - undefined, - ); + test("invalid parameters", () => { + expect(() => + s.property.model(testModel).type.serializeDiff(5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.serializeDiff([] as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.serializeDiff(new (class {})() as any), + ).toThrowError(InvalidTypeValueError); + }); + }); - { - const testModelInstance = testModel.model( - Object.assign(new TestModel(), { - id: 1, - name: "test", - price: 12.548777, - }), - ).instance; + describe("hasChanged", () => { + test("hasChanged", () => { + { + const testModelInstance = testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance; + expect( + s.property + .model(testModel) + .type.hasChanged(testModelInstance, testModelInstance), + ).toBeFalsy(); + } + { + const testModelInstance = testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance; + testModelInstance.price = 12.548778; + expect( + s.property + .model(testModel) + .type.hasChanged(testModelInstance, testModelInstance), + ).toBeTruthy(); + } expect( - s.property - .model(testModel) - .type.hasChanged(testModelInstance, testModelInstance), + s.property.model(testModel).type.hasChanged(null, null), ).toBeFalsy(); - } - { - const testModelInstance = testModel.model( - Object.assign(new TestModel(), { - id: 1, - name: "test", - price: 12.548777, - }), - ).instance; - testModelInstance.price = 12.548778; expect( - s.property - .model(testModel) - .type.hasChanged(testModelInstance, testModelInstance), + s.property.model(testModel).type.hasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.model(testModel).type.hasChanged(null, undefined), ).toBeTruthy(); - } - expect(s.property.model(testModel).type.hasChanged(null, null)).toBeFalsy(); - expect( - s.property.model(testModel).type.hasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.model(testModel).type.hasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.hasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.hasChanged( - null, - testModel.model( - Object.assign(new TestModel(), { - id: 1, - name: "test", - price: 12.548777, - }), - ).instance, - ), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.hasChanged( - undefined, - testModel.model( - Object.assign(new TestModel(), { - id: 1, - name: "test", - price: 12.548777, - }), - ).instance, - ), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.hasChanged( - testModel.model( - Object.assign(new TestModel(), { - id: 1, - name: "test", - price: 12.548777, - }), - ).instance, - null, - ), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.hasChanged( - testModel.model( - Object.assign(new TestModel(), { - id: 1, - name: "test", - price: 12.548777, - }), - ).instance, - undefined, - ), - ).toBeTruthy(); - - expect( - s.property - .model(testModel) - .type.serializedHasChanged( - {id: 1, name: "test", price: "12.548777"}, - {id: 1, price: "12.548777", name: "test"}, + expect( + s.property.model(testModel).type.hasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property.model(testModel).type.hasChanged( + null, + testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance, ), - ).toBeFalsy(); - expect( - s.property - .model(testModel) - .type.serializedHasChanged( - {id: 1, name: "test", price: "12.548777"}, - {id: 1, name: "test", price: "12.548778"}, + ).toBeTruthy(); + expect( + s.property.model(testModel).type.hasChanged( + undefined, + testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance, ), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.serializedHasChanged(null, null), - ).toBeFalsy(); - expect( - s.property - .model(testModel) - .type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.model(testModel).type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.serializedHasChanged(null, { - id: 1, - name: "test", - price: "12.548777", - }), - ).toBeTruthy(); - expect( - s.property.model(testModel).type.serializedHasChanged(undefined, { - id: 1, - name: "test", - price: "12.548777", - }), - ).toBeTruthy(); - expect( - s.property - .model(testModel) - .type.serializedHasChanged( - {id: 1, name: "test", price: "12.548777"}, + ).toBeTruthy(); + expect( + s.property.model(testModel).type.hasChanged( + testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance, null, ), - ).toBeTruthy(); - expect( - s.property - .model(testModel) - .type.serializedHasChanged( - {id: 1, name: "test", price: "12.548777"}, + ).toBeTruthy(); + expect( + s.property.model(testModel).type.hasChanged( + testModel.model( + Object.assign(new TestModel(), { + id: 1, + name: "test", + price: 12.548777, + }), + ).instance, undefined, ), - ).toBeTruthy(); + ).toBeTruthy(); + }); - { - // Serializing the difference to check that the difference has been reset. + test("invalid parameters", () => { + expect(() => + s.property.model(testModel).type.hasChanged(5 as any, 5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property + .model(testModel) + .type.hasChanged( + testModel.model(new TestModel()).instance, + [] as any, + ), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property + .model(testModel) + .type.hasChanged( + testModel.model(new TestModel()).instance, + new (class {})() as any, + ), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + s.property + .model(testModel) + .type.serializedHasChanged( + {id: 1, name: "test", price: "12.548777"}, + {id: 1, price: "12.548777", name: "test"}, + ), + ).toBeFalsy(); + expect( + s.property + .model(testModel) + .type.serializedHasChanged( + {id: 1, name: "test", price: "12.548777"}, + {id: 1, name: "test", price: "12.548778"}, + ), + ).toBeTruthy(); + expect( + s.property.model(testModel).type.serializedHasChanged(null, null), + ).toBeFalsy(); + expect( + s.property + .model(testModel) + .type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.model(testModel).type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.model(testModel).type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property.model(testModel).type.serializedHasChanged(null, { + id: 1, + name: "test", + price: "12.548777", + }), + ).toBeTruthy(); + expect( + s.property.model(testModel).type.serializedHasChanged(undefined, { + id: 1, + name: "test", + price: "12.548777", + }), + ).toBeTruthy(); + expect( + s.property + .model(testModel) + .type.serializedHasChanged( + {id: 1, name: "test", price: "12.548777"}, + null, + ), + ).toBeTruthy(); + expect( + s.property + .model(testModel) + .type.serializedHasChanged( + {id: 1, name: "test", price: "12.548777"}, + undefined, + ), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect(() => + s.property + .model(testModel) + .type.serializedHasChanged(5 as any, 5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property + .model(testModel) + .type.serializedHasChanged({} as any, [] as any), + ).toThrowError(InvalidTypeValueError); + expect( + s.property + .model(testModel) + .type.serializedHasChanged({} as any, new (class {})() as any), + ).toBeFalsy(); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { const testModelInstance = testModel.model( Object.assign(new TestModel(), { id: 1, @@ -234,6 +337,7 @@ describe("model type", () => { price: 12.548777, }), ).instance; + testModelInstance.price = 555.555; expect(testModel.model(testModelInstance).serializeDiff()).toEqual({ id: 1, @@ -243,12 +347,26 @@ describe("model type", () => { expect(testModel.model(testModelInstance).serializeDiff()).toEqual({ id: 1, }); - } - s.property.model(testModel).type.resetDiff(undefined); - s.property.model(testModel).type.resetDiff(null); + s.property.model(testModel).type.resetDiff(undefined); + s.property.model(testModel).type.resetDiff(null); + }); - { + test("invalid parameters", () => { + expect(() => + s.property.model(testModel).type.resetDiff(5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.resetDiff([] as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.resetDiff(new (class {})() as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("clone", () => { + test("clone", () => { // Test that values are cloned in a different model instance. const testModelInstance = testModel.model( Object.assign(new TestModel(), { @@ -268,10 +386,25 @@ describe("model type", () => { expect(testModel.model(clonedModelInstance).serializeDiff()).toEqual( testModel.model(testModelInstance).serializeDiff(), ); - } - expect(s.property.model(testModel).type.clone(undefined)).toBe(undefined); - expect(s.property.model(testModel).type.clone(null)).toBe(null); + expect(s.property.model(testModel).type.clone(undefined)).toBe(undefined); + expect(s.property.model(testModel).type.clone(null)).toBe(null); + }); + + test("invalid parameters", () => { + expect(() => + s.property.model(testModel).type.clone(5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.clone([] as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + s.property.model(testModel).type.clone(new (class {})() as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + test("applyPatch", () => { { // Apply a patch with undefined / NULL values. expect( @@ -446,80 +579,4 @@ describe("model type", () => { } } }); - - test("invalid parameters types", () => { - expect(() => - s.property.model(testModel).type.serialize(5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.deserialize(5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.serializeDiff(5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.resetDiff(5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.hasChanged(5 as any, 5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.serializedHasChanged(5 as any, 5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => s.property.model(testModel).type.clone(5 as any)).toThrowError( - InvalidTypeValueError, - ); - - expect(() => - s.property.model(testModel).type.serialize([] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.deserialize([] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.serializeDiff([] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.resetDiff([] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property - .model(testModel) - .type.hasChanged(testModel.model(new TestModel()).instance, [] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property - .model(testModel) - .type.serializedHasChanged({} as any, [] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.clone([] as any), - ).toThrowError(InvalidTypeValueError); - - expect(() => - s.property.model(testModel).type.serialize(new (class {})() as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.serializeDiff(new (class {})() as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property.model(testModel).type.resetDiff(new (class {})() as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - s.property - .model(testModel) - .type.hasChanged( - testModel.model(new TestModel()).instance, - new (class {})() as any, - ), - ).toThrowError(InvalidTypeValueError); - expect( - s.property - .model(testModel) - .type.serializedHasChanged({} as any, new (class {})() as any), - ).toBeFalsy(); - expect(() => - s.property.model(testModel).type.clone(new (class {})() as any), - ).toThrowError(InvalidTypeValueError); - }); }); diff --git a/tests/model/types/numeric.test.ts b/tests/model/types/numeric.test.ts index 1fe2584..a29aed5 100644 --- a/tests/model/types/numeric.test.ts +++ b/tests/model/types/numeric.test.ts @@ -2,68 +2,155 @@ import {describe, expect, test} from "vitest"; import {InvalidTypeValueError, NumericType, s} from "../../../src/library"; describe("numeric type", () => { - test("numeric type definition", () => { + test("definition", () => { const numericType = s.property.numeric(); expect(numericType.type).toBeInstanceOf(NumericType); }); - test("numeric type functions", () => { - expect(s.property.numeric().type.serialize(5.257)).toBe(5.257); - expect(s.property.numeric().type.deserialize(5.257)).toBe(5.257); - expect(s.property.numeric().type.serializeDiff(542)).toBe(542); + describe("serialize", () => { + test("serialize", () => { + expect(s.property.numeric().type.serialize(5.257)).toBe(5.257); - expect(s.property.numeric().type.serialize(null)).toBe(null); - expect(s.property.numeric().type.deserialize(null)).toBe(null); - expect(s.property.numeric().type.serializeDiff(null)).toBe(null); + expect(s.property.numeric().type.serialize(null)).toBe(null); + expect(s.property.numeric().type.serialize(undefined)).toBe(undefined); + }); - expect(s.property.numeric().type.serialize(undefined)).toBe(undefined); - expect(s.property.numeric().type.deserialize(undefined)).toBe(undefined); - expect(s.property.numeric().type.serializeDiff(undefined)).toBe(undefined); + test("invalid parameters", () => { + expect(() => s.property.numeric().type.serialize({} as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); - expect(s.property.numeric().type.hasChanged(5.257, 5.257)).toBeFalsy(); - expect(s.property.numeric().type.hasChanged(null, null)).toBeFalsy(); - expect( - s.property.numeric().type.hasChanged(undefined, undefined), - ).toBeFalsy(); - expect(s.property.numeric().type.hasChanged(null, undefined)).toBeTruthy(); - expect(s.property.numeric().type.hasChanged(undefined, null)).toBeTruthy(); - expect(s.property.numeric().type.hasChanged(null, 5.257)).toBeTruthy(); - expect(s.property.numeric().type.hasChanged(undefined, 5.257)).toBeTruthy(); - expect(s.property.numeric().type.hasChanged(5.257, null)).toBeTruthy(); - expect(s.property.numeric().type.hasChanged(5.257, undefined)).toBeTruthy(); + describe("deserialize", () => { + test("deserialize", () => { + expect(s.property.numeric().type.deserialize(5.257)).toBe(5.257); - expect( - s.property.numeric().type.serializedHasChanged(5.257, 5.257), - ).toBeFalsy(); - expect( - s.property.numeric().type.serializedHasChanged(null, null), - ).toBeFalsy(); - expect( - s.property.numeric().type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.numeric().type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.numeric().type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.numeric().type.serializedHasChanged(null, 5.257), - ).toBeTruthy(); - expect( - s.property.numeric().type.serializedHasChanged(undefined, 5.257), - ).toBeTruthy(); - expect( - s.property.numeric().type.serializedHasChanged(5.257, null), - ).toBeTruthy(); - expect( - s.property.numeric().type.serializedHasChanged(5.257, undefined), - ).toBeTruthy(); + expect(s.property.numeric().type.deserialize(null)).toBe(null); + expect(s.property.numeric().type.deserialize(undefined)).toBe(undefined); + }); - s.property.numeric().type.resetDiff(5.257); - s.property.numeric().type.resetDiff(undefined); - s.property.numeric().type.resetDiff(null); + test("invalid parameters", () => { + expect(() => + s.property.numeric().type.deserialize({} as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect(s.property.numeric().type.serializeDiff(542)).toBe(542); + + expect(s.property.numeric().type.serializeDiff(null)).toBe(null); + expect(s.property.numeric().type.serializeDiff(undefined)).toBe( + undefined, + ); + }); + + test("invalid parameters", () => { + expect(() => + s.property.numeric().type.serializeDiff({} as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("hasChanged", () => { + test("hasChanged", () => { + expect(s.property.numeric().type.hasChanged(5.257, 5.257)).toBeFalsy(); + expect(s.property.numeric().type.hasChanged(null, null)).toBeFalsy(); + expect( + s.property.numeric().type.hasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.numeric().type.hasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.numeric().type.hasChanged(undefined, null), + ).toBeTruthy(); + expect(s.property.numeric().type.hasChanged(null, 5.257)).toBeTruthy(); + expect( + s.property.numeric().type.hasChanged(undefined, 5.257), + ).toBeTruthy(); + expect(s.property.numeric().type.hasChanged(5.257, null)).toBeTruthy(); + expect( + s.property.numeric().type.hasChanged(5.257, undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.numeric().type.hasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property.numeric().type.hasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + s.property.numeric().type.serializedHasChanged(5.257, 5.257), + ).toBeFalsy(); + expect( + s.property.numeric().type.serializedHasChanged(null, null), + ).toBeFalsy(); + expect( + s.property.numeric().type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.numeric().type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.numeric().type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property.numeric().type.serializedHasChanged(null, 5.257), + ).toBeTruthy(); + expect( + s.property.numeric().type.serializedHasChanged(undefined, 5.257), + ).toBeTruthy(); + expect( + s.property.numeric().type.serializedHasChanged(5.257, null), + ).toBeTruthy(); + expect( + s.property.numeric().type.serializedHasChanged(5.257, undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.numeric().type.serializedHasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property + .numeric() + .type.serializedHasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + s.property.numeric().type.resetDiff(5.257); + s.property.numeric().type.resetDiff(undefined); + s.property.numeric().type.resetDiff(null); + }); + + test("invalid parameters", () => { + expect(() => + s.property.numeric().type.resetDiff({} as any), + ).not.toThrow(); + }); + }); + + describe("clone", () => { + test("invalid parameters", () => { + expect(s.property.numeric().type.clone({} as any)).toStrictEqual({}); + }); + }); + + test("applyPatch", () => { expect(s.property.numeric().type.applyPatch(1, 5.257, false)).toBe(5.257); expect(s.property.numeric().type.applyPatch(null, 5.257, true)).toBe(5.257); expect(s.property.numeric().type.applyPatch(undefined, 5.257, false)).toBe( @@ -74,32 +161,4 @@ describe("numeric type", () => { ).toBeUndefined(); expect(s.property.numeric().type.applyPatch(5.257, null, false)).toBeNull(); }); - - test("invalid parameters types", () => { - expect(() => s.property.numeric().type.serialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => s.property.numeric().type.deserialize({} as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => - s.property.numeric().type.serializeDiff({} as any), - ).toThrowError(InvalidTypeValueError); - expect(() => s.property.numeric().type.resetDiff({} as any)).not.toThrow(); - expect( - s.property.numeric().type.hasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.numeric().type.hasChanged(false as any, false as any), - ).toBeFalsy(); - expect( - s.property.numeric().type.serializedHasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property - .numeric() - .type.serializedHasChanged(false as any, false as any), - ).toBeFalsy(); - expect(s.property.numeric().type.clone({} as any)).toStrictEqual({}); - }); }); diff --git a/tests/model/types/object.test.ts b/tests/model/types/object.test.ts index b1063e3..8527790 100644 --- a/tests/model/types/object.test.ts +++ b/tests/model/types/object.test.ts @@ -8,7 +8,7 @@ import { } from "../../../src/library"; describe("object type", () => { - test("object type definition", () => { + test("definition", () => { const objectType = s.property.object({ test: s.property.string(), another: s.property.numeric(), @@ -31,132 +31,226 @@ describe("object type", () => { another: s.property.decimal(), }); - test("object type functions", () => { - expect( - testProperty.type.serialize({test: "test", another: 12.548777}), - ).toEqual({test: "test", another: "12.548777"}); - expect( - testProperty.type.deserialize({test: "test", another: "12.548777"}), - ).toEqual({test: "test", another: 12.548777}); - expect( - testProperty.type.serializeDiff({test: "test", another: 12.548777}), - ).toEqual({test: "test", another: "12.548777"}); + describe("serialize", () => { + test("serialize", () => { + expect( + testProperty.type.serialize({test: "test", another: 12.548777}), + ).toEqual({test: "test", another: "12.548777"}); - expect(testProperty.type.serialize(null)).toEqual(null); - expect(testProperty.type.deserialize(null)).toEqual(null); - expect(testProperty.type.serializeDiff(null)).toEqual(null); + expect(testProperty.type.serialize(null)).toEqual(null); + expect(testProperty.type.serialize(undefined)).toEqual(undefined); + }); - expect(testProperty.type.serialize(undefined)).toEqual(undefined); - expect(testProperty.type.deserialize(undefined)).toEqual(undefined); - expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined); + test("invalid parameters", () => { + expect(() => testProperty.type.serialize(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.serialize([] as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); - expect( - testProperty.type.hasChanged( - {test: "test", another: 12.548777}, - {another: 12.548777, test: "test"}, - ), - ).toBeFalsy(); - expect( - testProperty.type.hasChanged( - {test: "test", another: 12.548777}, - {test: "test", another: 12.548778}, - ), - ).toBeTruthy(); - expect(testProperty.type.hasChanged(null, null)).toBeFalsy(); - expect(testProperty.type.hasChanged(undefined, undefined)).toBeFalsy(); - expect(testProperty.type.hasChanged(null, undefined)).toBeTruthy(); - expect(testProperty.type.hasChanged(undefined, null)).toBeTruthy(); - expect( - testProperty.type.hasChanged(null, {test: "test", another: 12.548777}), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged(undefined, { - test: "test", - another: 12.548777, - }), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged({test: "test", another: 12.548777}, null), - ).toBeTruthy(); - expect( - testProperty.type.hasChanged( - {test: "test", another: 12.548777}, - undefined, - ), - ).toBeTruthy(); + describe("deserialize", () => { + test("deserialize", () => { + expect( + testProperty.type.deserialize({test: "test", another: "12.548777"}), + ).toEqual({test: "test", another: 12.548777}); - expect( - testProperty.type.serializedHasChanged( - {test: "test", another: "12.548777"}, - {another: "12.548777", test: "test"}, - ), - ).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged( - {test: "test", another: "12.548777"}, - {test: "test", another: "12.548778"}, - ), - ).toBeTruthy(); - expect(testProperty.type.serializedHasChanged(null, null)).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - testProperty.type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(null, { - test: "test", - another: "12.548777", - }), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged(undefined, { - test: "test", - another: "12.548777", - }), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - {test: "test", another: "12.548777"}, - null, - ), - ).toBeTruthy(); - expect( - testProperty.type.serializedHasChanged( - {test: "test", another: "12.548777"}, - undefined, - ), - ).toBeTruthy(); + expect(testProperty.type.deserialize(null)).toEqual(null); + expect(testProperty.type.deserialize(undefined)).toEqual(undefined); + }); - testProperty.type.resetDiff({test: "test", another: 12.548777}); - testProperty.type.resetDiff(undefined); - testProperty.type.resetDiff(null); + test("invalid parameters", () => { + expect(() => testProperty.type.deserialize(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.deserialize([] as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); - { - // Test that values are cloned in a different object. - const propertyValue = {test: "test", another: 12.548777}; - const clonedPropertyValue = testProperty.type.clone(propertyValue); - expect(clonedPropertyValue).not.toBe(propertyValue); - expect(clonedPropertyValue).toEqual(propertyValue); - } - { - // Test that values are cloned in a different object. - const propertyValue = {arr: [12, 11]}; - const clonedPropertyValue = s.property - .object({arr: s.property.array(s.property.numeric())}) - .type.clone(propertyValue); - expect(clonedPropertyValue).not.toBe(propertyValue); - expect(clonedPropertyValue).toEqual(propertyValue); - expect(clonedPropertyValue.arr).not.toBe(propertyValue.arr); - expect(clonedPropertyValue.arr).toEqual(propertyValue.arr); - } - expect(testProperty.type.clone(undefined)).toBe(undefined); - expect(testProperty.type.clone(null)).toBe(null); + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect( + testProperty.type.serializeDiff({test: "test", another: 12.548777}), + ).toEqual({test: "test", another: "12.548777"}); + expect(testProperty.type.serializeDiff(null)).toEqual(null); + expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.serializeDiff(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.serializeDiff([] as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + describe("hasChanged", () => { + test("hasChanged", () => { + expect( + testProperty.type.hasChanged( + {test: "test", another: 12.548777}, + {another: 12.548777, test: "test"}, + ), + ).toBeFalsy(); + expect( + testProperty.type.hasChanged( + {test: "test", another: 12.548777}, + {test: "test", another: 12.548778}, + ), + ).toBeTruthy(); + expect(testProperty.type.hasChanged(null, null)).toBeFalsy(); + expect(testProperty.type.hasChanged(undefined, undefined)).toBeFalsy(); + expect(testProperty.type.hasChanged(null, undefined)).toBeTruthy(); + expect(testProperty.type.hasChanged(undefined, null)).toBeTruthy(); + expect( + testProperty.type.hasChanged(null, {test: "test", another: 12.548777}), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged(undefined, { + test: "test", + another: 12.548777, + }), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged({test: "test", another: 12.548777}, null), + ).toBeTruthy(); + expect( + testProperty.type.hasChanged( + {test: "test", another: 12.548777}, + undefined, + ), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect(() => + testProperty.type.hasChanged(5 as any, 5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + testProperty.type.hasChanged({} as any, [] as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + testProperty.type.serializedHasChanged( + {test: "test", another: "12.548777"}, + {another: "12.548777", test: "test"}, + ), + ).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged( + {test: "test", another: "12.548777"}, + {test: "test", another: "12.548778"}, + ), + ).toBeTruthy(); + expect(testProperty.type.serializedHasChanged(null, null)).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + testProperty.type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(null, { + test: "test", + another: "12.548777", + }), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged(undefined, { + test: "test", + another: "12.548777", + }), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + {test: "test", another: "12.548777"}, + null, + ), + ).toBeTruthy(); + expect( + testProperty.type.serializedHasChanged( + {test: "test", another: "12.548777"}, + undefined, + ), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect(() => + testProperty.type.serializedHasChanged(5 as any, 5 as any), + ).toThrowError(InvalidTypeValueError); + expect(() => + testProperty.type.serializedHasChanged({} as any, [] as any), + ).toThrowError(InvalidTypeValueError); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + testProperty.type.resetDiff({test: "test", another: 12.548777}); + testProperty.type.resetDiff(undefined); + testProperty.type.resetDiff(null); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.resetDiff(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.resetDiff([] as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + describe("clone", () => { + test("clone", () => { + { + // Test that values are cloned in a different object. + const propertyValue = {test: "test", another: 12.548777}; + const clonedPropertyValue = testProperty.type.clone(propertyValue); + expect(clonedPropertyValue).not.toBe(propertyValue); + expect(clonedPropertyValue).toEqual(propertyValue); + } + { + // Test that values are cloned in a different object. + const propertyValue = {arr: [12, 11]}; + const clonedPropertyValue = s.property + .object({arr: s.property.array(s.property.numeric())}) + .type.clone(propertyValue); + expect(clonedPropertyValue).not.toBe(propertyValue); + expect(clonedPropertyValue).toEqual(propertyValue); + expect(clonedPropertyValue.arr).not.toBe(propertyValue.arr); + expect(clonedPropertyValue.arr).toEqual(propertyValue.arr); + } + expect(testProperty.type.clone(undefined)).toBe(undefined); + expect(testProperty.type.clone(null)).toBe(null); + }); + + test("invalid parameters", () => { + expect(() => testProperty.type.clone(5 as any)).toThrowError( + InvalidTypeValueError, + ); + expect(() => testProperty.type.clone([] as any)).toThrowError( + InvalidTypeValueError, + ); + }); + }); + + test("applyPatch", () => { { // Apply a patch with undefined / NULL values. expect( @@ -226,50 +320,4 @@ describe("object type", () => { } } }); - - test("invalid parameters types", () => { - expect(() => testProperty.type.serialize(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.deserialize(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.serializeDiff(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.resetDiff(5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.hasChanged(5 as any, 5 as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => - testProperty.type.serializedHasChanged(5 as any, 5 as any), - ).toThrowError(InvalidTypeValueError); - expect(() => testProperty.type.clone(5 as any)).toThrowError( - InvalidTypeValueError, - ); - - expect(() => testProperty.type.serialize([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.deserialize([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.serializeDiff([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => testProperty.type.resetDiff([] as any)).toThrowError( - InvalidTypeValueError, - ); - expect(() => - testProperty.type.hasChanged({} as any, [] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => - testProperty.type.serializedHasChanged({} as any, [] as any), - ).toThrowError(InvalidTypeValueError); - expect(() => testProperty.type.clone([] as any)).toThrowError( - InvalidTypeValueError, - ); - }); }); diff --git a/tests/model/types/string.test.ts b/tests/model/types/string.test.ts index 30b55a6..cff046d 100644 --- a/tests/model/types/string.test.ts +++ b/tests/model/types/string.test.ts @@ -2,68 +2,149 @@ import {describe, expect, test} from "vitest"; import {s, StringType} from "../../../src/library"; describe("string type", () => { - test("string type definition", () => { + test("definition", () => { const stringType = s.property.string(); expect(stringType.type).toBeInstanceOf(StringType); }); - test("string type functions", () => { - expect(s.property.string().type.serialize("test")).toBe("test"); - expect(s.property.string().type.deserialize("test")).toBe("test"); - expect(s.property.string().type.serializeDiff("test")).toBe("test"); + describe("serialize", () => { + test("serialize", () => { + expect(s.property.string().type.serialize("test")).toBe("test"); + expect(s.property.string().type.serialize(null)).toBe(null); + expect(s.property.string().type.serialize(undefined)).toBe(undefined); + }); - expect(s.property.string().type.serialize(null)).toBe(null); - expect(s.property.string().type.deserialize(null)).toBe(null); - expect(s.property.string().type.serializeDiff(null)).toBe(null); + test("invalid parameters", () => { + const testDate = new Date(); + expect(s.property.string().type.serialize({} as any)).toBe( + "[object Object]", + ); + expect(s.property.string().type.serialize(2120 as any)).toBe("2120"); + expect(s.property.string().type.serialize(testDate as any)).toBe( + testDate.toString(), + ); + }); + }); - expect(s.property.string().type.serialize(undefined)).toBe(undefined); - expect(s.property.string().type.deserialize(undefined)).toBe(undefined); - expect(s.property.string().type.serializeDiff(undefined)).toBe(undefined); + describe("deserialize", () => { + test("deserialize", () => { + expect(s.property.string().type.deserialize("test")).toBe("test"); + expect(s.property.string().type.deserialize(null)).toBe(null); + expect(s.property.string().type.deserialize(undefined)).toBe(undefined); + }); - expect(s.property.string().type.hasChanged("test", "test")).toBeFalsy(); - expect(s.property.string().type.hasChanged(null, null)).toBeFalsy(); - expect( - s.property.string().type.hasChanged(undefined, undefined), - ).toBeFalsy(); - expect(s.property.string().type.hasChanged(null, undefined)).toBeTruthy(); - expect(s.property.string().type.hasChanged(undefined, null)).toBeTruthy(); - expect(s.property.string().type.hasChanged(null, "test")).toBeTruthy(); - expect(s.property.string().type.hasChanged(undefined, "test")).toBeTruthy(); - expect(s.property.string().type.hasChanged("test", null)).toBeTruthy(); - expect(s.property.string().type.hasChanged("test", undefined)).toBeTruthy(); + test("invalid parameters", () => { + expect(s.property.string().type.deserialize({} as any)).toBe( + "[object Object]", + ); + expect(s.property.string().type.deserialize(2120 as any)).toBe("2120"); + }); + }); - expect( - s.property.string().type.serializedHasChanged("test", "test"), - ).toBeFalsy(); - expect( - s.property.string().type.serializedHasChanged(null, null), - ).toBeFalsy(); - expect( - s.property.string().type.serializedHasChanged(undefined, undefined), - ).toBeFalsy(); - expect( - s.property.string().type.serializedHasChanged(null, undefined), - ).toBeTruthy(); - expect( - s.property.string().type.serializedHasChanged(undefined, null), - ).toBeTruthy(); - expect( - s.property.string().type.serializedHasChanged(null, "test"), - ).toBeTruthy(); - expect( - s.property.string().type.serializedHasChanged(undefined, "test"), - ).toBeTruthy(); - expect( - s.property.string().type.serializedHasChanged("test", null), - ).toBeTruthy(); - expect( - s.property.string().type.serializedHasChanged("test", undefined), - ).toBeTruthy(); + describe("serializeDiff", () => { + test("serializeDiff", () => { + expect(s.property.string().type.serializeDiff("test")).toBe("test"); + expect(s.property.string().type.serializeDiff(null)).toBe(null); + expect(s.property.string().type.serializeDiff(undefined)).toBe(undefined); + }); - s.property.string().type.resetDiff("test"); - s.property.string().type.resetDiff(undefined); - s.property.string().type.resetDiff(null); + test("invalid parameters", () => { + expect(s.property.string().type.serializeDiff({} as any)).toBe( + "[object Object]", + ); + expect(s.property.string().type.serializeDiff(2120 as any)).toBe("2120"); + }); + }); + describe("hasChanged", () => { + test("hasChanged", () => { + expect(s.property.string().type.hasChanged("test", "test")).toBeFalsy(); + expect(s.property.string().type.hasChanged(null, null)).toBeFalsy(); + expect( + s.property.string().type.hasChanged(undefined, undefined), + ).toBeFalsy(); + expect(s.property.string().type.hasChanged(null, undefined)).toBeTruthy(); + expect(s.property.string().type.hasChanged(undefined, null)).toBeTruthy(); + expect(s.property.string().type.hasChanged(null, "test")).toBeTruthy(); + expect( + s.property.string().type.hasChanged(undefined, "test"), + ).toBeTruthy(); + expect(s.property.string().type.hasChanged("test", null)).toBeTruthy(); + expect( + s.property.string().type.hasChanged("test", undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.string().type.hasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property.string().type.hasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("serializedHasChanged", () => { + test("serializedHasChanged", () => { + expect( + s.property.string().type.serializedHasChanged("test", "test"), + ).toBeFalsy(); + expect( + s.property.string().type.serializedHasChanged(null, null), + ).toBeFalsy(); + expect( + s.property.string().type.serializedHasChanged(undefined, undefined), + ).toBeFalsy(); + expect( + s.property.string().type.serializedHasChanged(null, undefined), + ).toBeTruthy(); + expect( + s.property.string().type.serializedHasChanged(undefined, null), + ).toBeTruthy(); + expect( + s.property.string().type.serializedHasChanged(null, "test"), + ).toBeTruthy(); + expect( + s.property.string().type.serializedHasChanged(undefined, "test"), + ).toBeTruthy(); + expect( + s.property.string().type.serializedHasChanged("test", null), + ).toBeTruthy(); + expect( + s.property.string().type.serializedHasChanged("test", undefined), + ).toBeTruthy(); + }); + + test("invalid parameters", () => { + expect( + s.property.string().type.serializedHasChanged({} as any, {} as any), + ).toBeTruthy(); + expect( + s.property + .string() + .type.serializedHasChanged(false as any, false as any), + ).toBeFalsy(); + }); + }); + + describe("resetDiff", () => { + test("resetDiff", () => { + s.property.string().type.resetDiff("test"); + s.property.string().type.resetDiff(undefined); + s.property.string().type.resetDiff(null); + }); + + test("invalid parameters", () => { + expect(() => s.property.string().type.resetDiff({} as any)).not.toThrow(); + }); + }); + + test("clone", () => { + expect(s.property.string().type.clone({} as any)).toStrictEqual({}); + }); + + test("applyPatch", () => { expect(s.property.string().type.applyPatch("another", "test", false)).toBe( "test", ); @@ -78,36 +159,4 @@ describe("string type", () => { ).toBeUndefined(); expect(s.property.string().type.applyPatch("test", null, false)).toBeNull(); }); - - test("invalid parameters types", () => { - const testDate = new Date(); - expect(s.property.string().type.serialize({} as any)).toBe( - "[object Object]", - ); - expect(s.property.string().type.serialize(2120 as any)).toBe("2120"); - expect(s.property.string().type.serialize(testDate as any)).toBe( - testDate.toString(), - ); - expect(s.property.string().type.deserialize({} as any)).toBe( - "[object Object]", - ); - expect(s.property.string().type.deserialize(2120 as any)).toBe("2120"); - expect(s.property.string().type.serializeDiff({} as any)).toBe( - "[object Object]", - ); - expect(s.property.string().type.serializeDiff(2120 as any)).toBe("2120"); - expect( - s.property.string().type.hasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.string().type.hasChanged(false as any, false as any), - ).toBeFalsy(); - expect( - s.property.string().type.serializedHasChanged({} as any, {} as any), - ).toBeTruthy(); - expect( - s.property.string().type.serializedHasChanged(false as any, false as any), - ).toBeFalsy(); - expect(s.property.string().type.clone({} as any)).toStrictEqual({}); - }); });