Skip to content

Commit 92932cc

Browse files
fix(model): relations are not being applied if any is null (#134)
Fixes #133
1 parent 460bfb5 commit 92932cc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export default class Model extends StaticModel {
259259
const _relation = getProp(model, relation)
260260

261261
if (!_relation) {
262-
return;
262+
continue;
263263
}
264264

265265
if (Array.isArray(_relation.data) || Array.isArray(_relation)) {

tests/model.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,38 @@ describe('Model methods', () => {
115115
})
116116
})
117117

118+
test("find() method returns a object as instance of such Model with empty relationships", async () => {
119+
const _postResponse = postResponse;
120+
_postResponse.user = null;
121+
_postResponse.relationships.tags = [];
122+
123+
axiosMock.onGet("http://localhost/posts/1").reply(200, _postResponse);
124+
125+
const post = await Post.find(1);
126+
127+
expect(post).toEqual(postResponse);
128+
expect(post).toBeInstanceOf(Post);
129+
expect(post.user).toStrictEqual(null);
130+
expect(post.relationships.tags).toStrictEqual([]);
131+
});
132+
133+
test("find() method returns a object as instance of such Model with some empty relationships", async () => {
134+
const _postResponse = postResponse;
135+
_postResponse.user = null;
136+
137+
axiosMock.onGet("http://localhost/posts/1").reply(200, _postResponse);
138+
139+
const post = await Post.find(1);
140+
141+
expect(post).toEqual(postResponse);
142+
expect(post).toBeInstanceOf(Post);
143+
expect(post.user).toStrictEqual(null);
144+
145+
post.relationships.tags.forEach((tag) => {
146+
expect(tag).toBeInstanceOf(Tag);
147+
});
148+
});
149+
118150
test('get() method returns a array of objects as instance of suchModel', async () => {
119151
axiosMock.onGet('http://localhost/posts').reply(200, postsResponse)
120152

0 commit comments

Comments
 (0)