Skip to content

Commit ae88350

Browse files
committed
Test toJSON is called prior to serialization
1 parent 3c3f9fb commit ae88350

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* JsonController
3+
*
4+
* @description :: Server-side logic for managing jsons
5+
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
6+
*/
7+
8+
module.exports = {
9+
10+
};
11+

tests/dummy/api/models/Json.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Json.js
3+
*
4+
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
5+
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
6+
*/
7+
8+
module.exports = {
9+
10+
attributes: {
11+
variable: {
12+
type: 'string'
13+
},
14+
invisible: {
15+
type: 'string'
16+
},
17+
18+
toJSON: function() {
19+
20+
var obj = this.toObject();
21+
delete obj.invisible;
22+
23+
return obj;
24+
}
25+
},
26+
27+
autoCreatedAt: false,
28+
autoUpdatedAt: false
29+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var request = require('supertest');
2+
var JSONAPIValidator = require('jsonapi-validator').Validator;
3+
4+
validateJSONapi = function(res) {
5+
var validator = new JSONAPIValidator();
6+
7+
validator.validate(res.body);
8+
};
9+
10+
describe('toJSON compatibility', function() {
11+
12+
it('Should return record without the hidden field', function(done) {
13+
14+
let toCreate = {
15+
'data': {
16+
'attributes': {
17+
variable: "test",
18+
invisible: "should not be displayed"
19+
},
20+
'type': 'jsons'
21+
}
22+
};
23+
24+
request(sails.hooks.http.app)
25+
.post('/jsons')
26+
.send(toCreate)
27+
.expect(201)
28+
.expect(validateJSONapi)
29+
.expect({
30+
data: {
31+
id: '1',
32+
type: 'jsons',
33+
attributes: {
34+
variable: 'test'
35+
}
36+
}
37+
})
38+
.end(done);
39+
});
40+
});

0 commit comments

Comments
 (0)