2024-10-03 23:33:00 +02:00
|
|
|
import {s} from "../src";
|
2022-07-31 10:49:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Another test model.
|
|
|
|
*/
|
2024-10-03 23:33:00 +02:00
|
|
|
class Author extends s.model({
|
|
|
|
name: s.property.string(),
|
|
|
|
firstName: s.property.string(),
|
|
|
|
email: s.property.string(),
|
|
|
|
createdAt: s.property.date(),
|
|
|
|
active: s.property.bool(),
|
|
|
|
})
|
2022-07-31 10:49:32 +02:00
|
|
|
{
|
2022-09-18 19:47:38 +02:00
|
|
|
active: boolean = true;
|
|
|
|
|
2024-09-28 14:43:16 +02:00
|
|
|
constructor(name: string = "", firstName: string = "", email: string = "", createdAt: Date = new Date())
|
2022-07-31 10:49:32 +02:00
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
this.firstName = firstName;
|
|
|
|
this.email = email;
|
2022-09-18 19:27:23 +02:00
|
|
|
this.createdAt = createdAt;
|
2022-07-31 10:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A test model.
|
|
|
|
*/
|
2024-10-03 23:33:00 +02:00
|
|
|
class Article extends s.model({
|
|
|
|
id: s.property.numeric(),
|
|
|
|
title: s.property.string(),
|
|
|
|
authors: s.property.array(s.property.model(Author)),
|
|
|
|
text: s.property.string(),
|
|
|
|
evaluation: s.property.decimal(),
|
|
|
|
tags: s.property.array(
|
|
|
|
s.property.object({
|
|
|
|
name: s.property.string(),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
}, "id")
|
2022-07-31 10:49:32 +02:00
|
|
|
{
|
2022-11-01 19:13:21 +01:00
|
|
|
id: number;
|
|
|
|
title: string;
|
2022-07-31 10:49:32 +02:00
|
|
|
authors: Author[] = [];
|
2022-11-01 19:13:21 +01:00
|
|
|
text: string;
|
|
|
|
evaluation: number;
|
2024-09-28 17:02:40 +02:00
|
|
|
tags: {
|
|
|
|
name: string;
|
|
|
|
}[];
|
2022-07-31 10:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
it("deserialize", () => {
|
|
|
|
expect((new Article()).deserialize({
|
|
|
|
id: 1,
|
|
|
|
title: "this is a test",
|
|
|
|
authors: [
|
2022-09-18 19:47:38 +02:00
|
|
|
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", active: true, },
|
|
|
|
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, },
|
2022-07-31 10:49:32 +02:00
|
|
|
],
|
|
|
|
text: "this is a long test.",
|
|
|
|
evaluation: "25.23",
|
2024-09-28 17:02:40 +02:00
|
|
|
tags: [ {name: "test"}, {name: "foo"} ],
|
2022-07-31 10:49:32 +02:00
|
|
|
}).serialize()).toStrictEqual({
|
|
|
|
id: 1,
|
|
|
|
title: "this is a test",
|
|
|
|
authors: [
|
2022-09-18 19:47:38 +02:00
|
|
|
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: "2022-08-07T08:47:01.000Z", active: true, },
|
|
|
|
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: "2022-09-07T18:32:55.000Z", active: false, },
|
2022-07-31 10:49:32 +02:00
|
|
|
],
|
|
|
|
text: "this is a long test.",
|
|
|
|
evaluation: "25.23",
|
2024-09-28 17:02:40 +02:00
|
|
|
tags: [ {name: "test"}, {name: "foo"} ],
|
2022-07-31 10:49:32 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("create and check state then serialize", () => {
|
2022-09-18 19:27:23 +02:00
|
|
|
const now = new Date();
|
2022-07-31 10:49:32 +02:00
|
|
|
const article = new Article();
|
|
|
|
article.id = 1;
|
|
|
|
article.title = "this is a test";
|
|
|
|
article.authors = [
|
2022-09-18 19:27:23 +02:00
|
|
|
new Author("DOE", "John", "test@test.test", now),
|
2022-07-31 10:49:32 +02:00
|
|
|
];
|
|
|
|
article.text = "this is a long test.";
|
|
|
|
article.evaluation = 25.23;
|
2024-09-28 17:02:40 +02:00
|
|
|
article.tags = [];
|
|
|
|
article.tags.push({name: "test"});
|
|
|
|
article.tags.push({name: "foo"});
|
2022-07-31 10:49:32 +02:00
|
|
|
|
|
|
|
expect(article.isNew()).toBeTruthy();
|
|
|
|
expect(article.getIdentifier()).toStrictEqual(1);
|
|
|
|
|
|
|
|
expect(article.serialize()).toStrictEqual({
|
|
|
|
id: 1,
|
|
|
|
title: "this is a test",
|
|
|
|
authors: [
|
2022-09-18 19:47:38 +02:00
|
|
|
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: now.toISOString(), active: true, },
|
2022-07-31 10:49:32 +02:00
|
|
|
],
|
|
|
|
text: "this is a long test.",
|
|
|
|
evaluation: "25.23",
|
2024-09-28 17:02:40 +02:00
|
|
|
tags: [ {name: "test"}, {name: "foo"} ],
|
2022-07-31 10:49:32 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it("deserialize then save", () => {
|
|
|
|
const article = (new Article()).deserialize({
|
|
|
|
id: 1,
|
|
|
|
title: "this is a test",
|
|
|
|
authors: [
|
2024-10-03 23:33:00 +02:00
|
|
|
{ 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()).toISOString(), active: false, },
|
2022-07-31 10:49:32 +02:00
|
|
|
],
|
|
|
|
text: "this is a long test.",
|
|
|
|
evaluation: "25.23",
|
2024-09-28 17:02:40 +02:00
|
|
|
tags: [ {name: "test"}, {name: "foo"} ],
|
2022-07-31 10:49:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(article.isNew()).toBeFalsy();
|
|
|
|
expect(article.isDirty()).toBeFalsy();
|
|
|
|
expect(article.evaluation).toStrictEqual(25.23);
|
|
|
|
|
|
|
|
article.text = "Modified text.";
|
|
|
|
|
|
|
|
expect(article.isDirty()).toBeTruthy();
|
|
|
|
|
|
|
|
expect(article.save()).toStrictEqual({
|
|
|
|
id: 1,
|
|
|
|
text: "Modified text.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("save with modified submodels", () => {
|
|
|
|
const article = (new Article()).deserialize({
|
|
|
|
id: 1,
|
|
|
|
title: "this is a test",
|
|
|
|
authors: [
|
2024-10-03 23:33:00 +02:00
|
|
|
{ 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()).toISOString(), active: false, },
|
2022-07-31 10:49:32 +02:00
|
|
|
],
|
|
|
|
text: "this is a long test.",
|
|
|
|
evaluation: "25.23",
|
2024-09-28 17:02:40 +02:00
|
|
|
tags: [ {name: "test"}, {name: "foo"} ],
|
2022-07-31 10:49:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
article.authors = article.authors.map((author) => {
|
|
|
|
author.name = "TEST";
|
|
|
|
return author;
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(article.save()).toStrictEqual({
|
|
|
|
id: 1,
|
|
|
|
authors: [
|
|
|
|
{ name: "TEST", },
|
|
|
|
{}, //{ name: "TEST", firstName: "Another", email: "another@test.test" },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|