@@ -9,23 +9,20 @@ import { NameModel } from './data/models/NameModel';
99import { AdminModel } from './data/models/AdminModel' ;
1010
1111describe ( 'BaseModel' , ( ) => {
12- let json : any = null ;
12+ let json = data ;
1313
1414 beforeEach ( ( ) => {
15- json = Util . clone ( data as any ) ;
16- } ) ;
17-
18- afterEach ( ( ) => {
19- json = null ;
15+ json = Util . clone ( data ) ;
2016 } ) ;
2117
2218 test ( 'update returns itself' , ( ) => {
2319 const baseModel = new BaseModel ( ) ;
20+
2421 expect ( baseModel . update ( ) ) . toEqual ( baseModel ) ;
2522 } ) ;
2623
2724 test ( 'should populate InfoModel' , ( ) => {
28- const model = new InfoModel ( json . info ) ;
25+ const model = new InfoModel ( json . info as any ) ;
2926
3027 expect ( model . toJSON ( ) ) . toEqual ( {
3128 ...json . info ,
@@ -52,16 +49,14 @@ describe('BaseModel', () => {
5249
5350 expect ( model . info ) . toEqual ( null ) ;
5451 expect ( model . results ) . toEqual ( [ ] ) ;
55- expect ( model . sjsOptions ) . toEqual ( { expand : false } ) ;
5652 } ) ;
5753
5854 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 ( ) ;
6157
6258 clone . info . version = '888' ;
6359
64- expect ( model . info . sjsId ) . not . toEqual ( clone . info . sjsId ) ;
6560 expect ( model . info ) . not . toEqual ( clone . info ) ;
6661 expect ( model . toJSON ( ) ) . not . toEqual ( clone . toJSON ( ) ) ;
6762 } ) ;
@@ -87,23 +82,31 @@ describe('BaseModel', () => {
8782 } ) ;
8883
8984 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 ( ( ) => { } ) ;
9186
9287 const model = new UserResponseModel ( null as any ) ;
9388
9489 expect ( model . info ) . toEqual ( null ) ;
9590 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 ( ) ;
9796 } ) ;
9897
9998 test ( 'should have populate UserResponseModel' , ( ) => {
100- const model = new UserResponseModel ( json ) ;
99+ const model = new UserResponseModel ( json as any ) ;
101100
102101 const expectedData = {
103102 ...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+ } ) ) ,
107110 info : {
108111 ...json . info ,
109112 stringified : JSON . parse ( json . info . stringified ) ,
@@ -114,12 +117,16 @@ describe('BaseModel', () => {
114117 } ) ;
115118
116119 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 ) ;
118121 const expectedData = {
119122 ...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+ } ) ) ,
123130 info : {
124131 ...json . info ,
125132 stringified : JSON . parse ( json . info . stringified ) ,
@@ -132,15 +139,19 @@ describe('BaseModel', () => {
132139 } ) ;
133140
134141 test ( 'should have populate UserResponseModel from same UserResponseModel' , ( ) => {
135- let model = new UserResponseModel ( json ) ;
142+ let model = new UserResponseModel ( json as any ) ;
136143
137144 model = new UserResponseModel ( model ) ;
138145
139146 const expectedData = {
140147 ...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+ } ) ) ,
144155 info : {
145156 ...json . info ,
146157 stringified : JSON . parse ( json . info . stringified ) ,
@@ -151,7 +162,7 @@ describe('BaseModel', () => {
151162 } ) ;
152163
153164 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 ) ;
155166
156167 expect ( model . results . length ) . toBe ( 3 ) ;
157168
@@ -192,27 +203,22 @@ describe('BaseModel', () => {
192203
193204 test ( 'should populate UserModel' , ( ) => {
194205 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 ) ;
199207
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+ } ;
209215
210216 expect ( model . toJSON ( ) ) . toEqual ( expectedData ) ;
211217 } ) ;
212218
213219 test ( 'should update UserModel' , ( ) => {
214220 const theData = json . results [ 0 ] ;
215- const model = new UserModel ( theData ) ;
221+ const model = new UserModel ( theData as any ) ;
216222
217223 expect ( model . email ) . toBe ( theData . email ) ;
218224 expect ( model . name . last ) . toBe ( theData . name . last ) ;
@@ -257,18 +263,19 @@ describe('BaseModel', () => {
257263 expect ( model . toJSON ( ) ) . toEqual ( expected ) ;
258264 } ) ;
259265
260- test ( 'should test isObject' , ( ) => {
266+ describe ( 'should test isObject' , ( ) => {
261267 const model = new BaseModel ( ) ;
262268
263269 const objects : object [ ] = [ { } , new UserResponseModel ( ) ] ;
264270 const nonObjects : any [ ] = [ [ ] , true , false , undefined , null , 8 , 20.18 , '🚀' ] ;
265271
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+ } ) ;
270275
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+ } ) ;
272279 } ) ;
273280
274281 test ( 'should test IConversionOption' , ( ) => {
@@ -291,7 +298,6 @@ describe('BaseModel', () => {
291298
292299 test ( 'should test non existent keys on IConversionOption' , ( ) => {
293300 expect ( ( ) => {
294- // tslint:disable-next-line:no-unused-expression
295301 new NonExistentKeyConversionModel ( { } ) ;
296302 } ) . toThrow ( SyntaxError ) ;
297303 } ) ;
0 commit comments