Skip to content

Commit c2ae6ab

Browse files
committed
fix(upgrade): Update code and tests to match latest dependencies
1 parent caf767d commit c2ae6ab

File tree

9 files changed

+78
-73
lines changed

9 files changed

+78
-73
lines changed

lib/relationships.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ module.exports = function (app, options) {
1515
return next()
1616
}
1717

18-
if (ctx.method.name !== 'updateAttributes') return next()
18+
var allowedMethodNames = ['updateAttributes', 'patchAttributes']
19+
var methodName = ctx.method.name
20+
if (allowedMethodNames.indexOf(methodName) === -1) return next()
1921

2022
id = ctx.req.params.id
2123
data = options.data

test/create.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ describe('loopback json api component create method', function () {
129129
.set('Content-Type', 'application/json')
130130
.end(function (err, res) {
131131
expect(err).to.equal(null)
132-
expect(res.body).to.have.deep.property('data.links.self')
133132
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/1/)
134133
done()
135134
})

test/errors.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('disabling loopback-component-jsonapi error handler', function () {
2222
request(app).get('/posts/100').end(function (err, res) {
2323
expect(err).to.equal(null)
2424
expect(res.body).to.have.keys('error')
25-
expect(res.body.error).to.have.keys('name', 'status', 'message', 'statusCode', 'code', 'stack')
25+
expect(res.body.error).to.have.keys('name', 'message', 'statusCode')
2626
done()
2727
})
2828
})

test/find.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ describe('loopback json api component find methods', function () {
9292
.expect(200)
9393
.end(function (err, res) {
9494
expect(err).to.equal(null)
95-
expect(res.body).to.have.deep.property('links.self')
9695
expect(res.body.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts$/)
9796
done()
9897
})
@@ -103,7 +102,6 @@ describe('loopback json api component find methods', function () {
103102
.expect(200)
104103
.end(function (err, res) {
105104
expect(err).to.equal(null)
106-
expect(res.body).to.have.deep.property('data.links.self')
107105
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/1$/)
108106
done()
109107
})
@@ -114,7 +112,6 @@ describe('loopback json api component find methods', function () {
114112
.expect(200)
115113
.end(function (err, res) {
116114
expect(err).to.equal(null)
117-
expect(res.body).to.have.deep.property('data.links.self')
118115
expect(res.body.data.links.self).to.match(/http:\/\/127\.0\.0\.1.*\/posts\/2$/)
119116
done()
120117
})

test/hasManyPolymorphic.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@ var JSONAPIComponent = require('../')
55
var app
66
var ds
77
var Post
8-
var File
8+
var Resource
99

1010
describe('loopback json api hasMany polymorphic relationships', function () {
1111
beforeEach(function () {
1212
app = loopback()
1313
app.set('legacyExplorer', false)
1414
ds = loopback.createDataSource('memory')
1515

16-
File = ds.createModel('file', {
16+
Resource = ds.createModel('resource', {
1717
id: {type: Number, id: true},
1818
fileName: String,
1919
parentId: Number,
2020
parentType: String
2121
})
22-
File.settings.plural = 'files'
23-
app.model(File)
22+
Resource.settings.plural = 'resources'
23+
app.model(Resource)
2424

2525
Post = ds.createModel('post', {
2626
id: {type: Number, id: true},
2727
title: String,
2828
content: String
2929
})
3030
Post.settings.plural = 'posts'
31-
Post.hasMany(File, {
32-
as: 'files',
31+
Post.hasMany(Resource, {
32+
as: 'resources',
3333
polymorphic: 'parent'
3434
})
3535
app.model(Post)
@@ -38,54 +38,54 @@ describe('loopback json api hasMany polymorphic relationships', function () {
3838
JSONAPIComponent(app)
3939
})
4040

41-
describe('Post hasMany Files', function () {
41+
describe('Post hasMany Resources', function () {
4242
beforeEach(function (done) {
4343
Post.create({
4444
title: 'Post One',
4545
content: 'Content'
4646
}, function (err, post) {
4747
expect(err).to.equal(null)
48-
post.files.create({
48+
post.resources.create({
4949
fileName: 'blah.jpg',
5050
parentId: post.id,
5151
parentType: 'post'
5252
}, done)
5353
})
5454
})
5555

56-
it('should have a relationship to Files', function (done) {
56+
it('should have a relationship to Resources', function (done) {
5757
request(app).get('/posts/1')
5858
.end(function (err, res) {
5959
expect(err).to.equal(null)
6060
expect(res.body).to.not.have.key('errors')
61-
expect(res.body.data.relationships.files).to.be.an('object')
61+
expect(res.body.data.relationships.resources).to.be.an('object')
6262
done()
6363
})
6464
})
6565

66-
it('should return the Files that belong to this Post when included flag is present', function (done) {
67-
request(app).get('/posts/1?include=files')
66+
it('should return the Resources that belong to this Post when included flag is present', function (done) {
67+
request(app).get('/posts/1?include=resources')
6868
.end(function (err, res) {
6969
expect(err).to.equal(null)
7070
expect(res.body).to.not.have.key('errors')
7171
expect(res.body.included).to.be.an('array')
72-
expect(res.body.included[0].type).to.equal('files')
72+
expect(res.body.included[0].type).to.equal('resources')
7373
expect(res.body.included[0].id).to.equal('1')
7474
done()
7575
})
7676
})
7777

78-
it('should return the Files that belong to this Post when following the relationship link', function (done) {
78+
it('should return the Resources that belong to this Post when following the relationship link', function (done) {
7979
request(app).get('/posts/1')
8080
.end(function (err, res) {
8181
expect(err).to.equal(null)
8282
expect(res.body).to.not.have.key('errors')
83-
request(app).get(res.body.data.relationships.files.links.related.split('api')[1])
83+
request(app).get(res.body.data.relationships.resources.links.related.split('api')[1])
8484
.end(function (err, res) {
8585
expect(err).to.equal(null)
8686
expect(res.body).to.not.have.key('errors')
8787
expect(res.body.data).to.be.an('array')
88-
expect(res.body.data[0].type).to.equal('files')
88+
expect(res.body.data[0].type).to.equal('resources')
8989
expect(res.body.data[0].id).to.equal('1')
9090
done()
9191
})

test/override-deserializer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('hook in to modify deserialization process', function () {
114114
.end(function (err, res) {
115115
expect(err).to.equal(null)
116116
Comment.findById(1, function (err, comment) {
117-
expect(err).to.equal(undefined)
117+
expect(err).to.equal(null)
118118
expect(comment.postId).to.equal(2)
119119
done()
120120
})

test/relationships.test.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('loopback json api belongsTo relationships', function () {
7777
.end(function (err, res) {
7878
expect(err).to.equal(null)
7979
Comment.findById(1, function (err, comment) {
80-
expect(err).to.equal(undefined)
80+
expect(err).to.equal(null)
8181
expect(comment).not.to.equal(null)
8282
expect(comment.postId).to.equal(2)
8383

@@ -91,15 +91,26 @@ describe('loopback json api belongsTo relationships', function () {
9191
it('should update model linkages', function (done) {
9292
request(app).patch('/posts/1')
9393
.send({
94-
data: {type: 'posts', id: '1', attributes: {title: 'my post', content: 'my post content' },
95-
relationships: {comments: {data: []}}}
94+
data: {
95+
type: 'posts',
96+
id: '1',
97+
attributes: {
98+
title: 'my post',
99+
content: 'my post content'
100+
},
101+
relationships: {
102+
comments: {
103+
data: []
104+
}
105+
}
106+
}
96107
})
97108
.set('Accept', 'application/vnd.api+json')
98109
.set('Content-Type', 'application/json')
99110
.end(function (err, res) {
100111
expect(err).to.equal(null)
101112
Comment.findById(1, function (err, comment) {
102-
expect(err).to.equal(undefined)
113+
expect(err).to.equal(null)
103114
expect(comment).not.to.equal(null)
104115
expect(comment.postId).to.equal(null)
105116

@@ -129,7 +140,7 @@ describe('loopback json api belongsTo relationships', function () {
129140
.end(function (err, res) {
130141
expect(err).to.equal(null)
131142
Comment.find({postId: 1}, function (err, comments) {
132-
expect(err).to.equal(undefined)
143+
expect(err).to.equal(null)
133144
expect(comments.length).to.equal(2)
134145

135146
done()
@@ -150,7 +161,7 @@ describe('loopback json api belongsTo relationships', function () {
150161
.end(function (err, res) {
151162
expect(err).to.equal(null)
152163
Comment.findById(1, function (err, comment) {
153-
expect(err).to.equal(undefined)
164+
expect(err).to.equal(null)
154165
expect(comment).not.to.equal(null)
155166
expect(comment.postId).to.equal(null)
156167

@@ -180,7 +191,7 @@ describe('loopback json api belongsTo relationships', function () {
180191
.end(function (err, res) {
181192
expect(err).to.equal(null)
182193
Comment.find({postId: 1}, function (err, comments) {
183-
expect(err).to.equal(undefined)
194+
expect(err).to.equal(null)
184195
expect(comments.length).not.to.equal(2)
185196

186197
done()
@@ -236,7 +247,7 @@ describe('loopback json api belongsTo relationships', function () {
236247
expect(err).to.equal(null)
237248

238249
Comment.find({}, function (err, comments) {
239-
expect(err).to.equal(undefined)
250+
expect(err).to.equal(null)
240251
_.each(comments, function (comment) {
241252
expect(comment.postId).not.to.equal(null)
242253
})
@@ -269,9 +280,10 @@ describe('loopback json api hasOne relationships', function () {
269280
postId: Number,
270281
name: String
271282
})
283+
Person.settings.plural = 'people'
272284
app.model(Person)
285+
273286
Post.hasOne(Person, {as: 'author', foreignKey: 'postId'})
274-
Person.settings.plural = 'people'
275287

276288
app.use(loopback.rest())
277289
JSONAPIComponent(app)
@@ -304,7 +316,7 @@ describe('loopback json api hasOne relationships', function () {
304316
expect(res.body).not.to.have.keys('errors')
305317
expect(res.status).to.equal(201)
306318
Person.findById(1, function (err, person) {
307-
expect(err).to.equal(undefined)
319+
expect(err).to.equal(null)
308320
expect(person).not.to.equal(null)
309321
expect(person.postId).to.equal(2)
310322

@@ -328,7 +340,7 @@ describe('loopback json api hasOne relationships', function () {
328340
expect(res.body).not.to.have.keys('errors')
329341
expect(res.status).to.equal(200)
330342
Person.findById(1, function (err, person) {
331-
expect(err).to.equal(undefined)
343+
expect(err).to.equal(null)
332344
expect(person).not.to.equal(null)
333345
expect(person.postId).to.equal(null)
334346
done()
@@ -339,27 +351,40 @@ describe('loopback json api hasOne relationships', function () {
339351

340352
describe('replace linkages as part of an update operation', function () {
341353
beforeEach(function (done) {
342-
Person.create({name: 'Rachel McAdams'}, done)
354+
Person.create({id: 191, name: 'Rachel McAdams'}, done)
343355
})
344356
it('should update model linkages', function (done) {
345357
request(app).patch('/posts/1').send({
346-
data: {type: 'posts', id: '1', attributes: {title: 'my post', content: 'my post content' },
347-
relationships: {author: {data: {type: 'people', id: 2}}}}
358+
data: {
359+
type: 'posts',
360+
id: '1',
361+
attributes: {
362+
title: 'my post',
363+
content: 'my post content'
364+
},
365+
relationships: {
366+
author: {
367+
data: {
368+
type: 'people',
369+
id: 191
370+
}
371+
}
372+
}
373+
}
348374
})
349375
.set('Accept', 'application/vnd.api+json')
350376
.set('Content-Type', 'application/json')
351377
.end(function (err, res) {
352378
expect(err).to.equal(null)
353379
Person.find({where: {postId: 1}}, function (err, people) {
354-
expect(err).to.equal(undefined)
380+
expect(err).to.equal(null)
355381
expect(people.length).to.equal(1)
356-
expect(people[0].id).to.equal(2)
382+
expect(people[0].id).to.equal(191)
357383
done()
358384
})
359385
})
360386
})
361387
})
362-
363388
})
364389
})
365390

@@ -417,7 +442,7 @@ describe('loopback json api hasMany relationships', function () {
417442
.end(function (err, res) {
418443
expect(err).to.equal(null)
419444
Comment.findById(1, function (err, comment) {
420-
expect(err).to.equal(undefined)
445+
expect(err).to.equal(null)
421446
expect(comment).not.to.equal(null)
422447
expect(comment.postId).to.equal(2)
423448
done()
@@ -438,7 +463,7 @@ describe('loopback json api hasMany relationships', function () {
438463
.end(function (err, res) {
439464
expect(err).to.equal(null)
440465
Comment.findById(1, function (err, comment) {
441-
expect(err).to.equal(undefined)
466+
expect(err).to.equal(null)
442467
expect(comment).not.to.equal(null)
443468
expect(comment.postId).to.equal(null)
444469
done()
@@ -467,7 +492,7 @@ describe('loopback json api hasMany relationships', function () {
467492
.end(function (err, res) {
468493
expect(err).to.equal(null)
469494
Comment.find({postId: 1}, function (err, comments) {
470-
expect(err).to.equal(undefined)
495+
expect(err).to.equal(null)
471496
expect(comments.length).to.equal(2)
472497
done()
473498
})

test/throughModel.test.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ describe('through Model', function () {
6262
})
6363
.end(function (err, res) {
6464
expect(err).to.equal(null)
65-
console.error(res.body.errors)
6665
expect(res).to.not.have.deep.property('body.errors')
67-
expect(res).to.have.deep.property('body.data.id').and.equal('1')
66+
expect(res.body.data.id).to.equal('1')
6867
done(err)
6968
})
7069
})
@@ -79,13 +78,13 @@ describe('through Model', function () {
7978
.get('/users/1?include=topics')
8079
.end(function (err, res) {
8180
expect(err).to.equal(null)
82-
expect(res).to.have.deep.property('body.data.relationships.topics.data').and.have.property('length').and.equal(2)
83-
expect(res).to.have.deep.property('body.data.relationships.topics.data[0].type').to.equal('topics')
84-
expect(res).to.have.deep.property('body.data.relationships.topics.data[1].type').to.equal('topics')
85-
expect(res).to.have.deep.property('body.included[0].type').and.equal('topics')
86-
expect(res).to.have.deep.property('body.included[0].id').and.equal('1')
87-
expect(res).to.have.deep.property('body.included[1].type').and.equal('topics')
88-
expect(res).to.have.deep.property('body.included[1].id').and.equal('2')
81+
expect(res.body.data.relationships.topics.data).to.have.property('length').and.equal(2)
82+
expect(res.body.data.relationships.topics.data[0].type).to.equal('topics')
83+
expect(res.body.data.relationships.topics.data[1].type).to.equal('topics')
84+
expect(res.body.included[0].type).to.equal('topics')
85+
expect(res.body.included[0].id).to.equal('1')
86+
expect(res.body.included[1].type).to.equal('topics')
87+
expect(res.body.included[1].id).to.equal('2')
8988
done(err)
9089
})
9190
})
@@ -100,10 +99,10 @@ describe('through Model', function () {
10099
.get('/topics/1?include=users')
101100
.end(function (err, res) {
102101
expect(err).to.equal(null)
103-
expect(res).to.have.deep.property('body.data.relationships.users.data').and.have.property('length').and.equal(1)
104-
expect(res).to.have.deep.property('body.data.relationships.users.data[0].type').to.equal('users')
105-
expect(res).to.have.deep.property('body.included[0].type').and.equal('users')
106-
expect(res).to.have.deep.property('body.included[0].id').and.equal('1')
102+
expect(res.body.data.relationships.users.data).to.have.property('length').and.equal(1)
103+
expect(res.body.data.relationships.users.data[0].type).to.equal('users')
104+
expect(res.body.included[0].type).to.equal('users')
105+
expect(res.body.included[0].id).to.equal('1')
107106
done(err)
108107
})
109108
})

0 commit comments

Comments
 (0)