|
| 1 | +var request = require('supertest'); |
| 2 | +var loopback = require('loopback'); |
| 3 | +var expect = require('chai').expect; |
| 4 | +var JSONAPIComponent = require('../'); |
| 5 | +var ds; |
| 6 | +var app; |
| 7 | +var Post; |
| 8 | +var Comment; |
| 9 | + |
| 10 | +describe('include option', function () { |
| 11 | + beforeEach(function () { |
| 12 | + app = loopback(); |
| 13 | + app.set('legacyExplorer', false); |
| 14 | + ds = loopback.createDataSource('memory'); |
| 15 | + Post = ds.createModel('post', { |
| 16 | + id: {type: Number, id: true}, |
| 17 | + title: String, |
| 18 | + content: String |
| 19 | + }, { |
| 20 | + scope: { |
| 21 | + 'include': 'comments' |
| 22 | + } |
| 23 | + }); |
| 24 | + app.model(Post); |
| 25 | + |
| 26 | + Comment = ds.createModel('comment', { |
| 27 | + id: {type: Number, id: true}, |
| 28 | + postId: Number, |
| 29 | + title: String, |
| 30 | + comment: String |
| 31 | + }); |
| 32 | + app.model(Comment); |
| 33 | + Post.hasMany(Comment, {as: 'comments', foreignKey: 'postId'}); |
| 34 | + Comment.settings.plural = 'comments'; |
| 35 | + |
| 36 | + app.use(loopback.rest()); |
| 37 | + JSONAPIComponent(app, {restApiRoot: '/'}); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('include defined at model level', function () { |
| 41 | + beforeEach(function (done) { |
| 42 | + Post.create({ |
| 43 | + title: 'my post', |
| 44 | + content: 'my post content' |
| 45 | + }, function (err, post) { |
| 46 | + expect(err).to.equal(null); |
| 47 | + post.comments.create({ |
| 48 | + title: 'My comment', |
| 49 | + comment: 'My comment text' |
| 50 | + }, function () { |
| 51 | + post.comments.create({ |
| 52 | + title: 'My second comment', |
| 53 | + comment: 'My second comment text' |
| 54 | + }, done); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('hasMany response', function () { |
| 60 | + |
| 61 | + it('should have key `included`', function (done) { |
| 62 | + request(app).get('/posts/1') |
| 63 | + .end(function (err, res) { |
| 64 | + expect(err).to.equal(null); |
| 65 | + expect(res.body.included).to.be.an('array'); |
| 66 | + done(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('attributes should not have relationship key', function (done) { |
| 71 | + request(app).get('/posts/1') |
| 72 | + .end(function (err, res) { |
| 73 | + expect(err).to.equal(null); |
| 74 | + expect(res.body.data.attributes).to.not.include.key('comments'); |
| 75 | + done(); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | +}); |
0 commit comments