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,218 +18,294 @@ describe("array type", () => {
identifier: "id", identifier: "id",
}); });
test("array type definition", () => { test("definition", () => {
const arrayType = s.property.array(s.property.model(testModel)); const arrayType = s.property.array(s.property.model(testModel));
expect(arrayType.type).toBeInstanceOf(ArrayType); expect(arrayType.type).toBeInstanceOf(ArrayType);
}); });
const testProperty = s.property.array(s.property.decimal()); const testProperty = s.property.array(s.property.decimal());
test("array type functions", () => { describe("serialize", () => {
expect(testProperty.type.serialize([12.547, 8, -52.11])).toEqual([ test("serialize", () => {
"12.547", expect(testProperty.type.serialize([12.547, 8, -52.11])).toEqual([
"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, [
"12.547", "12.547",
"8", "8",
"-52.11", "-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();
{ expect(testProperty.type.serialize(null)).toBe(null);
// Try to reset the difference of an array with one changed model. expect(testProperty.type.serialize(undefined)).toBe(undefined);
const propertyValue = [ });
testModel.model(
Object.assign(new TestModel(), {id: 1, name: "test", price: 22}), test("invalid parameters", () => {
).instance, expect(() => testProperty.type.serialize({} as any)).toThrowError(
testModel.model( InvalidTypeValueError,
Object.assign(new TestModel(), { );
id: 2, });
name: "another", });
price: 12.55,
}), describe("deserialize", () => {
).instance, test("deserialize", () => {
]; expect(testProperty.type.deserialize(["12.547", "8", "-52.11"])).toEqual([
propertyValue[0].name = "new"; 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( 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 s.property
.array(s.property.model(testModel)) .array(s.property.model(testModel))
.type.serializeDiff(propertyValue), .type.resetDiff(propertyValue);
).toEqual([{id: 1, name: "new"}, {id: 2}]); expect(
s.property s.property
.array(s.property.model(testModel)) .array(s.property.model(testModel))
.type.resetDiff(propertyValue); .type.serializeDiff(propertyValue),
expect( ).toEqual([{id: 1}, {id: 2}]);
s.property }
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)) .array(s.property.model(testModel))
.type.serializeDiff(propertyValue), .type.clone(propertyValue);
).toEqual([{id: 1}, {id: 2}]); expect(clonedPropertyValue).not.toBe(propertyValue);
}
testProperty.type.resetDiff(undefined);
testProperty.type.resetDiff(null);
{ // Array values must be different objects but have the same values.
// Test that values are cloned in a different array. expect(clonedPropertyValue[0]).not.toBe(propertyValue[0]);
const propertyValue = [12.547, 8, -52.11]; expect(clonedPropertyValue[1]).not.toBe(propertyValue[1]);
const clonedPropertyValue = testProperty.type.clone(propertyValue); expect(
expect(clonedPropertyValue).not.toBe(propertyValue); testModel.model(clonedPropertyValue[0]).getInstanceProperties(),
expect(clonedPropertyValue).toEqual(propertyValue); ).toEqual(testModel.model(propertyValue[0]).getInstanceProperties());
} expect(
{ testModel.model(clonedPropertyValue[1]).getInstanceProperties(),
// Test that values are cloned recursively. ).toEqual(testModel.model(propertyValue[1]).getInstanceProperties());
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. expect(testProperty.type.clone(undefined)).toBe(undefined);
const clonedPropertyValue = s.property expect(testProperty.type.clone(null)).toBe(null);
.array(s.property.model(testModel)) });
.type.clone(propertyValue);
expect(clonedPropertyValue).not.toBe(propertyValue);
// Array values must be different objects but have the same values. test("invalid parameters", () => {
expect(clonedPropertyValue[0]).not.toBe(propertyValue[0]); expect(() => testProperty.type.clone({} as any)).toThrowError(
expect(clonedPropertyValue[1]).not.toBe(propertyValue[1]); InvalidTypeValueError,
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("applyPatch", () => {
{ {
// Test simple patch. // Test simple patch.
expect( 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"; import {BooleanType, s} from "../../../src/library";
describe("boolean type", () => { describe("boolean type", () => {
test("boolean type definition", () => { test("definition", () => {
{ {
const booleanType = s.property.boolean(); const booleanType = s.property.boolean();
expect(booleanType.type).toBeInstanceOf(BooleanType); expect(booleanType.type).toBeInstanceOf(BooleanType);
@ -13,63 +13,144 @@ describe("boolean type", () => {
} }
}); });
test("boolean type functions", () => { describe("serialize", () => {
expect(s.property.boolean().type.serialize(false)).toBe(false); test("serialize", () => {
expect(s.property.boolean().type.deserialize(false)).toBe(false); expect(s.property.boolean().type.serialize(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.serialize(undefined)).toBe(undefined);
});
expect(s.property.boolean().type.serialize(null)).toBe(null); test("invalid parameters", () => {
expect(s.property.boolean().type.deserialize(null)).toBe(null); expect(s.property.boolean().type.serialize(1 as any)).toBeTruthy();
expect(s.property.boolean().type.serializeDiff(null)).toBe(null); expect(s.property.boolean().type.serialize(0 as any)).toBeFalsy();
});
});
expect(s.property.boolean().type.serialize(undefined)).toBe(undefined); describe("deserialize", () => {
expect(s.property.boolean().type.deserialize(undefined)).toBe(undefined); test("deserialize", () => {
expect(s.property.boolean().type.serializeDiff(undefined)).toBe(undefined); 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(); test("invalid parameters", () => {
expect(s.property.boolean().type.hasChanged(null, null)).toBeFalsy(); expect(s.property.boolean().type.deserialize(1 as any)).toBeTruthy();
expect( expect(s.property.boolean().type.deserialize(0 as any)).toBeFalsy();
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();
expect( describe("serializeDiff", () => {
s.property.boolean().type.serializedHasChanged(false, false), test("serializeDiff", () => {
).toBeFalsy(); expect(s.property.boolean().type.serializeDiff(true)).toBe(true);
expect( expect(s.property.boolean().type.serializeDiff(null)).toBe(null);
s.property.boolean().type.serializedHasChanged(null, null), expect(s.property.boolean().type.serializeDiff(undefined)).toBe(
).toBeFalsy(); undefined,
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();
s.property.boolean().type.resetDiff(false); test("invalid parameters", () => {
s.property.boolean().type.resetDiff(undefined); expect(s.property.boolean().type.serializeDiff(1 as any)).toBeTruthy();
s.property.boolean().type.resetDiff(null); 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( expect(
s.property.boolean().type.applyPatch(false, true, true), s.property.boolean().type.applyPatch(false, true, true),
).toBeTruthy(); ).toBeTruthy();
@ -91,29 +172,4 @@ describe("boolean type", () => {
s.property.boolean().type.applyPatch(null, false, false), s.property.boolean().type.applyPatch(null, false, false),
).toBeFalsy(); ).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,117 +4,202 @@ import {DateType, InvalidTypeValueError, s} from "../../../src/library";
describe("date type", () => { describe("date type", () => {
const testDate = new Date(); const testDate = new Date();
test("date type definition", () => { test("definition", () => {
const dateType = s.property.date(); const dateType = s.property.date();
expect(dateType.type).toBeInstanceOf(DateType); expect(dateType.type).toBeInstanceOf(DateType);
}); });
test("date type functions", () => { describe("serialize", () => {
expect(s.property.date().type.serialize(testDate)).toBe( test("serialize", () => {
testDate.toISOString(), 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(),
);
expect(s.property.date().type.serialize(null)).toBe(null); expect(s.property.date().type.serialize(new Date(NaN))).toBe(
expect(s.property.date().type.deserialize(null)).toBe(null); new Date(NaN).toString(),
expect(s.property.date().type.serializeDiff(null)).toBe(null); );
expect(s.property.date().type.serialize(undefined)).toBe(undefined); expect(s.property.date().type.serialize(null)).toBe(null);
expect(s.property.date().type.deserialize(undefined)).toBe(undefined); expect(s.property.date().type.serialize(undefined)).toBe(undefined);
expect(s.property.date().type.serializeDiff(undefined)).toBe(undefined); });
expect( test("invalid parameters", () => {
s.property.date().type.hasChanged(testDate, new Date(testDate)), expect(() => s.property.date().type.serialize({} as any)).toThrowError(
).toBeFalsy(); InvalidTypeValueError,
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();
expect( describe("deserialize", () => {
s.property test("deserialize", () => {
.date() expect(
.type.serializedHasChanged( s.property.date().type.deserialize(testDate.toISOString())?.getTime(),
testDate.toISOString(), ).toBe(testDate.getTime());
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();
s.property.date().type.resetDiff(testDate); expect(
s.property.date().type.resetDiff(undefined); s.property
s.property.date().type.resetDiff(null); .date()
.type.deserialize("2565152-2156121-256123121 5121544175:21515612")
.valueOf(),
).toBeNaN();
{ expect(s.property.date().type.deserialize(null)).toBe(null);
// Test that the date is cloned in a different object. expect(s.property.date().type.deserialize(undefined)).toBe(undefined);
const propertyValue = new Date(); });
const clonedPropertyValue = s.property.date().type.clone(propertyValue);
expect(clonedPropertyValue).not.toBe(propertyValue);
expect(clonedPropertyValue).toEqual(propertyValue);
}
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( expect(
s.property s.property
.date() .date()
@ -144,33 +229,4 @@ describe("date type", () => {
s.property.date().type.applyPatch(new Date(), null, false), s.property.date().type.applyPatch(new Date(), null, false),
).toBeNull(); ).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,63 +7,148 @@ describe("decimal type", () => {
expect(decimalType.type).toBeInstanceOf(DecimalType); expect(decimalType.type).toBeInstanceOf(DecimalType);
}); });
test("decimal type functions", () => { describe("serialize", () => {
expect(s.property.decimal().type.serialize(5.257)).toBe("5.257"); test("serialize", () => {
expect(s.property.decimal().type.deserialize("5.257")).toBe(5.257); expect(s.property.decimal().type.serialize(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.serialize(null)).toBe(null);
expect(s.property.decimal().type.deserialize(null)).toBe(null); expect(s.property.decimal().type.serialize(undefined)).toBe(undefined);
expect(s.property.decimal().type.serializeDiff(null)).toBe(null); });
expect(s.property.decimal().type.serialize(undefined)).toBe(undefined); test("invalid parameters", () => {
expect(s.property.decimal().type.deserialize(undefined)).toBe(undefined); expect(() => s.property.decimal().type.serialize({} as any)).toThrowError(
expect(s.property.decimal().type.serializeDiff(undefined)).toBe(undefined); InvalidTypeValueError,
);
});
});
expect(s.property.decimal().type.hasChanged(5.257, 5.257)).toBeFalsy(); describe("deserialize", () => {
expect(s.property.decimal().type.hasChanged(null, null)).toBeFalsy(); test("deserialize", () => {
expect( expect(s.property.decimal().type.deserialize("5.257")).toBe(5.257);
s.property.decimal().type.hasChanged(undefined, undefined), expect(s.property.decimal().type.deserialize(null)).toBe(null);
).toBeFalsy(); expect(s.property.decimal().type.deserialize(undefined)).toBe(undefined);
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();
expect( test("invalid parameters", () => {
s.property.decimal().type.serializedHasChanged("5.257", "5.257"), expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN);
).toBeFalsy(); expect(s.property.decimal().type.deserialize({} as any)).toBe(NaN);
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();
s.property.decimal().type.resetDiff(5.257); describe("serializeDiff", () => {
s.property.decimal().type.resetDiff(undefined); test("serializeDiff", () => {
s.property.decimal().type.resetDiff(null); 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(1, "5.257", false)).toBe(5.257);
expect(s.property.decimal().type.applyPatch(undefined, "5.257", true)).toBe( expect(s.property.decimal().type.applyPatch(undefined, "5.257", true)).toBe(
5.257, 5.257,
@ -76,31 +161,4 @@ describe("decimal type", () => {
).toBeUndefined(); ).toBeUndefined();
expect(s.property.decimal().type.applyPatch(5.257, null, false)).toBeNull(); 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 {describe, expect, test} from "vitest";
import { import {InvalidTypeValueError, s} from "../../../src/library";
InvalidTypeValueError,
NumericType,
s,
StringType,
} from "../../../src/library";
import {MapType} from "../../../src/model/types/map"; import {MapType} from "../../../src/model/types/map";
describe("map type", () => { describe("map type", () => {
test("map type definition", () => { test("definition", () => {
const mapType = s.property.map(s.property.string(), s.property.numeric()); const mapType = s.property.map(s.property.string(), s.property.numeric());
expect(mapType.type).toBeInstanceOf(MapType); expect(mapType.type).toBeInstanceOf(MapType);
}); });
@ -21,135 +16,215 @@ describe("map type", () => {
testMapValue.set("test", 1.52); testMapValue.set("test", 1.52);
testMapValue.set("another", 55); testMapValue.set("another", 55);
test("object type functions", () => { describe("serialize", () => {
expect(testProperty.type.serialize(testMapValue)).toEqual({ test("serialize", () => {
test: "1.52", expect(testProperty.type.serialize(testMapValue)).toEqual({
another: "55",
});
expect(
testProperty.type.deserialize({
test: "1.52", test: "1.52",
another: "55", another: "55",
}), });
).toEqual(testMapValue);
expect(testProperty.type.serializeDiff(testMapValue)).toEqual({ expect(testProperty.type.serialize(null)).toEqual(null);
test: "1.52", expect(testProperty.type.serialize(undefined)).toEqual(undefined);
another: "55",
}); });
expect(testProperty.type.serialize(null)).toEqual(null); test("invalid parameters", () => {
expect(testProperty.type.deserialize(null)).toEqual(null); expect(() => testProperty.type.serialize(5 as any)).toThrowError(
expect(testProperty.type.serializeDiff(null)).toEqual(null); InvalidTypeValueError,
);
expect(() => testProperty.type.serialize([] as any)).toThrowError(
InvalidTypeValueError,
);
expect(() => testProperty.type.serialize({} as any)).toThrowError(
InvalidTypeValueError,
);
});
});
expect(testProperty.type.serialize(undefined)).toEqual(undefined); describe("deserialize", () => {
expect(testProperty.type.deserialize(undefined)).toEqual(undefined); test("deserialize", () => {
expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined); expect(
testProperty.type.deserialize({
test: "1.52",
another: "55",
}),
).toEqual(testMapValue);
const anotherTestMapValue = new Map<string, number>(); expect(testProperty.type.deserialize(null)).toEqual(null);
anotherTestMapValue.set("test", 1.52); expect(testProperty.type.deserialize(undefined)).toEqual(undefined);
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( test("invalid parameters", () => {
testProperty.type.serializedHasChanged( expect(() => testProperty.type.deserialize(5 as any)).toThrowError(
{test: "1.52", another: "55"}, InvalidTypeValueError,
{test: "1.52", another: "55"}, );
), expect(() => testProperty.type.deserialize([] as any)).toThrowError(
).toBeFalsy(); InvalidTypeValueError,
expect( );
testProperty.type.serializedHasChanged( });
{test: "1.52", another: "55"}, });
{test: "1.521", another: "55"},
), describe("serializeDiff", () => {
).toBeTruthy(); test("serializeDiff", () => {
expect( expect(testProperty.type.serializeDiff(testMapValue)).toEqual({
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", test: "1.52",
another: "55", 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); expect(testProperty.type.serializeDiff(null)).toEqual(null);
testProperty.type.resetDiff(undefined); expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined);
testProperty.type.resetDiff(null); });
{ test("invalid parameters", () => {
// Test that keys and values are cloned in a different map. expect(() => testProperty.type.serializeDiff(5 as any)).toThrowError(
const clonedTestMapValue = testProperty.type.clone(testMapValue); InvalidTypeValueError,
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( expect(() => testProperty.type.serializeDiff([] as any)).toThrowError(
propertyValue.get("test"), InvalidTypeValueError,
); );
} expect(() => testProperty.type.serializeDiff({} as any)).toThrowError(
expect(testProperty.type.clone(undefined)).toBe(undefined); InvalidTypeValueError,
expect(testProperty.type.clone(null)).toBe(null); );
});
});
describe("hasChanged", () => {
test("hasChanged", () => {
const anotherTestMapValue = new Map<string, number>();
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. // Apply a patch with undefined / NULL values.
expect( 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", identifier: "id",
}); });
test("model type definition", () => { test("definition", () => {
const modelType = s.property.model(testModel); const modelType = s.property.model(testModel);
expect(modelType.type).toBeInstanceOf(ModelType); expect(modelType.type).toBeInstanceOf(ModelType);
}); });
test("model type functions", () => { describe("serialize", () => {
{ test("serialize", () => {
// Try to serialize / deserialize.
const testModelInstance = testModel.model( const testModelInstance = testModel.model(
Object.assign(new TestModel(), { Object.assign(new TestModel(), {
id: 1, id: 1,
@ -33,9 +32,40 @@ describe("model type", () => {
price: 12.548777, price: 12.548777,
}), }),
).instance; ).instance;
expect( expect(
s.property.model(testModel).type.serialize(testModelInstance), s.property.model(testModel).type.serialize(testModelInstance),
).toEqual({id: 1, name: "test", price: "12.548777"}); ).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( expect(
testModel testModel
.model( .model(
@ -45,9 +75,25 @@ describe("model type", () => {
) )
.getInstanceProperties(), .getInstanceProperties(),
).toEqual(testModel.model(testModelInstance).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. // Try to serialize the difference.
const testModelInstance = testModel.model( const testModelInstance = testModel.model(
Object.assign(new TestModel(), { Object.assign(new TestModel(), {
@ -56,177 +102,234 @@ describe("model type", () => {
price: 12.548777, price: 12.548777,
}), }),
).instance; ).instance;
testModelInstance.name = "new"; testModelInstance.name = "new";
expect( expect(
s.property.model(testModel).type.serializeDiff(testModelInstance), s.property.model(testModel).type.serializeDiff(testModelInstance),
).toEqual({id: 1, name: "new"}); ).toEqual({id: 1, name: "new"});
}
expect(s.property.model(testModel).type.serialize(null)).toEqual(null); expect(s.property.model(testModel).type.serializeDiff(null)).toEqual(
expect(s.property.model(testModel).type.deserialize(null)).toEqual(null); 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( test("invalid parameters", () => {
undefined, expect(() =>
); s.property.model(testModel).type.serializeDiff(5 as any),
expect(s.property.model(testModel).type.deserialize(undefined)).toEqual( ).toThrowError(InvalidTypeValueError);
undefined, expect(() =>
); s.property.model(testModel).type.serializeDiff([] as any),
expect(s.property.model(testModel).type.serializeDiff(undefined)).toEqual( ).toThrowError(InvalidTypeValueError);
undefined, expect(() =>
); s.property.model(testModel).type.serializeDiff(new (class {})() as any),
).toThrowError(InvalidTypeValueError);
});
});
{ describe("hasChanged", () => {
const testModelInstance = testModel.model( test("hasChanged", () => {
Object.assign(new TestModel(), { {
id: 1, const testModelInstance = testModel.model(
name: "test", Object.assign(new TestModel(), {
price: 12.548777, id: 1,
}), name: "test",
).instance; 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( expect(
s.property s.property.model(testModel).type.hasChanged(null, null),
.model(testModel)
.type.hasChanged(testModelInstance, testModelInstance),
).toBeFalsy(); ).toBeFalsy();
}
{
const testModelInstance = testModel.model(
Object.assign(new TestModel(), {
id: 1,
name: "test",
price: 12.548777,
}),
).instance;
testModelInstance.price = 12.548778;
expect( expect(
s.property s.property.model(testModel).type.hasChanged(undefined, undefined),
.model(testModel) ).toBeFalsy();
.type.hasChanged(testModelInstance, testModelInstance), expect(
s.property.model(testModel).type.hasChanged(null, undefined),
).toBeTruthy(); ).toBeTruthy();
} expect(
expect(s.property.model(testModel).type.hasChanged(null, null)).toBeFalsy(); s.property.model(testModel).type.hasChanged(undefined, null),
expect( ).toBeTruthy();
s.property.model(testModel).type.hasChanged(undefined, undefined), expect(
).toBeFalsy(); s.property.model(testModel).type.hasChanged(
expect( null,
s.property.model(testModel).type.hasChanged(null, undefined), testModel.model(
).toBeTruthy(); Object.assign(new TestModel(), {
expect( id: 1,
s.property.model(testModel).type.hasChanged(undefined, null), name: "test",
).toBeTruthy(); price: 12.548777,
expect( }),
s.property.model(testModel).type.hasChanged( ).instance,
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"},
), ),
).toBeFalsy(); ).toBeTruthy();
expect( expect(
s.property s.property.model(testModel).type.hasChanged(
.model(testModel) undefined,
.type.serializedHasChanged( testModel.model(
{id: 1, name: "test", price: "12.548777"}, Object.assign(new TestModel(), {
{id: 1, name: "test", price: "12.548778"}, id: 1,
name: "test",
price: 12.548777,
}),
).instance,
), ),
).toBeTruthy(); ).toBeTruthy();
expect( expect(
s.property.model(testModel).type.serializedHasChanged(null, null), s.property.model(testModel).type.hasChanged(
).toBeFalsy(); testModel.model(
expect( Object.assign(new TestModel(), {
s.property id: 1,
.model(testModel) name: "test",
.type.serializedHasChanged(undefined, undefined), price: 12.548777,
).toBeFalsy(); }),
expect( ).instance,
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, null,
), ),
).toBeTruthy(); ).toBeTruthy();
expect( expect(
s.property s.property.model(testModel).type.hasChanged(
.model(testModel) testModel.model(
.type.serializedHasChanged( Object.assign(new TestModel(), {
{id: 1, name: "test", price: "12.548777"}, id: 1,
name: "test",
price: 12.548777,
}),
).instance,
undefined, undefined,
), ),
).toBeTruthy(); ).toBeTruthy();
});
{ test("invalid parameters", () => {
// Serializing the difference to check that the difference has been reset. 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( const testModelInstance = testModel.model(
Object.assign(new TestModel(), { Object.assign(new TestModel(), {
id: 1, id: 1,
@ -234,6 +337,7 @@ describe("model type", () => {
price: 12.548777, price: 12.548777,
}), }),
).instance; ).instance;
testModelInstance.price = 555.555; testModelInstance.price = 555.555;
expect(testModel.model(testModelInstance).serializeDiff()).toEqual({ expect(testModel.model(testModelInstance).serializeDiff()).toEqual({
id: 1, id: 1,
@ -243,12 +347,26 @@ describe("model type", () => {
expect(testModel.model(testModelInstance).serializeDiff()).toEqual({ expect(testModel.model(testModelInstance).serializeDiff()).toEqual({
id: 1, id: 1,
}); });
}
s.property.model(testModel).type.resetDiff(undefined); s.property.model(testModel).type.resetDiff(undefined);
s.property.model(testModel).type.resetDiff(null); 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. // Test that values are cloned in a different model instance.
const testModelInstance = testModel.model( const testModelInstance = testModel.model(
Object.assign(new TestModel(), { Object.assign(new TestModel(), {
@ -268,10 +386,25 @@ describe("model type", () => {
expect(testModel.model(clonedModelInstance).serializeDiff()).toEqual( expect(testModel.model(clonedModelInstance).serializeDiff()).toEqual(
testModel.model(testModelInstance).serializeDiff(), 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. // Apply a patch with undefined / NULL values.
expect( 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,68 +2,155 @@ import {describe, expect, test} from "vitest";
import {InvalidTypeValueError, NumericType, s} from "../../../src/library"; import {InvalidTypeValueError, NumericType, s} from "../../../src/library";
describe("numeric type", () => { describe("numeric type", () => {
test("numeric type definition", () => { test("definition", () => {
const numericType = s.property.numeric(); const numericType = s.property.numeric();
expect(numericType.type).toBeInstanceOf(NumericType); expect(numericType.type).toBeInstanceOf(NumericType);
}); });
test("numeric type functions", () => { describe("serialize", () => {
expect(s.property.numeric().type.serialize(5.257)).toBe(5.257); test("serialize", () => {
expect(s.property.numeric().type.deserialize(5.257)).toBe(5.257); expect(s.property.numeric().type.serialize(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.serialize(null)).toBe(null);
expect(s.property.numeric().type.deserialize(null)).toBe(null); expect(s.property.numeric().type.serialize(undefined)).toBe(undefined);
expect(s.property.numeric().type.serializeDiff(null)).toBe(null); });
expect(s.property.numeric().type.serialize(undefined)).toBe(undefined); test("invalid parameters", () => {
expect(s.property.numeric().type.deserialize(undefined)).toBe(undefined); expect(() => s.property.numeric().type.serialize({} as any)).toThrowError(
expect(s.property.numeric().type.serializeDiff(undefined)).toBe(undefined); InvalidTypeValueError,
);
});
});
expect(s.property.numeric().type.hasChanged(5.257, 5.257)).toBeFalsy(); describe("deserialize", () => {
expect(s.property.numeric().type.hasChanged(null, null)).toBeFalsy(); test("deserialize", () => {
expect( expect(s.property.numeric().type.deserialize(5.257)).toBe(5.257);
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();
expect( expect(s.property.numeric().type.deserialize(null)).toBe(null);
s.property.numeric().type.serializedHasChanged(5.257, 5.257), expect(s.property.numeric().type.deserialize(undefined)).toBe(undefined);
).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();
s.property.numeric().type.resetDiff(5.257); test("invalid parameters", () => {
s.property.numeric().type.resetDiff(undefined); expect(() =>
s.property.numeric().type.resetDiff(null); 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(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(null, 5.257, true)).toBe(5.257);
expect(s.property.numeric().type.applyPatch(undefined, 5.257, false)).toBe( expect(s.property.numeric().type.applyPatch(undefined, 5.257, false)).toBe(
@ -74,32 +161,4 @@ describe("numeric type", () => {
).toBeUndefined(); ).toBeUndefined();
expect(s.property.numeric().type.applyPatch(5.257, null, false)).toBeNull(); 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"; } from "../../../src/library";
describe("object type", () => { describe("object type", () => {
test("object type definition", () => { test("definition", () => {
const objectType = s.property.object({ const objectType = s.property.object({
test: s.property.string(), test: s.property.string(),
another: s.property.numeric(), another: s.property.numeric(),
@ -31,132 +31,226 @@ describe("object type", () => {
another: s.property.decimal(), another: s.property.decimal(),
}); });
test("object type functions", () => { describe("serialize", () => {
expect( test("serialize", () => {
testProperty.type.serialize({test: "test", another: 12.548777}), expect(
).toEqual({test: "test", another: "12.548777"}); testProperty.type.serialize({test: "test", another: 12.548777}),
expect( ).toEqual({test: "test", another: "12.548777"});
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"});
expect(testProperty.type.serialize(null)).toEqual(null); expect(testProperty.type.serialize(null)).toEqual(null);
expect(testProperty.type.deserialize(null)).toEqual(null); expect(testProperty.type.serialize(undefined)).toEqual(undefined);
expect(testProperty.type.serializeDiff(null)).toEqual(null); });
expect(testProperty.type.serialize(undefined)).toEqual(undefined); test("invalid parameters", () => {
expect(testProperty.type.deserialize(undefined)).toEqual(undefined); expect(() => testProperty.type.serialize(5 as any)).toThrowError(
expect(testProperty.type.serializeDiff(undefined)).toEqual(undefined); InvalidTypeValueError,
);
expect(() => testProperty.type.serialize([] as any)).toThrowError(
InvalidTypeValueError,
);
});
});
expect( describe("deserialize", () => {
testProperty.type.hasChanged( test("deserialize", () => {
{test: "test", another: 12.548777}, expect(
{another: 12.548777, test: "test"}, testProperty.type.deserialize({test: "test", another: "12.548777"}),
), ).toEqual({test: "test", another: 12.548777});
).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();
expect( expect(testProperty.type.deserialize(null)).toEqual(null);
testProperty.type.serializedHasChanged( expect(testProperty.type.deserialize(undefined)).toEqual(undefined);
{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();
testProperty.type.resetDiff({test: "test", another: 12.548777}); test("invalid parameters", () => {
testProperty.type.resetDiff(undefined); expect(() => testProperty.type.deserialize(5 as any)).toThrowError(
testProperty.type.resetDiff(null); InvalidTypeValueError,
);
expect(() => testProperty.type.deserialize([] as any)).toThrowError(
InvalidTypeValueError,
);
});
});
{ describe("serializeDiff", () => {
// Test that values are cloned in a different object. test("serializeDiff", () => {
const propertyValue = {test: "test", another: 12.548777}; expect(
const clonedPropertyValue = testProperty.type.clone(propertyValue); testProperty.type.serializeDiff({test: "test", another: 12.548777}),
expect(clonedPropertyValue).not.toBe(propertyValue); ).toEqual({test: "test", another: "12.548777"});
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);
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. // Apply a patch with undefined / NULL values.
expect( 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,68 +2,149 @@ import {describe, expect, test} from "vitest";
import {s, StringType} from "../../../src/library"; import {s, StringType} from "../../../src/library";
describe("string type", () => { describe("string type", () => {
test("string type definition", () => { test("definition", () => {
const stringType = s.property.string(); const stringType = s.property.string();
expect(stringType.type).toBeInstanceOf(StringType); expect(stringType.type).toBeInstanceOf(StringType);
}); });
test("string type functions", () => { describe("serialize", () => {
expect(s.property.string().type.serialize("test")).toBe("test"); test("serialize", () => {
expect(s.property.string().type.deserialize("test")).toBe("test"); expect(s.property.string().type.serialize("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.serialize(undefined)).toBe(undefined);
});
expect(s.property.string().type.serialize(null)).toBe(null); test("invalid parameters", () => {
expect(s.property.string().type.deserialize(null)).toBe(null); const testDate = new Date();
expect(s.property.string().type.serializeDiff(null)).toBe(null); 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); describe("deserialize", () => {
expect(s.property.string().type.deserialize(undefined)).toBe(undefined); test("deserialize", () => {
expect(s.property.string().type.serializeDiff(undefined)).toBe(undefined); 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(); test("invalid parameters", () => {
expect(s.property.string().type.hasChanged(null, null)).toBeFalsy(); expect(s.property.string().type.deserialize({} as any)).toBe(
expect( "[object Object]",
s.property.string().type.hasChanged(undefined, undefined), );
).toBeFalsy(); expect(s.property.string().type.deserialize(2120 as any)).toBe("2120");
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();
expect( describe("serializeDiff", () => {
s.property.string().type.serializedHasChanged("test", "test"), test("serializeDiff", () => {
).toBeFalsy(); expect(s.property.string().type.serializeDiff("test")).toBe("test");
expect( expect(s.property.string().type.serializeDiff(null)).toBe(null);
s.property.string().type.serializedHasChanged(null, null), expect(s.property.string().type.serializeDiff(undefined)).toBe(undefined);
).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();
s.property.string().type.resetDiff("test"); test("invalid parameters", () => {
s.property.string().type.resetDiff(undefined); expect(s.property.string().type.serializeDiff({} as any)).toBe(
s.property.string().type.resetDiff(null); "[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( expect(s.property.string().type.applyPatch("another", "test", false)).toBe(
"test", "test",
); );
@ -78,36 +159,4 @@ describe("string type", () => {
).toBeUndefined(); ).toBeUndefined();
expect(s.property.string().type.applyPatch("test", null, false)).toBeNull(); 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({});
});
}); });