@@ -9,7 +9,7 @@ import type {UserSimple as IUserSimple, FriendSimple as IFriendSimple} from "./m
99import { fromJSON } from "../src/functions/FromJSON" ;
1010
1111describe ( "Base functions" , ( ) => {
12- it ( "user from method fromJSON must be instance of User " , async ( ) => {
12+ it ( "should deserialize JSON to User instance using instance method fromJSON() " , async ( ) => {
1313 const { User} = await import ( "./models/User" ) ;
1414 const json = await import ( "./jsons/json-generator.json" , { with : { type : "json" } } ) ;
1515
@@ -44,7 +44,7 @@ describe("Base functions", () => {
4444 } ) ;
4545 } ) ;
4646
47- it ( "user from static method fromJSON must be instance of User " , async ( ) => {
47+ it ( "should deserialize JSON to User instance using static method fromJSON() " , async ( ) => {
4848 const { User} = await import ( "./models/User" ) ;
4949 const json = await import ( "./jsons/json-generator.json" , { with : { type : "json" } } ) ;
5050 const [ object ] = Reflect . get ( json , "default" ) as typeof json ;
@@ -78,7 +78,7 @@ describe("Base functions", () => {
7878 } ) ;
7979 } ) ;
8080
81- it ( "user from method fromString must be instance of User " , async ( ) => {
81+ it ( "should deserialize JSON string to User instance using instance method fromString() " , async ( ) => {
8282 const { User} = await import ( "./models/User" ) ;
8383 const json = await import ( "./jsons/json-generator.json" , { with : { type : "json" } } ) ;
8484 const [ object ] = Reflect . get ( json , "default" ) as typeof json ;
@@ -112,7 +112,7 @@ describe("Base functions", () => {
112112 } ) ;
113113 } ) ;
114114
115- it ( "user from static method fromString must be instance of User " , async ( ) => {
115+ it ( "should deserialize JSON string to User instance using static method fromString() " , async ( ) => {
116116 const { User} = await import ( "./models/User" ) ;
117117 const json = await import ( "./jsons/json-generator.json" , { with : { type : "json" } } ) ;
118118 const [ object ] = Reflect . get ( json , "default" ) as typeof json ;
@@ -146,7 +146,7 @@ describe("Base functions", () => {
146146 } ) ;
147147 } ) ;
148148
149- it ( "user from function fromJSON must be instance of User " , async ( ) => {
149+ it ( "should deserialize JSON to plain class instance using standalone fromJSON() function " , async ( ) => {
150150 const { UserSimple} = await import ( "./models/UserSimple" ) ;
151151 const json = await import ( "./jsons/json-generator.json" , { with : { type : "json" } } ) ;
152152
@@ -180,4 +180,39 @@ describe("Base functions", () => {
180180 assert . strictEqual ( friend . name , object . friends [ index ] . name , `friend ${ String ( index ) } name is not equal` ) ;
181181 } ) ;
182182 } ) ;
183+
184+ it ( "should deserialize JSON by passing class constructor to standalone fromJSON() function" , async ( ) => {
185+ const { UserSimple} = await import ( "./models/UserSimple" ) ;
186+ const json = await import ( "./jsons/json-generator.json" , { with : { type : "json" } } ) ;
187+
188+ const [ object ] = Reflect . get ( json , "default" ) as typeof json ;
189+
190+ const user : IUserSimple = fromJSON ( UserSimple , object ) ;
191+
192+ assert . isTrue ( user instanceof UserSimple ) ;
193+ assert . strictEqual ( user . id , object . id , "id is not equal" ) ;
194+ assert . strictEqual ( user . index , object . index , "index is not equal" ) ;
195+ assert . strictEqual ( user . guid , object . guid , "guid is not equal" ) ;
196+ assert . strictEqual ( user . isActive , object . isActive , "isActive is not equal" ) ;
197+ assert . strictEqual ( user . balance , object . balance , "balance is not equal" ) ;
198+ assert . strictEqual ( user . picture , object . picture , "picture is not equal" ) ;
199+ assert . strictEqual ( user . age , object . age , "age is not equal" ) ;
200+ assert . strictEqual ( user . eyeColor , object . eyeColor , "eyeColor is not equal" ) ;
201+ assert . strictEqual ( user . name , object . name , "name is not equal" ) ;
202+ assert . strictEqual ( user . company , object . company , "company is not equal" ) ;
203+ assert . strictEqual ( user . email , object . email , "email is not equal" ) ;
204+ assert . strictEqual ( user . phone , object . phone , "phone is not equal" ) ;
205+ assert . strictEqual ( user . address , object . address , "address is not equal" ) ;
206+ assert . strictEqual ( user . about , object . about , "about is not equal" ) ;
207+ assert . strictEqual ( user . latitude , object . latitude , "latitude is not equal" ) ;
208+ assert . strictEqual ( user . longitude , object . longitude , "longitude is not equal" ) ;
209+ assert . deepEqual ( user . tags , object . tags , "tags is not equal" ) ;
210+ assert . strictEqual ( user . greeting , object . greeting , "greeting is not equal" ) ;
211+ assert . strictEqual ( user . favoriteFruit , object . favoriteFruit , "favoriteFruit is not equal" ) ;
212+
213+ user . friends . forEach ( ( friend : IFriendSimple , index : number ) => {
214+ assert . strictEqual ( friend . id , object . friends [ index ] . id , `friend ${ String ( index ) } id is not equal` ) ;
215+ assert . strictEqual ( friend . name , object . friends [ index ] . name , `friend ${ String ( index ) } name is not equal` ) ;
216+ } ) ;
217+ } ) ;
183218} ) ;
0 commit comments