Skip to content

Commit 0f7b15d

Browse files
committed
Fix 'fromResource' when using find()
Resolve robsontenorio#66
1 parent a07e672 commit 0f7b15d

File tree

4 files changed

+821
-1382
lines changed

4 files changed

+821
-1382
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
"babel-eslint": "^10.1.0",
4747
"codecov": "^3.6.5",
4848
"eslint": "^6.8.0",
49-
"jest": "^25.5.0"
49+
"jest": "^24.1.0"
5050
}
51-
}
51+
}

src/Model.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class Model extends StaticModel {
4747

4848
custom(...args) {
4949

50-
if(args.length === 0) {
50+
if (args.length === 0) {
5151
throw new Error('The custom() method takes a minimum of one argument.')
5252
}
5353

@@ -59,22 +59,22 @@ export default class Model extends StaticModel {
5959
let resource = '';
6060

6161
args.forEach(value => {
62-
switch(true) {
62+
switch (true) {
6363
case (typeof value === 'string'):
6464
resource += slash + value.replace(/^\/+/, '');
6565
break;
6666
case (value instanceof Model):
6767
resource += slash + value.resource();
6868

69-
if(value.isValidId(value.getPrimaryKey())) {
69+
if (value.isValidId(value.getPrimaryKey())) {
7070
resource += '/' + value.getPrimaryKey();
7171
}
7272
break;
7373
default:
7474
throw new Error('Arguments to custom() must be strings or instances of Model.')
7575
}
7676

77-
if( !slash.length ) {
77+
if (!slash.length) {
7878
slash = '/';
7979
}
8080
});
@@ -152,7 +152,7 @@ export default class Model extends StaticModel {
152152
}
153153
}
154154

155-
parameterNames () {
155+
parameterNames() {
156156
return {
157157
include: 'include',
158158
filter: 'filter',
@@ -256,7 +256,15 @@ export default class Model extends StaticModel {
256256
return this.request({
257257
url,
258258
method: 'GET'
259-
}).then(response => new this.constructor(response.data))
259+
}).then(response => {
260+
const item = new this.constructor(response.data)
261+
262+
if (this._fromResource) {
263+
item._from(this._fromResource)
264+
}
265+
266+
return item
267+
})
260268
}
261269

262270
$find(identifier) {
@@ -283,7 +291,10 @@ export default class Model extends StaticModel {
283291

284292
collection = collection.map(c => {
285293
let item = new this.constructor(c)
286-
Object.defineProperty(item, '_fromResource', { get: () => this._fromResource })
294+
295+
if (this._fromResource) {
296+
item._from(this._fromResource)
297+
}
287298

288299
return item
289300
})

tests/model.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('Model methods', () => {
8181
const post = await Post.$find(1)
8282

8383
expect(post).toEqual(postEmbedResponse.data)
84+
expect(post).toBeInstanceOf(Post)
8485
})
8586

8687
test('$find() handles request without "data" wrapper', async () => {
@@ -89,6 +90,8 @@ describe('Model methods', () => {
8990
const post = await Post.$find(1)
9091

9192
expect(post).toEqual(postResponse)
93+
expect(post).toBeInstanceOf(Post)
94+
9295
})
9396

9497
test('get() method returns a array of objects as instance of suchModel', async () => {
@@ -547,4 +550,22 @@ describe('Model methods', () => {
547550

548551
expect(errorModel).toThrow('Arguments to custom() must be strings or instances of Model.')
549552
})
553+
554+
test('save() method makes a PUT request to the correct URL on nested object thas was fetched with find() method', async () => {
555+
axiosMock.onGet('http://localhost/posts/1/comments/1').reply(200, commentsResponse[0])
556+
axiosMock.onPut('http://localhost/posts/1/comments/1').reply(200, commentsResponse[0])
557+
558+
const post = new Post({ id: 1 })
559+
const comment = await post.comments().find(1)
560+
561+
axiosMock.onAny().reply((config) => {
562+
expect(config.method).toEqual('put')
563+
expect(config.url).toEqual('http://localhost/posts/1/comments/1')
564+
565+
return [200, {}]
566+
})
567+
568+
comment.text = 'Hola!'
569+
await comment.save()
570+
})
550571
})

0 commit comments

Comments
 (0)