File tree Expand file tree Collapse file tree 2 files changed +15
-5
lines changed Expand file tree Collapse file tree 2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,18 @@ describe('JsonApiModel', () => {
129
129
} ) ;
130
130
} ) ;
131
131
132
+ it ( 'should return an empty array for hasMany relationship when one is included without any elements' , ( ) => {
133
+ const BOOK_NUMBER = 0 ;
134
+ const DATA = getAuthorData ( 'books' , BOOK_NUMBER ) ;
135
+ author = new Author ( datastore , DATA ) ;
136
+ author . syncRelationships ( DATA , getIncludedBooks ( BOOK_NUMBER ) ) ;
137
+
138
+ expect ( author ) . toBeDefined ( ) ;
139
+ expect ( author . id ) . toBe ( AUTHOR_ID ) ;
140
+ expect ( author . books ) . toBeDefined ( ) ;
141
+ expect ( author . books . length ) . toBe ( BOOK_NUMBER ) ;
142
+ } ) ;
143
+
132
144
it ( 'should parse infinite levels of relationships by reference' , ( ) => {
133
145
const BOOK_NUMBER = 4 ;
134
146
const DATA = getAuthorData ( 'books' , BOOK_NUMBER ) ;
Original file line number Diff line number Diff line change @@ -120,7 +120,7 @@ export class JsonApiModel {
120
120
for ( const metadata of hasMany ) {
121
121
const relationship : any = data . relationships ? data . relationships [ metadata . relationship ] : null ;
122
122
123
- if ( relationship && relationship . data && relationship . data . length > 0 ) {
123
+ if ( relationship && relationship . data && Array . isArray ( relationship . data ) ) {
124
124
let allModels : JsonApiModel [ ] = [ ] ;
125
125
const modelTypesFetched : any = [ ] ;
126
126
@@ -148,11 +148,9 @@ export class JsonApiModel {
148
148
throw { message : `parseHasMany - Model type for relationship ${ typeName } not found.` } ;
149
149
}
150
150
}
151
-
152
- if ( allModels . length > 0 ) {
153
- this [ metadata . propertyName ] = allModels ;
154
- }
155
151
}
152
+
153
+ this [ metadata . propertyName ] = allModels ;
156
154
}
157
155
}
158
156
}
You can’t perform that action at this time.
0 commit comments