@@ -8,20 +8,14 @@ import {STRING} from '../../../';
8
8
import { Entity , ModelDefinition } from '../../../' ;
9
9
10
10
describe ( 'model' , ( ) => {
11
- const addressDef = new ModelDefinition ( 'Address' ) ;
12
- addressDef
13
- . addProperty ( 'street' , 'string' )
14
- . addProperty ( 'city' , 'string' )
15
- . addProperty ( 'state' , String )
16
- . addProperty ( 'zipCode' , STRING ) ;
17
-
18
11
const customerDef = new ModelDefinition ( 'Customer' ) ;
19
12
customerDef
20
13
. addProperty ( 'id' , 'string' )
21
14
. addProperty ( 'email' , 'string' )
22
15
. addProperty ( 'firstName' , String )
23
16
. addProperty ( 'lastName' , STRING )
24
17
. addProperty ( 'address' , 'object' )
18
+ . addProperty ( 'phones' , 'array' )
25
19
. addSetting ( 'id' , 'id' ) ;
26
20
27
21
const realmCustomerDef = new ModelDefinition ( 'RealmCustomer' ) ;
@@ -44,6 +38,13 @@ describe('model', () => {
44
38
. addProperty ( 'id' , { type : 'string' , id : true } )
45
39
. addSetting ( 'strict' , false ) ;
46
40
41
+ const addressDef = new ModelDefinition ( 'Address' ) ;
42
+ addressDef
43
+ . addProperty ( 'street' , 'string' )
44
+ . addProperty ( 'city' , 'string' )
45
+ . addProperty ( 'state' , String )
46
+ . addProperty ( 'zipCode' , STRING ) ;
47
+
47
48
class Address extends Entity {
48
49
static definition = addressDef ;
49
50
street : string ;
@@ -56,13 +57,27 @@ describe('model', () => {
56
57
}
57
58
}
58
59
60
+ const phoneDef = new ModelDefinition ( 'Phone' ) ;
61
+ phoneDef . addProperty ( 'number' , 'string' ) . addProperty ( 'label' , 'string' ) ;
62
+
63
+ class Phone extends Entity {
64
+ static definition = phoneDef ;
65
+ number : string ;
66
+ label : string ;
67
+
68
+ constructor ( data ?: Partial < Phone > ) {
69
+ super ( data ) ;
70
+ }
71
+ }
72
+
59
73
class Customer extends Entity {
60
74
static definition = customerDef ;
61
75
id : string ;
62
76
email : string ;
63
77
firstName : string ;
64
78
lastName : string ;
65
79
address ?: Address ;
80
+ phones ?: Phone [ ] ;
66
81
67
82
constructor ( data ?: Partial < Customer > ) {
68
83
super ( data ) ;
@@ -110,7 +125,7 @@ describe('model', () => {
110
125
return customer ;
111
126
}
112
127
113
- function createCustomerWithAddress ( ) {
128
+ function createCustomerWithContact ( ) {
114
129
const customer = new Customer ( ) ;
115
130
customer . id = '123' ;
116
131
customer . email = 'xyz@example.com' ;
@@ -120,6 +135,10 @@ describe('model', () => {
120
135
state : 'CA' ,
121
136
zipCode : '95131' ,
122
137
} ) ;
138
+ customer . phones = [
139
+ new Phone ( { label : 'home' , number : '111-222-3333' } ) ,
140
+ new Phone ( { label : 'work' , number : '111-222-5555' } ) ,
141
+ ] ;
123
142
return customer ;
124
143
}
125
144
@@ -159,7 +178,7 @@ describe('model', () => {
159
178
} ) ;
160
179
161
180
it ( 'converts to json recursively' , ( ) => {
162
- const customer = createCustomerWithAddress ( ) ;
181
+ const customer = createCustomerWithContact ( ) ;
163
182
expect ( customer . toJSON ( ) ) . to . eql ( {
164
183
id : '123' ,
165
184
email : 'xyz@example.com' ,
@@ -169,6 +188,10 @@ describe('model', () => {
169
188
state : 'CA' ,
170
189
zipCode : '95131' ,
171
190
} ,
191
+ phones : [
192
+ { label : 'home' , number : '111-222-3333' } ,
193
+ { label : 'work' , number : '111-222-5555' } ,
194
+ ] ,
172
195
} ) ;
173
196
} ) ;
174
197
@@ -191,7 +214,7 @@ describe('model', () => {
191
214
} ) ;
192
215
193
216
it ( 'converts to plain object recursively' , ( ) => {
194
- const customer = createCustomerWithAddress ( ) ;
217
+ const customer = createCustomerWithContact ( ) ;
195
218
Object . assign ( customer , { unknown : 'abc' } ) ;
196
219
Object . assign ( customer . address , { unknown : 'xyz' } ) ;
197
220
expect ( customer . toObject ( ) ) . to . eql ( {
@@ -203,6 +226,10 @@ describe('model', () => {
203
226
state : 'CA' ,
204
227
zipCode : '95131' ,
205
228
} ,
229
+ phones : [
230
+ { label : 'home' , number : '111-222-3333' } ,
231
+ { label : 'work' , number : '111-222-5555' } ,
232
+ ] ,
206
233
} ) ;
207
234
expect ( customer . toObject ( { ignoreUnknownProperties : false } ) ) . to . eql ( {
208
235
id : '123' ,
@@ -215,6 +242,10 @@ describe('model', () => {
215
242
zipCode : '95131' ,
216
243
unknown : 'xyz' ,
217
244
} ,
245
+ phones : [
246
+ { label : 'home' , number : '111-222-3333' } ,
247
+ { label : 'work' , number : '111-222-5555' } ,
248
+ ] ,
218
249
} ) ;
219
250
} ) ;
220
251
0 commit comments