|
| 1 | +var request = require('supertest'); |
| 2 | +var loopback = require('loopback'); |
| 3 | +var expect = require('chai').expect; |
| 4 | +var JSONAPIComponent = require('../'); |
| 5 | +var app; |
| 6 | +var Post; |
| 7 | +var Comment; |
| 8 | + |
| 9 | +describe('hook in to modify serialization process', function () { |
| 10 | + beforeEach(function (done) { |
| 11 | + app = loopback(); |
| 12 | + app.set('legacyExplorer', false); |
| 13 | + var ds = loopback.createDataSource('memory'); |
| 14 | + |
| 15 | + Post = ds.createModel('post', { title: String, content: String, other: String }); |
| 16 | + app.model(Post); |
| 17 | + |
| 18 | + Comment = ds.createModel('comment', { title: String, comment: String, other: String }); |
| 19 | + app.model(Comment); |
| 20 | + |
| 21 | + app.use(loopback.rest()); |
| 22 | + |
| 23 | + Post.create({title: 'my post', content: 'my post content', other: 'my post other'}, function () { |
| 24 | + Comment.create({title: 'my comment title', comment: 'my comment', other: 'comment other'}, done); |
| 25 | + }); |
| 26 | + |
| 27 | + JSONAPIComponent(app); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('before serialization', function () { |
| 31 | + beforeEach(function () { |
| 32 | + Post.beforeJsonApiSerialize = function (options, cb) { |
| 33 | + options.type = 'notPosts'; |
| 34 | + cb(null, options); |
| 35 | + }; |
| 36 | + }); |
| 37 | + afterEach(function () { |
| 38 | + delete Post.beforeJsonApiSerialize; |
| 39 | + }); |
| 40 | + it('should allow options to be modified before serialization', function (done) { |
| 41 | + request(app).get('/posts') |
| 42 | + .expect(200) |
| 43 | + .end(function (err, res) { |
| 44 | + expect(err).to.equal(null); |
| 45 | + expect(res.body.data[0].type).to.equal('notPosts'); |
| 46 | + done(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + it('should not affect models without a beforeJsonApiSerialize method', function (done) { |
| 50 | + request(app).get('/comments') |
| 51 | + .expect(200) |
| 52 | + .end(function (err, res) { |
| 53 | + expect(err).to.equal(null); |
| 54 | + expect(res.body.data[0].type).to.equal('comments'); |
| 55 | + done(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('after serialization', function () { |
| 61 | + beforeEach(function () { |
| 62 | + Post.afterJsonApiSerialize = function (options, cb) { |
| 63 | + options.results.data = options.results.data.map(function (result) { |
| 64 | + result.attributes.testing = true; |
| 65 | + return result; |
| 66 | + }); |
| 67 | + cb(null, options); |
| 68 | + }; |
| 69 | + }); |
| 70 | + afterEach(function () { |
| 71 | + delete Post.afterJsonApiSerialize; |
| 72 | + }); |
| 73 | + it('should allow results to be modified after serialization', function (done) { |
| 74 | + request(app).get('/posts') |
| 75 | + .expect(200) |
| 76 | + .end(function (err, res) { |
| 77 | + expect(err).to.equal(null); |
| 78 | + expect(res.body.data[0].attributes.testing).to.equal(true); |
| 79 | + done(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + it('should not affect models without an afterJsonApiSerialize method', function (done) { |
| 83 | + request(app).get('/comments') |
| 84 | + .expect(200) |
| 85 | + .end(function (err, res) { |
| 86 | + expect(err).to.equal(null); |
| 87 | + expect(res.body.data[0].attributes.testing).to.equal(undefined); |
| 88 | + done(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('custom serialization', function () { |
| 94 | + beforeEach(function () { |
| 95 | + Post.jsonApiSerialize = function (options, cb) { |
| 96 | + options.results = options.results.map(function (result) { |
| 97 | + return result.title; |
| 98 | + }); |
| 99 | + cb(null, options); |
| 100 | + }; |
| 101 | + }); |
| 102 | + afterEach(function () { |
| 103 | + delete Post.jsonApiSerialize; |
| 104 | + }); |
| 105 | + it('should allow a custom serializer to be defined for a model', function (done) { |
| 106 | + request(app).get('/posts') |
| 107 | + .expect(200) |
| 108 | + .end(function (err, res) { |
| 109 | + expect(err).to.equal(null); |
| 110 | + expect(res.body[0]).to.equal('my post'); |
| 111 | + done(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + it('should not affect models without a jsonApiSerialize method', function (done) { |
| 115 | + request(app).get('/comments') |
| 116 | + .expect(200) |
| 117 | + .end(function (err, res) { |
| 118 | + expect(err).to.equal(null); |
| 119 | + expect(res.body.data[0].attributes.title).to.equal('my comment title'); |
| 120 | + done(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments