@@ -9,23 +9,20 @@ import { NameModel } from './data/models/NameModel';
9
9
import { AdminModel } from './data/models/AdminModel' ;
10
10
11
11
describe ( 'BaseModel' , ( ) => {
12
- let json : any = null ;
12
+ let json = data ;
13
13
14
14
beforeEach ( ( ) => {
15
- json = Util . clone ( data as any ) ;
16
- } ) ;
17
-
18
- afterEach ( ( ) => {
19
- json = null ;
15
+ json = Util . clone ( data ) ;
20
16
} ) ;
21
17
22
18
test ( 'update returns itself' , ( ) => {
23
19
const baseModel = new BaseModel ( ) ;
20
+
24
21
expect ( baseModel . update ( ) ) . toEqual ( baseModel ) ;
25
22
} ) ;
26
23
27
24
test ( 'should populate InfoModel' , ( ) => {
28
- const model = new InfoModel ( json . info ) ;
25
+ const model = new InfoModel ( json . info as any ) ;
29
26
30
27
expect ( model . toJSON ( ) ) . toEqual ( {
31
28
...json . info ,
@@ -52,16 +49,14 @@ describe('BaseModel', () => {
52
49
53
50
expect ( model . info ) . toEqual ( null ) ;
54
51
expect ( model . results ) . toEqual ( [ ] ) ;
55
- expect ( model . sjsOptions ) . toEqual ( { expand : false } ) ;
56
52
} ) ;
57
53
58
54
test ( 'should clone and not mutate data' , ( ) => {
59
- const model = new UserResponseModel ( json ) ;
60
- const clone = model . clone < UserResponseModel > ( ) ;
55
+ const model = new UserResponseModel ( json as any ) ;
56
+ const clone = model . clone ( ) ;
61
57
62
58
clone . info . version = '888' ;
63
59
64
- expect ( model . info . sjsId ) . not . toEqual ( clone . info . sjsId ) ;
65
60
expect ( model . info ) . not . toEqual ( clone . info ) ;
66
61
expect ( model . toJSON ( ) ) . not . toEqual ( clone . toJSON ( ) ) ;
67
62
} ) ;
@@ -87,23 +82,31 @@ describe('BaseModel', () => {
87
82
} ) ;
88
83
89
84
test ( 'should have default of UserResponseModel with null passed in' , ( ) => {
90
- console . error ( 'Ignore the "Something is wrong!" errors. They are expected.' ) ;
85
+ const consoleError = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
91
86
92
87
const model = new UserResponseModel ( null as any ) ;
93
88
94
89
expect ( model . info ) . toEqual ( null ) ;
95
90
expect ( model . results ) . toEqual ( [ ] ) ;
96
- expect ( model . sjsOptions ) . toEqual ( { expand : false } ) ;
91
+ expect ( consoleError ) . toBeCalledWith (
92
+ 'Something is wrong! UserResponseModel only allows Objects but "null" was passed in.'
93
+ ) ;
94
+
95
+ consoleError . mockReset ( ) ;
97
96
} ) ;
98
97
99
98
test ( 'should have populate UserResponseModel' , ( ) => {
100
- const model = new UserResponseModel ( json ) ;
99
+ const model = new UserResponseModel ( json as any ) ;
101
100
102
101
const expectedData = {
103
102
...json ,
104
- results : json . results . map ( ( resultItem : any ) =>
105
- Util . deletePropertyFromObject ( resultItem , [ 'location' , 'login' , 'dob' , 'registered' , 'phone' , 'cell' , 'id' ] )
106
- ) ,
103
+ results : json . results . map ( ( resultItem ) => ( {
104
+ gender : resultItem . gender ,
105
+ name : resultItem . name ,
106
+ picture : resultItem . picture ,
107
+ email : resultItem . email ,
108
+ nat : resultItem . nat ,
109
+ } ) ) ,
107
110
info : {
108
111
...json . info ,
109
112
stringified : JSON . parse ( json . info . stringified ) ,
@@ -114,12 +117,16 @@ describe('BaseModel', () => {
114
117
} ) ;
115
118
116
119
test ( 'should call update with empty object and have no changes' , ( ) => {
117
- const model = new UserResponseModel ( json ) ;
120
+ const model = new UserResponseModel ( json as any ) ;
118
121
const expectedData = {
119
122
...json ,
120
- results : json . results . map ( ( resultItem : any ) =>
121
- Util . deletePropertyFromObject ( resultItem , [ 'location' , 'login' , 'dob' , 'registered' , 'phone' , 'cell' , 'id' ] )
122
- ) ,
123
+ results : json . results . map ( ( resultItem ) => ( {
124
+ gender : resultItem . gender ,
125
+ name : resultItem . name ,
126
+ picture : resultItem . picture ,
127
+ email : resultItem . email ,
128
+ nat : resultItem . nat ,
129
+ } ) ) ,
123
130
info : {
124
131
...json . info ,
125
132
stringified : JSON . parse ( json . info . stringified ) ,
@@ -132,15 +139,19 @@ describe('BaseModel', () => {
132
139
} ) ;
133
140
134
141
test ( 'should have populate UserResponseModel from same UserResponseModel' , ( ) => {
135
- let model = new UserResponseModel ( json ) ;
142
+ let model = new UserResponseModel ( json as any ) ;
136
143
137
144
model = new UserResponseModel ( model ) ;
138
145
139
146
const expectedData = {
140
147
...json ,
141
- results : json . results . map ( ( resultItem : any ) =>
142
- Util . deletePropertyFromObject ( resultItem , [ 'location' , 'login' , 'dob' , 'registered' , 'phone' , 'cell' , 'id' ] )
143
- ) ,
148
+ results : json . results . map ( ( resultItem ) => ( {
149
+ gender : resultItem . gender ,
150
+ name : resultItem . name ,
151
+ picture : resultItem . picture ,
152
+ email : resultItem . email ,
153
+ nat : resultItem . nat ,
154
+ } ) ) ,
144
155
info : {
145
156
...json . info ,
146
157
stringified : JSON . parse ( json . info . stringified ) ,
@@ -151,7 +162,7 @@ describe('BaseModel', () => {
151
162
} ) ;
152
163
153
164
test ( 'should update email results to one item in the array' , ( ) => {
154
- const model = new UserResponseModel ( json ) ;
165
+ const model = new UserResponseModel ( json as any ) ;
155
166
156
167
expect ( model . results . length ) . toBe ( 3 ) ;
157
168
@@ -192,27 +203,22 @@ describe('BaseModel', () => {
192
203
193
204
test ( 'should populate UserModel' , ( ) => {
194
205
const theData = json . results [ 0 ] ;
195
- const model = new UserModel ( theData ) ;
196
-
197
- // const hey = model.toJSON()
198
- // hey.
206
+ const model = new UserModel ( theData as any ) ;
199
207
200
- const expectedData = Util . deletePropertyFromObject ( theData , [
201
- 'location' ,
202
- 'login' ,
203
- 'dob' ,
204
- 'registered' ,
205
- 'phone' ,
206
- 'cell' ,
207
- 'id' ,
208
- ] ) ;
208
+ const expectedData = {
209
+ gender : theData . gender ,
210
+ name : theData . name ,
211
+ picture : theData . picture ,
212
+ email : theData . email ,
213
+ nat : theData . nat ,
214
+ } ;
209
215
210
216
expect ( model . toJSON ( ) ) . toEqual ( expectedData ) ;
211
217
} ) ;
212
218
213
219
test ( 'should update UserModel' , ( ) => {
214
220
const theData = json . results [ 0 ] ;
215
- const model = new UserModel ( theData ) ;
221
+ const model = new UserModel ( theData as any ) ;
216
222
217
223
expect ( model . email ) . toBe ( theData . email ) ;
218
224
expect ( model . name . last ) . toBe ( theData . name . last ) ;
@@ -257,18 +263,19 @@ describe('BaseModel', () => {
257
263
expect ( model . toJSON ( ) ) . toEqual ( expected ) ;
258
264
} ) ;
259
265
260
- test ( 'should test isObject' , ( ) => {
266
+ describe ( 'should test isObject' , ( ) => {
261
267
const model = new BaseModel ( ) ;
262
268
263
269
const objects : object [ ] = [ { } , new UserResponseModel ( ) ] ;
264
270
const nonObjects : any [ ] = [ [ ] , true , false , undefined , null , 8 , 20.18 , '🚀' ] ;
265
271
266
- // tslint:disable-next-line:no-string-literal
267
- objects . forEach ( ( object : object ) => expect ( model [ '_isObject' ] ( object ) ) . toBeTruthy ( ) ) ;
268
- // tslint:disable-next-line:no-string-literal
269
- nonObjects . forEach ( ( nonObject : any ) => expect ( model [ '_isObject' ] ( nonObject ) ) . toBeFalsy ( ) ) ;
272
+ test . each ( objects ) ( 'toBeTruthy' , ( object ) => {
273
+ expect ( model [ '_isObject' ] ( object ) ) . toBeTruthy ( ) ;
274
+ } ) ;
270
275
271
- console . error ( 'Ignore the "Something is wrong!" errors. They are expected.' ) ;
276
+ test . each ( nonObjects ) ( 'toBeTruthy' , ( nonObject ) => {
277
+ expect ( model [ '_isObject' ] ( nonObject ) ) . toBeFalsy ( ) ;
278
+ } ) ;
272
279
} ) ;
273
280
274
281
test ( 'should test IConversionOption' , ( ) => {
@@ -291,7 +298,6 @@ describe('BaseModel', () => {
291
298
292
299
test ( 'should test non existent keys on IConversionOption' , ( ) => {
293
300
expect ( ( ) => {
294
- // tslint:disable-next-line:no-unused-expression
295
301
new NonExistentKeyConversionModel ( { } ) ;
296
302
} ) . toThrow ( SyntaxError ) ;
297
303
} ) ;
0 commit comments