Skip to content

Commit 5911807

Browse files
committed
fix: correct import path for getPropertyName function and add UserSimple model with tests
1 parent 1cbb4ba commit 5911807

File tree

7 files changed

+142
-7
lines changed

7 files changed

+142
-7
lines changed

src/classes/Serializable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {deserializeProperty} from "../functions/DeserializeProperty.js";
55
import type {AcceptedTypes} from "../models/AcceptedType.js";
66
import {SerializationSettings} from "../models/SerializationSettings.js";
77
import {classToFormData} from "../functions/ClassToFormData.js";
8-
import {getPropertyName} from "../functions/GetProperyName.js";
8+
import {getPropertyName} from "../functions/GetPropertyName.js";
99
import {fromJSON} from "../functions/FromJSON.js";
1010

1111
/**

src/functions/ClassToFormData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-statements */
22
/* eslint-disable max-lines-per-function */
3-
import {getPropertyName} from "./GetProperyName.js";
3+
import {getPropertyName} from "./GetPropertyName.js";
44

55
/**
66
* Converts a class instance to FormData for use in AJAX forms.

src/functions/FromJSON.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {Serializable} from "../classes/Serializable";
77
import {AcceptedTypes} from "../models/AcceptedType";
88
import {SerializationSettings} from "../models/SerializationSettings";
99
import {deserializeProperty} from "./DeserializeProperty";
10-
import {getPropertyName} from "./GetProperyName.js";
10+
import {getPropertyName} from "./GetPropertyName.js";
1111
import {onWrongType} from "./OnWrongType.js";
1212

1313
// eslint-disable-next-line max-statements

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export {PascalCaseNamingStrategy} from "./naming-strategies/PascalCaseNamingStra
2525
export {KebabCaseNamingStrategy} from "./naming-strategies/KebabCaseNamingStrategy.js";
2626
export {CamelCaseNamingStrategy} from "./naming-strategies/CamelCaseNamingStrategy.js";
2727

28-
// Utils
28+
// Functions
2929
export {classToFormData} from "./functions/ClassToFormData.js";
30-
export {getPropertyName} from "./functions/GetProperyName.js";
30+
export {deserializeProperty} from "./functions/DeserializeProperty.js";
31+
export {fromJSON} from "./functions/FromJSON.js";
32+
export {getPropertyName} from "./functions/GetPropertyName.js";
33+
export {onWrongType} from "./functions/OnWrongType.js";
34+

tests/base-functions.spec.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {assert} from "chai";
55
import {describe, it} from "node:test";
66

77
import type {User as IUser, Friend as IFriend} from "./models/User";
8+
import type {UserSimple as IUserSimple, FriendSimple as IFriendSimple} from "./models/UserSimple";
9+
import {fromJSON} from "../src/functions/FromJSON";
810

911
describe("Base functions", () => {
1012
it("user from method fromJSON must be instance of User", async () => {
@@ -13,7 +15,7 @@ describe("Base functions", () => {
1315

1416
const [object] = Reflect.get(json, "default") as typeof json;
1517

16-
const user = new User().fromJSON(object);
18+
const user: IUser = new User().fromJSON(object);
1719

1820
assert.isTrue(user instanceof User);
1921
assert.strictEqual(user.id, object.id, "id is not equal");
@@ -81,7 +83,7 @@ describe("Base functions", () => {
8183
const json = await import("./jsons/json-generator.json", {with: {type: "json"}});
8284
const [object] = Reflect.get(json, "default") as typeof json;
8385

84-
const user = new User().fromString(JSON.stringify(object));
86+
const user: IUser = new User().fromString(JSON.stringify(object));
8587

8688
assert.isTrue(user instanceof User);
8789
assert.strictEqual(user.id, object.id, "id is not equal");
@@ -143,4 +145,39 @@ describe("Base functions", () => {
143145
assert.strictEqual(friend.name, object.friends[index].name, `friend ${String(index)} name is not equal`);
144146
});
145147
});
148+
149+
it("user from function fromJSON must be instance of User", async () => {
150+
const {UserSimple} = await import("./models/UserSimple");
151+
const json = await import("./jsons/json-generator.json", {with: {type: "json"}});
152+
153+
const [object] = Reflect.get(json, "default") as typeof json;
154+
155+
const user: IUserSimple = fromJSON(new UserSimple(), object);
156+
157+
assert.isTrue(user instanceof UserSimple);
158+
assert.strictEqual(user.id, object.id, "id is not equal");
159+
assert.strictEqual(user.index, object.index, "index is not equal");
160+
assert.strictEqual(user.guid, object.guid, "guid is not equal");
161+
assert.strictEqual(user.isActive, object.isActive, "isActive is not equal");
162+
assert.strictEqual(user.balance, object.balance, "balance is not equal");
163+
assert.strictEqual(user.picture, object.picture, "picture is not equal");
164+
assert.strictEqual(user.age, object.age, "age is not equal");
165+
assert.strictEqual(user.eyeColor, object.eyeColor, "eyeColor is not equal");
166+
assert.strictEqual(user.name, object.name, "name is not equal");
167+
assert.strictEqual(user.company, object.company, "company is not equal");
168+
assert.strictEqual(user.email, object.email, "email is not equal");
169+
assert.strictEqual(user.phone, object.phone, "phone is not equal");
170+
assert.strictEqual(user.address, object.address, "address is not equal");
171+
assert.strictEqual(user.about, object.about, "about is not equal");
172+
assert.strictEqual(user.latitude, object.latitude, "latitude is not equal");
173+
assert.strictEqual(user.longitude, object.longitude, "longitude is not equal");
174+
assert.deepEqual(user.tags, object.tags, "tags is not equal");
175+
assert.strictEqual(user.greeting, object.greeting, "greeting is not equal");
176+
assert.strictEqual(user.favoriteFruit, object.favoriteFruit, "favoriteFruit is not equal");
177+
178+
user.friends.forEach((friend: IFriendSimple, index: number) => {
179+
assert.strictEqual(friend.id, object.friends[index].id, `friend ${String(index)} id is not equal`);
180+
assert.strictEqual(friend.name, object.friends[index].name, `friend ${String(index)} name is not equal`);
181+
});
182+
});
146183
});

tests/models/UserSimple.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eslint-disable max-classes-per-file */
2+
import {jsonProperty} from "../../src/decorators/JsonProperty";
3+
import {jsonIgnore} from "../../src/decorators/JsonIgnore";
4+
5+
export class FriendSimple {
6+
7+
@jsonProperty(Number)
8+
public id: number = 0;
9+
10+
@jsonProperty(String)
11+
public name: string = "";
12+
13+
}
14+
15+
export class UserSimple {
16+
17+
@jsonProperty(String)
18+
public id?: string = "";
19+
20+
@jsonProperty(Number)
21+
public index: number = 0;
22+
23+
@jsonProperty(String)
24+
public guid: string = "";
25+
26+
@jsonProperty(Boolean)
27+
public isActive: boolean = false;
28+
29+
@jsonProperty(String)
30+
public balance: string = "";
31+
32+
@jsonProperty(String)
33+
public picture: string = "";
34+
35+
@jsonProperty(Number)
36+
public age: number = 0;
37+
38+
@jsonProperty(String)
39+
public eyeColor: string = "";
40+
41+
@jsonProperty(String)
42+
public name: string = "";
43+
44+
@jsonProperty(String)
45+
public gender: string = "";
46+
47+
@jsonProperty(String)
48+
public company: string = "";
49+
50+
@jsonProperty(String)
51+
public email: string = "";
52+
53+
@jsonProperty(String)
54+
public phone: string = "";
55+
56+
@jsonProperty(String)
57+
public address: string = "";
58+
59+
@jsonProperty(String)
60+
public about: string = "";
61+
62+
@jsonProperty(Date, null)
63+
public registered: Date | null = null;
64+
65+
@jsonProperty(Number)
66+
public latitude: number = 0;
67+
68+
@jsonProperty(Number)
69+
public longitude: number = 0;
70+
71+
@jsonProperty([String])
72+
public tags: string[] = [];
73+
74+
@jsonProperty([FriendSimple])
75+
public friends: FriendSimple[] = [];
76+
77+
@jsonProperty(String)
78+
public greeting: string = "";
79+
80+
@jsonProperty(String)
81+
public favoriteFruit: string = "";
82+
83+
@jsonIgnore()
84+
public isExpanded: boolean = false;
85+
86+
public getName (): string {
87+
return [
88+
this.email,
89+
this.company,
90+
this.address
91+
].join(" ");
92+
}
93+
94+
}

0 commit comments

Comments
 (0)