Skip to content

Commit 35cfcb7

Browse files
committed
Merge pull request #57 from digitalsadhu/test/test-no-attributes-patch
#56 Adds tests for attributes in PATH requests.
2 parents 9f95265 + c646d1c commit 35cfcb7

File tree

1 file changed

+93
-16
lines changed

1 file changed

+93
-16
lines changed

test/update.test.js

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ describe('loopback json api component update method', function () {
1111
app.set('legacyExplorer', false);
1212
var ds = loopback.createDataSource('memory');
1313
Post = ds.createModel('post', {
14-
id: {type: Number, id: true},
14+
id: {
15+
type: Number,
16+
id: true
17+
},
1518
title: String,
1619
content: String
1720
});
@@ -24,7 +27,7 @@ describe('loopback json api component update method', function () {
2427
}, done);
2528
});
2629

27-
describe('headers', function () {
30+
describe('Headers', function () {
2831
it('PATCH /models/:id should have the JSON API Content-Type header set on response', function (done) {
2932
//TODO: superagent/supertest breaks when trying to use JSON API Content-Type header
3033
//waiting on a fix
@@ -48,9 +51,10 @@ describe('loopback json api component update method', function () {
4851
});
4952
});
5053

51-
describe('status codes', function () {
54+
describe('Status codes', function () {
5255
it('PATCH /models/:id should return a 200 status code', function (done) {
53-
request(app).patch('/posts/1')
56+
request(app)
57+
.patch('/posts/1')
5458
.send({
5559
data: {
5660
type: 'posts',
@@ -83,7 +87,7 @@ describe('loopback json api component update method', function () {
8387
});
8488
});
8589

86-
describe('self links', function () {
90+
describe('Links', function () {
8791
it('should produce resource level self links', function (done) {
8892
request(app).patch('/posts/1')
8993
.send({
@@ -106,9 +110,10 @@ describe('loopback json api component update method', function () {
106110
});
107111
});
108112

109-
describe('Creating a resource using PATCH /models/:id', function () {
113+
describe('Updating a resource using PATCH', function () {
110114
it('PATCH /models/:id should return a correct JSON API response', function (done) {
111-
request(app).patch('/posts/1')
115+
request(app)
116+
.patch('/posts/1')
112117
.send({
113118
data: {
114119
type: 'posts',
@@ -133,26 +138,80 @@ describe('loopback json api component update method', function () {
133138
done();
134139
});
135140
});
141+
142+
it('PATCH /models/:id if property in `attributes` is not present, it should not be considered null or change from it\'s original value', function (done) {
143+
request(app)
144+
.patch('/posts/1')
145+
.send({
146+
data: {
147+
type: 'posts',
148+
id: 1,
149+
attributes: {
150+
content: 'only changing content, not title'
151+
}
152+
}
153+
})
154+
.set('Content-Type', 'application/json')
155+
.end(function (err, res) {
156+
expect(err).to.equal(null);
157+
expect(res.body).to.have.all.keys('data');
158+
expect(res.body.data).to.have.all.keys('id', 'type', 'attributes', 'links');
159+
expect(res.body.data.id).to.equal('1');
160+
expect(res.body.data.type).to.equal('posts');
161+
expect(res.body.data.attributes).to.have.all.keys('title', 'content');
162+
expect(res.body.data.attributes).to.not.have.keys('id');
163+
expect(res.body.data.attributes.title).to.equal('my post');
164+
expect(res.body.data.attributes.content).to.equal('only changing content, not title');
165+
done();
166+
});
167+
});
136168
});
137169

138170
describe('Errors', function () {
139-
it('PATCH /models/:id with empty `attributes` should not return an error', function (done) {
171+
it('PATCH /models/:id with empty `attributes` should not return an error and should not modify existing attributes', function (done) {
140172
request(app).patch('/posts/1')
141173
.send({ data: { type: 'posts', id: 1, attributes: { } } })
142174
.set('Content-Type', 'application/json')
143175
.expect(200)
144-
.end(done);
176+
.end(function (err, res) {
177+
expect(err).to.equal(null);
178+
expect(res.body).to.have.all.keys('data');
179+
expect(res.body.data).to.have.all.keys('id', 'type', 'attributes', 'links');
180+
expect(res.body.data.id).to.equal('1');
181+
expect(res.body.data.type).to.equal('posts');
182+
expect(res.body.data.attributes).to.have.all.keys('title', 'content');
183+
expect(res.body.data.attributes).to.not.have.keys('id');
184+
expect(res.body.data.attributes.title).to.equal('my post');
185+
expect(res.body.data.attributes.content).to.equal('my post content');
186+
done();
187+
});
145188
});
146189

147-
it('PATCH /models/:id with no `attributes` should not return an error', function (done) {
190+
it('PATCH /models/:id with no `attributes` should not return an error and should not modify existing attributes', function (done) {
148191
request(app).patch('/posts/1')
149-
.send({ data: { type: 'posts', id: 1 } })
192+
.send({
193+
data: {
194+
type: 'posts',
195+
id: 1
196+
}
197+
})
150198
.set('Content-Type', 'application/json')
151199
.expect(200)
152-
.end(done);
200+
.end(function (err, res) {
201+
expect(err).to.equal(null);
202+
expect(res.body).to.have.all.keys('data');
203+
expect(res.body.data).to.have.all.keys('id', 'type', 'attributes', 'links');
204+
expect(res.body.data.id).to.equal('1');
205+
expect(res.body.data.type).to.equal('posts');
206+
expect(res.body.data.attributes).to.have.all.keys('title', 'content');
207+
expect(res.body.data.attributes).to.not.have.keys('id');
208+
expect(res.body.data.attributes.title).to.equal('my post');
209+
expect(res.body.data.attributes.content).to.equal('my post content');
210+
done();
211+
});
153212
});
154213

155-
it('PATCH /models/:id should return an 400 error if type key is not present', function (done) {
214+
it('PATCH /models/:id should return an 400 error if `type` key is not present and include an errors array', function (done) {
156215
request(app).patch('/posts/1')
157216
.send({
158217
data: {
@@ -165,10 +224,19 @@ describe('loopback json api component update method', function () {
165224
})
166225
.expect(400)
167226
.set('Content-Type', 'application/json')
168-
.end(done);
227+
.end(function (err, res) {
228+
expect(err).to.equal(null);
229+
expect(res.body).to.have.keys('errors');
230+
expect(res.body.errors).to.be.a('array');
231+
expect(res.body.errors.length).to.equal(1);
232+
expect(res.body.errors[0].status).to.equal(422);
233+
expect(res.body.errors[0].title).to.equal('ValidationError');
234+
expect(res.body.errors[0].detail).to.equal('JSON API resource object must contain `data.type` property');
235+
done();
236+
});
169237
});
170238

171-
it('PATCH /models/:id should return an 400 error if id key is not present', function (done) {
239+
it('PATCH /models/:id should return an 400 error if `id` key is not present and include an errors array', function (done) {
172240
request(app).patch('/posts/1')
173241
.send({
174242
data: {
@@ -181,7 +249,16 @@ describe('loopback json api component update method', function () {
181249
})
182250
.expect(400)
183251
.set('Content-Type', 'application/json')
184-
.end(done);
252+
.end(function (err, res) {
253+
expect(err).to.equal(null);
254+
expect(res.body).to.have.keys('errors');
255+
expect(res.body.errors).to.be.a('array');
256+
expect(res.body.errors.length).to.equal(1);
257+
expect(res.body.errors[0].status).to.equal(422);
258+
expect(res.body.errors[0].title).to.equal('ValidationError');
259+
expect(res.body.errors[0].detail).to.equal('JSON API resource object must contain `data.id` property');
260+
done();
261+
});
185262
});
186263
});
187264
});

0 commit comments

Comments
 (0)