Compare commits
No commits in common. "main" and "v3.0.2" have entirely different histories.
9 changed files with 149 additions and 307 deletions
|
@ -19,7 +19,7 @@
|
|||
</p>
|
||||
|
||||
<p align="center">
|
||||
<img alt="Version 3.3.0" src="https://img.shields.io/badge/version-3.3.0-blue" />
|
||||
<img alt="Version 3.0.2" src="https://img.shields.io/badge/version-3.0.2-blue" />
|
||||
</p>
|
||||
|
||||
## Introduction
|
||||
|
@ -27,7 +27,7 @@
|
|||
Sharkitek is a Javascript / TypeScript library designed to ease development of client-side models.
|
||||
|
||||
With Sharkitek, you define the architecture of your models by specifying their properties and their types.
|
||||
Then, you can use the defined methods like `serialize`, `deserialize`, `patch` or `serializeDiff`.
|
||||
Then, you can use the defined methods like `serialize`, `deserialize`, `save` or `serializeDiff`.
|
||||
|
||||
```typescript
|
||||
class Example extends s.model({
|
||||
|
@ -227,7 +227,7 @@ const result = model.serializeDiff();
|
|||
// result = {}
|
||||
```
|
||||
|
||||
#### `patch()`
|
||||
#### `save()`
|
||||
|
||||
Get difference between original values and current ones, then reset it.
|
||||
Similar to call `serializeDiff()` then `resetDiff()`.
|
||||
|
@ -246,7 +246,7 @@ const model = (new TestModel()).deserialize({
|
|||
|
||||
model.title = "A new title for a new world";
|
||||
|
||||
const result = model.patch();
|
||||
const result = model.save();
|
||||
// if `id` is defined as the model identifier:
|
||||
// result = { id: 5, title: "A new title for a new world" }
|
||||
// if `id` is not defined as the model identifier:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@sharkitek/core",
|
||||
"version": "3.3.0",
|
||||
"version": "3.0.2",
|
||||
"description": "TypeScript library for well-designed model architectures.",
|
||||
"keywords": [
|
||||
"deserialization",
|
||||
|
|
|
@ -34,31 +34,16 @@ export type SerializedModel<Shape extends ModelShape> = {
|
|||
*/
|
||||
export type Model<Shape extends ModelShape, IdentifierType = unknown> = ModelDefinition<Shape, IdentifierType> & PropertiesModel<Shape>;
|
||||
|
||||
/**
|
||||
* Type of the extends function of model classes.
|
||||
*/
|
||||
export type ExtendsFunctionType<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends keyof Shape = any> =
|
||||
<Extension extends object>(extension: ThisType<ModelType> & Extension) => ModelClass<ModelType & Extension, Shape, Identifier>;
|
||||
|
||||
/**
|
||||
* Type of a model class.
|
||||
*/
|
||||
export type ModelClass<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends keyof Shape = any> = (
|
||||
ConstructorOf<ModelType> & {
|
||||
extends: ExtendsFunctionType<ModelType, Shape, Identifier>;
|
||||
}
|
||||
);
|
||||
export type ModelClass<Shape extends ModelShape, Identifier extends keyof Shape = any> = ConstructorOf<Model<Shape, IdentifierType<Shape, Identifier>>>;
|
||||
|
||||
/**
|
||||
* Identifier type.
|
||||
*/
|
||||
export type IdentifierType<Shape extends ModelShape, K extends keyof Shape> = Shape[K]["_sharkitek"];
|
||||
|
||||
/**
|
||||
* Identifier name type.
|
||||
*/
|
||||
export type IdentifierNameType<Shape> = Shape extends ModelShape ? keyof Shape : unknown;
|
||||
|
||||
/**
|
||||
* Interface of a Sharkitek model definition.
|
||||
*/
|
||||
|
@ -69,11 +54,6 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
|
|||
*/
|
||||
getIdentifier(): IdentifierType;
|
||||
|
||||
/**
|
||||
* Get model identifier name.
|
||||
*/
|
||||
getIdentifierName(): IdentifierNameType<Shape>;
|
||||
|
||||
/**
|
||||
* Serialize the model.
|
||||
*/
|
||||
|
@ -105,7 +85,7 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
|
|||
* Get difference between original values and current ones, then reset it.
|
||||
* Similar to call `serializeDiff()` then `resetDiff()`.
|
||||
*/
|
||||
patch(): Partial<SerializedModel<Shape>>;
|
||||
save(): Partial<SerializedModel<Shape>>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,184 +93,144 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
|
|||
* @param shape Model shape definition.
|
||||
* @param identifier Identifier property name.
|
||||
*/
|
||||
export function model<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends IdentifierNameType<Shape> = any>(
|
||||
export function model<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends keyof Shape = any>(
|
||||
shape: Shape,
|
||||
identifier?: Identifier,
|
||||
): ModelClass<ModelType, Shape, Identifier>
|
||||
): ConstructorOf<ModelType>
|
||||
{
|
||||
// Get shape entries.
|
||||
const shapeEntries = Object.entries(shape) as [keyof Shape, UnknownDefinition][];
|
||||
|
||||
return withExtends(
|
||||
// Initialize generic model class.
|
||||
class GenericModel implements ModelDefinition<Shape, IdentifierType<Shape, Identifier>, ModelType>
|
||||
return class GenericModel implements ModelDefinition<Shape, IdentifierType<Shape, Identifier>, ModelType>
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
// Initialize properties to undefined.
|
||||
Object.assign(this,
|
||||
// Build empty properties model from shape entries.
|
||||
Object.fromEntries(shapeEntries.map(([key]) => [key, undefined])) as PropertiesModel<Shape>
|
||||
);
|
||||
}
|
||||
// Initialize properties to undefined.
|
||||
Object.assign(this,
|
||||
// Build empty properties model from shape entries.
|
||||
Object.fromEntries(shapeEntries.map(([key]) => [key, undefined])) as PropertiesModel<Shape>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calling a function for each defined property.
|
||||
* @param callback - The function to call.
|
||||
* @protected
|
||||
*/
|
||||
protected forEachModelProperty<ReturnType>(callback: (propertyName: keyof Shape, propertyDefinition: UnknownDefinition) => ReturnType): ReturnType
|
||||
{
|
||||
for (const [propertyName, propertyDefinition] of shapeEntries)
|
||||
{ // For each property, checking that its type is defined and calling the callback with its type.
|
||||
// If the property is defined, calling the function with the property name and definition.
|
||||
const result = callback(propertyName, propertyDefinition);
|
||||
// If there is a return value, returning it directly (loop is broken).
|
||||
if (typeof result !== "undefined") return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The original properties values.
|
||||
* @protected
|
||||
*/
|
||||
protected _originalProperties: Partial<PropertiesModel<Shape>> = {};
|
||||
|
||||
/**
|
||||
* The original (serialized) object.
|
||||
* @protected
|
||||
*/
|
||||
protected _originalObject: SerializedModel<Shape>|null = null;
|
||||
|
||||
|
||||
|
||||
getIdentifier(): IdentifierType<Shape, Identifier>
|
||||
{
|
||||
return (this as PropertiesModel<Shape>)?.[identifier];
|
||||
}
|
||||
|
||||
getIdentifierName(): IdentifierNameType<Shape>
|
||||
{
|
||||
return identifier;
|
||||
}
|
||||
|
||||
serialize(): SerializedModel<Shape>
|
||||
{
|
||||
// Creating an empty (=> partial) serialized object.
|
||||
const serializedObject: Partial<SerializedModel<Shape>> = {};
|
||||
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each defined model property, adding it to the serialized object.
|
||||
serializedObject[propertyName] = propertyDefinition.type.serialize((this as PropertiesModel<Shape>)?.[propertyName]);
|
||||
});
|
||||
|
||||
return serializedObject as SerializedModel<Shape>; // Returning the serialized object.
|
||||
}
|
||||
|
||||
deserialize(obj: SerializedModel<Shape>): ModelType
|
||||
{
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each defined model property, assigning its deserialized value.
|
||||
(this as PropertiesModel<Shape>)[propertyName] = propertyDefinition.type.deserialize(obj[propertyName]);
|
||||
});
|
||||
|
||||
// Reset original property values.
|
||||
this.resetDiff();
|
||||
|
||||
this._originalObject = obj; // The model is not a new one, but loaded from a deserialized one. Storing it.
|
||||
|
||||
return this as unknown as ModelType;
|
||||
}
|
||||
|
||||
|
||||
isNew(): boolean
|
||||
{
|
||||
return !this._originalObject;
|
||||
}
|
||||
|
||||
isDirty(): boolean
|
||||
{
|
||||
return this.forEachModelProperty((propertyName, propertyDefinition) => (
|
||||
// For each property, checking if it is different.
|
||||
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as PropertiesModel<Shape>)[propertyName])
|
||||
// There is a difference, we should return false.
|
||||
? true
|
||||
// There is no difference, returning nothing.
|
||||
: undefined
|
||||
)) === true;
|
||||
}
|
||||
|
||||
|
||||
serializeDiff(): Partial<SerializedModel<Shape>>
|
||||
{
|
||||
// Creating an empty (=> partial) serialized object.
|
||||
const serializedObject: Partial<SerializedModel<Shape>> = {};
|
||||
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each defined model property, adding it to the serialized object if it has changed or if it is the identifier.
|
||||
if (
|
||||
identifier == propertyName ||
|
||||
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as PropertiesModel<Shape>)[propertyName])
|
||||
) // Adding the current property to the serialized object if it is the identifier or its value has changed.
|
||||
serializedObject[propertyName] = propertyDefinition.type.serializeDiff((this as PropertiesModel<Shape>)?.[propertyName]);
|
||||
});
|
||||
|
||||
return serializedObject; // Returning the serialized object.
|
||||
}
|
||||
|
||||
resetDiff(): void
|
||||
{
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each property, set its original value to its current property value.
|
||||
this._originalProperties[propertyName] = structuredClone(this as PropertiesModel<Shape>)[propertyName];
|
||||
propertyDefinition.type.resetDiff((this as PropertiesModel<Shape>)[propertyName]);
|
||||
});
|
||||
}
|
||||
|
||||
patch(): Partial<SerializedModel<Shape>>
|
||||
{
|
||||
// Get the difference.
|
||||
const diff = this.serializeDiff();
|
||||
|
||||
// Once the difference has been obtained, reset it.
|
||||
this.resetDiff();
|
||||
|
||||
return diff; // Return the difference.
|
||||
}
|
||||
|
||||
} as unknown as ConstructorOf<ModelType>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Any Sharkitek model.
|
||||
*/
|
||||
export type AnyModel = Model<any, any>;
|
||||
/**
|
||||
* Any Sharkitek model class.
|
||||
*/
|
||||
export type AnyModelClass = ModelClass<AnyModel, any>;
|
||||
|
||||
/**
|
||||
* Add extends function to a model class.
|
||||
* @param genericModel The model class on which to add the extends function.
|
||||
*/
|
||||
function withExtends<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends keyof Shape = any>(
|
||||
genericModel: ConstructorOf<ModelType>
|
||||
): ModelClass<ModelType, Shape, Identifier>
|
||||
{
|
||||
return Object.assign(
|
||||
genericModel,
|
||||
{ // Extends function definition.
|
||||
extends<Extension extends object>(extension: Extension): ModelClass<ModelType & Extension, Shape, Identifier>
|
||||
{
|
||||
// Clone the model class and add extends function.
|
||||
const classClone = withExtends(class extends (genericModel as AnyModelClass) {} as AnyModelClass as ConstructorOf<ModelType & Extension>);
|
||||
// Add extension to the model class prototype.
|
||||
Object.assign(classClone.prototype, extension);
|
||||
return classClone;
|
||||
/**
|
||||
* Calling a function for each defined property.
|
||||
* @param callback - The function to call.
|
||||
* @protected
|
||||
*/
|
||||
protected forEachModelProperty<ReturnType>(callback: (propertyName: keyof Shape, propertyDefinition: UnknownDefinition) => ReturnType): ReturnType
|
||||
{
|
||||
for (const [propertyName, propertyDefinition] of shapeEntries)
|
||||
{ // For each property, checking that its type is defined and calling the callback with its type.
|
||||
// If the property is defined, calling the function with the property name and definition.
|
||||
const result = callback(propertyName, propertyDefinition);
|
||||
// If there is a return value, returning it directly (loop is broken).
|
||||
if (typeof result !== "undefined") return result;
|
||||
}
|
||||
}
|
||||
) as AnyModelClass as ModelClass<ModelType, Shape, Identifier>;
|
||||
|
||||
|
||||
/**
|
||||
* The original properties values.
|
||||
* @protected
|
||||
*/
|
||||
protected _originalProperties: Partial<PropertiesModel<Shape>> = {};
|
||||
|
||||
/**
|
||||
* The original (serialized) object.
|
||||
* @protected
|
||||
*/
|
||||
protected _originalObject: SerializedModel<Shape>|null = null;
|
||||
|
||||
|
||||
|
||||
getIdentifier(): IdentifierType<Shape, Identifier>
|
||||
{
|
||||
return (this as PropertiesModel<Shape>)?.[identifier];
|
||||
}
|
||||
|
||||
serialize(): SerializedModel<Shape>
|
||||
{
|
||||
// Creating an empty (=> partial) serialized object.
|
||||
const serializedObject: Partial<SerializedModel<Shape>> = {};
|
||||
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each defined model property, adding it to the serialized object.
|
||||
serializedObject[propertyName] = propertyDefinition.type.serialize((this as PropertiesModel<Shape>)?.[propertyName]);
|
||||
});
|
||||
|
||||
return serializedObject as SerializedModel<Shape>; // Returning the serialized object.
|
||||
}
|
||||
|
||||
deserialize(obj: SerializedModel<Shape>): ModelType
|
||||
{
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each defined model property, assigning its deserialized value.
|
||||
(this as PropertiesModel<Shape>)[propertyName] = propertyDefinition.type.deserialize(obj[propertyName]);
|
||||
});
|
||||
|
||||
// Reset original property values.
|
||||
this.resetDiff();
|
||||
|
||||
this._originalObject = obj; // The model is not a new one, but loaded from a deserialized one. Storing it.
|
||||
|
||||
return this as unknown as ModelType;
|
||||
}
|
||||
|
||||
|
||||
isNew(): boolean
|
||||
{
|
||||
return !this._originalObject;
|
||||
}
|
||||
|
||||
isDirty(): boolean
|
||||
{
|
||||
return this.forEachModelProperty((propertyName, propertyDefinition) => (
|
||||
// For each property, checking if it is different.
|
||||
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as PropertiesModel<Shape>)[propertyName])
|
||||
// There is a difference, we should return false.
|
||||
? true
|
||||
// There is no difference, returning nothing.
|
||||
: undefined
|
||||
)) === true;
|
||||
}
|
||||
|
||||
|
||||
serializeDiff(): Partial<SerializedModel<Shape>>
|
||||
{
|
||||
// Creating an empty (=> partial) serialized object.
|
||||
const serializedObject: Partial<SerializedModel<Shape>> = {};
|
||||
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each defined model property, adding it to the serialized object if it has changed or if it is the identifier.
|
||||
if (
|
||||
identifier == propertyName ||
|
||||
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as PropertiesModel<Shape>)[propertyName])
|
||||
) // Adding the current property to the serialized object if it is the identifier or its value has changed.
|
||||
serializedObject[propertyName] = propertyDefinition.type.serializeDiff((this as PropertiesModel<Shape>)?.[propertyName]);
|
||||
});
|
||||
|
||||
return serializedObject; // Returning the serialized object.
|
||||
}
|
||||
|
||||
resetDiff(): void
|
||||
{
|
||||
this.forEachModelProperty((propertyName, propertyDefinition) => {
|
||||
// For each property, set its original value to its current property value.
|
||||
this._originalProperties[propertyName] = (this as PropertiesModel<Shape>)[propertyName];
|
||||
propertyDefinition.type.resetDiff((this as PropertiesModel<Shape>)[propertyName]);
|
||||
});
|
||||
}
|
||||
|
||||
save(): Partial<SerializedModel<Shape>>
|
||||
{
|
||||
// Get the difference.
|
||||
const diff = this.serializeDiff();
|
||||
|
||||
// Once the difference has been obtained, reset it.
|
||||
this.resetDiff();
|
||||
|
||||
return diff; // Return the difference.
|
||||
}
|
||||
|
||||
} as unknown as ConstructorOf<ModelType>;
|
||||
}
|
||||
|
|
|
@ -54,40 +54,6 @@ export class ArrayType<SerializedValueType, SharkitekValueType> extends Type<Ser
|
|||
// Reset diff of all elements.
|
||||
value.forEach((value) => this.valueDefinition.type.resetDiff(value));
|
||||
}
|
||||
|
||||
propertyHasChanged(originalValue: SharkitekValueType[]|null|undefined, currentValue: SharkitekValueType[]|null|undefined): boolean
|
||||
{
|
||||
// If any array length is different, arrays are different.
|
||||
if (originalValue?.length != currentValue?.length) return true;
|
||||
// If length is undefined, values are probably not arrays.
|
||||
if (originalValue?.length == undefined) return false;
|
||||
|
||||
for (const key of originalValue.keys())
|
||||
{ // Check for any change for each value in the array.
|
||||
if (this.valueDefinition.type.propertyHasChanged(originalValue[key], currentValue[key]))
|
||||
// The value has changed, the array is different.
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // No change detected.
|
||||
}
|
||||
|
||||
serializedPropertyHasChanged(originalValue: SerializedValueType[] | null | undefined, currentValue: SerializedValueType[] | null | undefined): boolean
|
||||
{
|
||||
// If any array length is different, arrays are different.
|
||||
if (originalValue?.length != currentValue?.length) return true;
|
||||
// If length is undefined, values are probably not arrays.
|
||||
if (originalValue?.length == undefined) return false;
|
||||
|
||||
for (const key of originalValue.keys())
|
||||
{ // Check for any change for each value in the array.
|
||||
if (this.valueDefinition.type.serializedPropertyHasChanged(originalValue[key], currentValue[key]))
|
||||
// The value has changed, the array is different.
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // No change detected.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,11 +21,6 @@ export class DateType extends Type<string, Date>
|
|||
|
||||
return value?.toISOString();
|
||||
}
|
||||
|
||||
propertyHasChanged(originalValue: Date|null|undefined, currentValue: Date|null|undefined): boolean
|
||||
{
|
||||
return originalValue?.toISOString() != currentValue?.toISOString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -48,15 +48,6 @@ export class ModelType<Shape extends ModelShape> extends Type<SerializedModel<Sh
|
|||
// Reset diff of the given model.
|
||||
value?.resetDiff();
|
||||
}
|
||||
|
||||
propertyHasChanged(originalValue: Model<Shape>|null|undefined, currentValue: Model<Shape>|null|undefined): boolean
|
||||
{
|
||||
if (originalValue === undefined) return currentValue !== undefined;
|
||||
if (originalValue === null) return currentValue !== null;
|
||||
|
||||
// If the current value is dirty, property has changed.
|
||||
return currentValue.isDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -66,46 +66,6 @@ export class ObjectType<Shape extends ModelShape> extends Type<SerializedModel<S
|
|||
fieldDefinition.type.resetDiff(value?.[fieldName]);
|
||||
});
|
||||
}
|
||||
|
||||
propertyHasChanged(originalValue: PropertiesModel<Shape>|null|undefined, currentValue: PropertiesModel<Shape>|null|undefined): boolean
|
||||
{
|
||||
// Get keys arrays.
|
||||
const originalKeys = Object.keys(originalValue) as (keyof Shape)[];
|
||||
const currentKeys = Object.keys(currentValue) as (keyof Shape)[];
|
||||
|
||||
if (originalKeys.join(",") != currentKeys.join(","))
|
||||
// Keys have changed, objects are different.
|
||||
return true;
|
||||
|
||||
for (const key of originalKeys)
|
||||
{ // Check for any change for each value in the object.
|
||||
if (this.shape[key].type.propertyHasChanged(originalValue[key], currentValue[key]))
|
||||
// The value has changed, the object is different.
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // No change detected.
|
||||
}
|
||||
|
||||
serializedPropertyHasChanged(originalValue: SerializedModel<Shape>|null|undefined, currentValue: SerializedModel<Shape>|null|undefined): boolean
|
||||
{
|
||||
// Get keys arrays.
|
||||
const originalKeys = Object.keys(originalValue) as (keyof Shape)[];
|
||||
const currentKeys = Object.keys(currentValue) as (keyof Shape)[];
|
||||
|
||||
if (originalKeys.join(",") != currentKeys.join(","))
|
||||
// Keys have changed, objects are different.
|
||||
return true;
|
||||
|
||||
for (const key of originalKeys)
|
||||
{ // Check for any change for each value in the object.
|
||||
if (this.shape[key].type.serializedPropertyHasChanged(originalValue[key], currentValue[key]))
|
||||
// The value has changed, the object is different.
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // No change detected.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,11 +9,6 @@ class Author extends s.model({
|
|||
email: s.property.string(),
|
||||
createdAt: s.property.date(),
|
||||
active: s.property.bool(),
|
||||
}).extends({
|
||||
extension(): string
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
})
|
||||
{
|
||||
active: boolean = true;
|
||||
|
@ -109,7 +104,7 @@ it("create and check state then serialize", () => {
|
|||
});
|
||||
|
||||
|
||||
it("deserialize then patch", () => {
|
||||
it("deserialize then save", () => {
|
||||
const article = (new Article()).deserialize({
|
||||
id: 1,
|
||||
title: "this is a test",
|
||||
|
@ -130,39 +125,35 @@ it("deserialize then patch", () => {
|
|||
|
||||
expect(article.isDirty()).toBeTruthy();
|
||||
|
||||
expect(article.patch()).toStrictEqual({
|
||||
expect(article.save()).toStrictEqual({
|
||||
id: 1,
|
||||
text: "Modified text.",
|
||||
});
|
||||
});
|
||||
|
||||
it("patch with modified submodels", () => {
|
||||
it("save with modified submodels", () => {
|
||||
const article = (new Article()).deserialize({
|
||||
id: 1,
|
||||
title: "this is a test",
|
||||
authors: [
|
||||
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: (new Date()).toISOString(), active: true, },
|
||||
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: (new Date("1997-09-09")).toISOString(), active: false, },
|
||||
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: (new Date()).toISOString(), active: false, },
|
||||
],
|
||||
text: "this is a long test.",
|
||||
evaluation: "25.23",
|
||||
tags: [ {name: "test"}, {name: "foo"} ],
|
||||
});
|
||||
|
||||
article.authors[0].name = "TEST";
|
||||
article.authors[1].createdAt.setMonth(9);
|
||||
article.authors = article.authors.map((author) => {
|
||||
author.name = "TEST";
|
||||
return author;
|
||||
});
|
||||
|
||||
expect(article.patch()).toStrictEqual({
|
||||
expect(article.save()).toStrictEqual({
|
||||
id: 1,
|
||||
authors: [
|
||||
{ name: "TEST" },
|
||||
{ createdAt: (new Date("1997-10-09")).toISOString() }, //{ name: "TEST", firstName: "Another", email: "another@test.test" },
|
||||
{ name: "TEST", },
|
||||
{}, //{ name: "TEST", firstName: "Another", email: "another@test.test" },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("test author extension", () => {
|
||||
const author = new Author();
|
||||
author.name = "test name";
|
||||
expect(author.extension()).toStrictEqual("test name");
|
||||
});
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
"incremental": true,
|
||||
"sourceMap": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"resolveJsonModule": true,
|
||||
|
@ -25,6 +24,6 @@
|
|||
"lib": [
|
||||
"ESNext",
|
||||
"DOM"
|
||||
]
|
||||
],
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue