|
| 1 | +var request = require('supertest'); |
| 2 | +var loopback = require('loopback'); |
| 3 | +var expect = require('chai').expect; |
| 4 | +var JSONAPIComponent = require('../'); |
| 5 | +var ds, app, Post, Author, Comment, Category; |
| 6 | + |
| 7 | +describe('include option', function () { |
| 8 | + beforeEach(function () { |
| 9 | + app = loopback(); |
| 10 | + app.set('legacyExplorer', false); |
| 11 | + ds = loopback.createDataSource('memory'); |
| 12 | + Post = ds.createModel('post', { |
| 13 | + id: {type: Number, id: true}, |
| 14 | + title: String, |
| 15 | + content: String |
| 16 | + }, { |
| 17 | + scope: { |
| 18 | + 'include': 'comments' |
| 19 | + } |
| 20 | + }); |
| 21 | + app.model(Post); |
| 22 | + |
| 23 | + Comment = ds.createModel('comment', { |
| 24 | + id: {type: Number, id: true}, |
| 25 | + postId: Number, |
| 26 | + authorId: Number, |
| 27 | + title: String, |
| 28 | + comment: String |
| 29 | + }); |
| 30 | + |
| 31 | + app.model(Comment); |
| 32 | + |
| 33 | + Author = ds.createModel('author', { |
| 34 | + id: {type: Number, id: true}, |
| 35 | + name: String |
| 36 | + }); |
| 37 | + |
| 38 | + app.model(Author); |
| 39 | + |
| 40 | + Category = ds.createModel('category', { |
| 41 | + id: {type: Number, id: true}, |
| 42 | + name: String |
| 43 | + }); |
| 44 | + |
| 45 | + app.model(Author); |
| 46 | + |
| 47 | + Post.hasMany(Comment, {as: 'comments', foreignKey: 'postId'}); |
| 48 | + Post.belongsTo(Author, {as: 'author', foreignKey: 'authorId'}); |
| 49 | + Post.belongsTo(Category, {as: 'category', foreignKey: 'categoryId'}); |
| 50 | + Comment.settings.plural = 'comments'; |
| 51 | + |
| 52 | + app.use(loopback.rest()); |
| 53 | + JSONAPIComponent(app, {restApiRoot: '/'}); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('include defined at model level', function () { |
| 57 | + beforeEach(function (done) { |
| 58 | + Post.create({ |
| 59 | + title: 'my post', |
| 60 | + content: 'my post content' |
| 61 | + }, function (err, post) { |
| 62 | + expect(err).to.equal(null); |
| 63 | + post.comments.create({ |
| 64 | + title: 'My comment', |
| 65 | + comment: 'My comment text' |
| 66 | + }, function () { |
| 67 | + post.comments.create({ |
| 68 | + title: 'My second comment', |
| 69 | + comment: 'My second comment text' |
| 70 | + }, function () { |
| 71 | + post.author.create({ |
| 72 | + name: 'Joe' |
| 73 | + }, function () { |
| 74 | + post.category.create({ |
| 75 | + name: 'Programming' |
| 76 | + }, done); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('response', function () { |
| 84 | + |
| 85 | + it('should have key `included`', function (done) { |
| 86 | + request(app).get('/posts/1') |
| 87 | + .end(function (err, res) { |
| 88 | + expect(err).to.equal(null); |
| 89 | + expect(res.body.included).to.be.an('array'); |
| 90 | + done(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('attributes should not have relationship key', function (done) { |
| 95 | + request(app).get('/posts/1') |
| 96 | + .end(function (err, res) { |
| 97 | + expect(err).to.equal(null); |
| 98 | + expect(res.body.data.attributes).to.not.include.key('comments'); |
| 99 | + done(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('with include paramter should have both models', function (done) { |
| 104 | + request(app).get('/posts/1?filter[include]=author') |
| 105 | + .end(function (err, res) { |
| 106 | + expect(err).to.equal(null); |
| 107 | + expect(res.body.included.length).equal(3); |
| 108 | + expect(res.body.included[0].type).equal('authors'); |
| 109 | + expect(res.body.included[1].type).equal('comments'); |
| 110 | + done(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + }); |
| 116 | + |
| 117 | +}); |
0 commit comments