Refactor types tests.
All checks were successful
/ test (push) Successful in 30s

This commit is contained in:
Madeorsk 2025-06-28 22:45:56 +02:00
parent 38c87249b1
commit a4c1c88138
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
9 changed files with 1706 additions and 1236 deletions

View file

@ -18,23 +18,51 @@ 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", () => {
describe("serialize", () => {
test("serialize", () => {
expect(testProperty.type.serialize([12.547, 8, -52.11])).toEqual([
"12.547",
"8",
"-52.11",
]);
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 = [
@ -57,14 +85,19 @@ describe("array type", () => {
).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);
});
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();
@ -96,7 +129,18 @@ describe("array type", () => {
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"],
@ -156,7 +200,20 @@ describe("array type", () => {
["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 = [
@ -186,9 +243,18 @@ describe("array type", () => {
.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];
@ -227,9 +293,19 @@ describe("array type", () => {
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,
);
});
});

View file

@ -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,31 +13,82 @@ describe("boolean type", () => {
}
});
test("boolean type functions", () => {
describe("serialize", () => {
test("serialize", () => {
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);
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);
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);
});
test("invalid parameters", () => {
expect(s.property.boolean().type.serialize(1 as any)).toBeTruthy();
expect(s.property.boolean().type.serialize(0 as any)).toBeFalsy();
});
});
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);
});
test("invalid parameters", () => {
expect(s.property.boolean().type.deserialize(1 as any)).toBeTruthy();
expect(s.property.boolean().type.deserialize(0 as any)).toBeFalsy();
});
});
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,
);
});
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, 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(undefined, false),
).toBeTruthy();
expect(s.property.boolean().type.hasChanged(false, null)).toBeTruthy();
expect(s.property.boolean().type.hasChanged(false, undefined)).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();
@ -65,11 +116,41 @@ describe("boolean type", () => {
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({});
});
});

View file

@ -4,57 +4,115 @@ 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", () => {
describe("serialize", () => {
test("serialize", () => {
expect(s.property.date().type.serialize(testDate)).toBe(
testDate.toISOString(),
);
expect(s.property.date().type.serialize(new Date(NaN))).toBe(
new Date(NaN).toString(),
);
expect(s.property.date().type.serialize(null)).toBe(null);
expect(s.property.date().type.serialize(undefined)).toBe(undefined);
});
test("invalid parameters", () => {
expect(() => s.property.date().type.serialize({} as any)).toThrowError(
InvalidTypeValueError,
);
});
});
describe("deserialize", () => {
test("deserialize", () => {
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(),
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.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(undefined)).toBe(undefined);
expect(s.property.date().type.deserialize(undefined)).toBe(undefined);
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(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(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), 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()
@ -63,7 +121,9 @@ describe("date type", () => {
new Date(testDate).toISOString(),
),
).toBeFalsy();
expect(s.property.date().type.serializedHasChanged(null, null)).toBeFalsy();
expect(
s.property.date().type.serializedHasChanged(null, null),
).toBeFalsy();
expect(
s.property.date().type.serializedHasChanged(undefined, undefined),
).toBeFalsy();
@ -74,7 +134,9 @@ describe("date type", () => {
s.property.date().type.serializedHasChanged(undefined, null),
).toBeTruthy();
expect(
s.property.date().type.serializedHasChanged(null, testDate.toISOString()),
s.property
.date()
.type.serializedHasChanged(null, testDate.toISOString()),
).toBeTruthy();
expect(
s.property
@ -82,7 +144,9 @@ describe("date type", () => {
.type.serializedHasChanged(undefined, testDate.toISOString()),
).toBeTruthy();
expect(
s.property.date().type.serializedHasChanged(testDate.toISOString(), null),
s.property
.date()
.type.serializedHasChanged(testDate.toISOString(), null),
).toBeTruthy();
expect(
s.property
@ -102,19 +166,40 @@ describe("date type", () => {
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({});
});
});

View file

@ -7,31 +7,100 @@ describe("decimal type", () => {
expect(decimalType.type).toBeInstanceOf(DecimalType);
});
test("decimal type functions", () => {
describe("serialize", () => {
test("serialize", () => {
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");
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(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,
);
});
});
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);
});
test("invalid parameters", () => {
expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN);
expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN);
});
});
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, 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(undefined, 5.257),
).toBeTruthy();
expect(s.property.decimal().type.hasChanged(5.257, null)).toBeTruthy();
expect(s.property.decimal().type.hasChanged(5.257, undefined)).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();
@ -59,11 +128,27 @@ describe("decimal type", () => {
expect(
s.property.decimal().type.serializedHasChanged("5.257", undefined),
).toBeTruthy();
});
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.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({});
});
});

View file

@ -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,30 +16,79 @@ describe("map type", () => {
testMapValue.set("test", 1.52);
testMapValue.set("another", 55);
test("object type functions", () => {
describe("serialize", () => {
test("serialize", () => {
expect(testProperty.type.serialize(testMapValue)).toEqual({
test: "1.52",
another: "55",
});
expect(testProperty.type.serialize(null)).toEqual(null);
expect(testProperty.type.serialize(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.serialize({} as any)).toThrowError(
InvalidTypeValueError,
);
});
});
describe("deserialize", () => {
test("deserialize", () => {
expect(
testProperty.type.deserialize({
test: "1.52",
another: "55",
}),
).toEqual(testMapValue);
expect(testProperty.type.deserialize(null)).toEqual(null);
expect(testProperty.type.deserialize(undefined)).toEqual(undefined);
});
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",
});
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(undefined)).toEqual(undefined);
expect(testProperty.type.deserialize(undefined)).toEqual(undefined);
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,
);
expect(() => testProperty.type.serializeDiff({} as any)).toThrowError(
InvalidTypeValueError,
);
});
});
describe("hasChanged", () => {
test("hasChanged", () => {
const anotherTestMapValue = new Map<string, number>();
anotherTestMapValue.set("test", 1.52);
anotherTestMapValue.set("another", 55);
@ -64,10 +108,18 @@ describe("map type", () => {
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(undefined, testMapValue),
).toBeTruthy();
expect(testProperty.type.hasChanged(testMapValue, null)).toBeTruthy();
expect(testProperty.type.hasChanged(testMapValue, undefined)).toBeTruthy();
expect(
testProperty.type.hasChanged(testMapValue, undefined),
).toBeTruthy();
});
});
describe("serializedHasChanged", () => {
test("serializedHasChanged", () => {
expect(
testProperty.type.serializedHasChanged(
{test: "1.52", another: "55"},
@ -120,11 +172,19 @@ describe("map type", () => {
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);
@ -149,7 +209,22 @@ describe("map type", () => {
}
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,
);
});
});

View file

@ -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,26 +102,35 @@ 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.serialize(undefined)).toEqual(
undefined,
);
expect(s.property.model(testModel).type.deserialize(undefined)).toEqual(
undefined,
expect(s.property.model(testModel).type.serializeDiff(null)).toEqual(
null,
);
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);
});
});
describe("hasChanged", () => {
test("hasChanged", () => {
{
const testModelInstance = testModel.model(
Object.assign(new TestModel(), {
@ -105,7 +160,9 @@ describe("model type", () => {
.type.hasChanged(testModelInstance, testModelInstance),
).toBeTruthy();
}
expect(s.property.model(testModel).type.hasChanged(null, null)).toBeFalsy();
expect(
s.property.model(testModel).type.hasChanged(null, null),
).toBeFalsy();
expect(
s.property.model(testModel).type.hasChanged(undefined, undefined),
).toBeFalsy();
@ -163,7 +220,33 @@ describe("model type", () => {
undefined,
),
).toBeTruthy();
});
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)
@ -224,9 +307,29 @@ describe("model type", () => {
undefined,
),
).toBeTruthy();
});
{
// Serializing the difference to check that the difference has been reset.
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);
});
{
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);
});
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);
});
});

View file

@ -2,36 +2,93 @@ 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", () => {
describe("serialize", () => {
test("serialize", () => {
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);
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(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,
);
});
});
describe("deserialize", () => {
test("deserialize", () => {
expect(s.property.numeric().type.deserialize(5.257)).toBe(5.257);
expect(s.property.numeric().type.deserialize(null)).toBe(null);
expect(s.property.numeric().type.deserialize(undefined)).toBe(undefined);
});
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, 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(undefined, 5.257),
).toBeTruthy();
expect(s.property.numeric().type.hasChanged(5.257, null)).toBeTruthy();
expect(s.property.numeric().type.hasChanged(5.257, undefined)).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();
@ -59,11 +116,41 @@ describe("numeric type", () => {
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({});
});
});

View file

@ -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,25 +31,68 @@ describe("object type", () => {
another: s.property.decimal(),
});
test("object type functions", () => {
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.serialize(undefined)).toEqual(undefined);
});
test("invalid parameters", () => {
expect(() => testProperty.type.serialize(5 as any)).toThrowError(
InvalidTypeValueError,
);
expect(() => testProperty.type.serialize([] as any)).toThrowError(
InvalidTypeValueError,
);
});
});
describe("deserialize", () => {
test("deserialize", () => {
expect(
testProperty.type.deserialize({test: "test", another: "12.548777"}),
).toEqual({test: "test", another: 12.548777});
expect(testProperty.type.deserialize(null)).toEqual(null);
expect(testProperty.type.deserialize(undefined)).toEqual(undefined);
});
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({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(undefined)).toEqual(undefined);
expect(testProperty.type.deserialize(undefined)).toEqual(undefined);
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},
@ -84,7 +127,20 @@ describe("object type", () => {
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"},
@ -131,11 +187,37 @@ describe("object type", () => {
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};
@ -156,7 +238,19 @@ describe("object type", () => {
}
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,
);
});
});

View file

@ -2,24 +2,62 @@ 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", () => {
describe("serialize", () => {
test("serialize", () => {
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");
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);
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);
});
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(),
);
});
});
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);
});
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");
});
});
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);
});
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(
@ -28,10 +66,27 @@ describe("string type", () => {
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(undefined, "test"),
).toBeTruthy();
expect(s.property.string().type.hasChanged("test", null)).toBeTruthy();
expect(s.property.string().type.hasChanged("test", undefined)).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();
@ -59,11 +114,37 @@ describe("string type", () => {
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({});
});
});