Skip to content

Commit

Permalink
fix(model): relations are not being applied if any is null (#134)
Browse files Browse the repository at this point in the history
Fixes #133
  • Loading branch information
JoaoPedroAS51 authored Oct 26, 2020
1 parent 460bfb5 commit 92932cc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export default class Model extends StaticModel {
const _relation = getProp(model, relation)

if (!_relation) {
return;
continue;
}

if (Array.isArray(_relation.data) || Array.isArray(_relation)) {
Expand Down
32 changes: 32 additions & 0 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,38 @@ describe('Model methods', () => {
})
})

test("find() method returns a object as instance of such Model with empty relationships", async () => {
const _postResponse = postResponse;
_postResponse.user = null;
_postResponse.relationships.tags = [];

axiosMock.onGet("http://localhost/posts/1").reply(200, _postResponse);

const post = await Post.find(1);

expect(post).toEqual(postResponse);
expect(post).toBeInstanceOf(Post);
expect(post.user).toStrictEqual(null);
expect(post.relationships.tags).toStrictEqual([]);
});

test("find() method returns a object as instance of such Model with some empty relationships", async () => {
const _postResponse = postResponse;
_postResponse.user = null;

axiosMock.onGet("http://localhost/posts/1").reply(200, _postResponse);

const post = await Post.find(1);

expect(post).toEqual(postResponse);
expect(post).toBeInstanceOf(Post);
expect(post.user).toStrictEqual(null);

post.relationships.tags.forEach((tag) => {
expect(tag).toBeInstanceOf(Tag);
});
});

test('get() method returns a array of objects as instance of suchModel', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, postsResponse)

Expand Down

0 comments on commit 92932cc

Please sign in to comment.