|
| 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 | +var Person; |
| 9 | +var ds; |
| 10 | + |
| 11 | +describe('loopback json api hasOne relationships', function() { |
| 12 | + beforeEach(function() { |
| 13 | + app = loopback(); |
| 14 | + app.set('legacyExplorer', false); |
| 15 | + ds = loopback.createDataSource('memory'); |
| 16 | + Post = ds.createModel('post', { |
| 17 | + id: {type: Number, id: true}, |
| 18 | + title: String, |
| 19 | + content: String |
| 20 | + }); |
| 21 | + app.model(Post); |
| 22 | + |
| 23 | + Comment = ds.createModel('comment', { |
| 24 | + id: {type: Number, id: true}, |
| 25 | + postId: Number, |
| 26 | + title: String, |
| 27 | + comment: String |
| 28 | + }); |
| 29 | + app.model(Comment); |
| 30 | + Comment.settings.plural = 'comments'; |
| 31 | + Comment.belongsTo(Post, {as: 'post', foreignKey: 'postId'}); |
| 32 | + |
| 33 | + app.use(loopback.rest()); |
| 34 | + JSONAPIComponent(app); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('Comments without a post', function (done) { |
| 38 | + beforeEach(function (done) { |
| 39 | + Comment.create({ |
| 40 | + title: 'my comment', |
| 41 | + comment: 'my post comment' |
| 42 | + }, done); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('GET /comments/1/post', function () { |
| 46 | + it('should return status code 200 OK', function (done) { |
| 47 | + request(app).get('/comments/1/post') |
| 48 | + .expect(200) |
| 49 | + .end(done); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return `null` keyed by `data`', function (done) { |
| 53 | + request(app).get('/comments/1/post') |
| 54 | + .end(function (err, res) { |
| 55 | + expect(res.body).to.be.an('object'); |
| 56 | + expect(res.body.links).to.be.an('object'); |
| 57 | + expect(res.body.links.self).to.match(/comments\/1\/post/); |
| 58 | + expect(res.body.data).to.equal(null); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('GET /comments/1/relationships/post', function () { |
| 65 | + it('should return status code 200 OK', function (done) { |
| 66 | + request(app).get('/comments/1/relationships/post') |
| 67 | + .expect(200) |
| 68 | + .end(done); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return `null` keyed by `data`', function (done) { |
| 72 | + request(app).get('/comments/1/relationships/post') |
| 73 | + .end(function (err, res) { |
| 74 | + expect(res.body).to.be.an('object'); |
| 75 | + expect(res.body.links).to.be.an('object'); |
| 76 | + expect(res.body.links.self).to.match(/comments\/1\/relationships\/post/); |
| 77 | + expect(res.body.links.related).to.match(/comments\/1\/post/); |
| 78 | + expect(res.body.data).to.equal(null); |
| 79 | + done(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('Comment with an post', function (done) { |
| 86 | + beforeEach(function (done) { |
| 87 | + Comment.create({ |
| 88 | + title: 'my comment', |
| 89 | + comment: 'my post comment' |
| 90 | + }, function (err, comment) { |
| 91 | + comment.post.create({ |
| 92 | + title: 'My post', |
| 93 | + content: 'My post content' |
| 94 | + }, done); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('GET /comments/1/post', function () { |
| 99 | + it('should return status code 200 OK', function (done) { |
| 100 | + request(app).get('/comments/1/post') |
| 101 | + .expect(200) |
| 102 | + .end(done); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should display a single resource object keyed by `data`', function (done) { |
| 106 | + request(app).get('/comments/1/post') |
| 107 | + .end(function (err, res) { |
| 108 | + expect(res.body).to.be.an('object'); |
| 109 | + expect(res.body.links).to.be.an('object'); |
| 110 | + expect(res.body.links.self).to.match(/comments\/1\/post/); |
| 111 | + expect(res.body.data.attributes).to.deep.equal({ |
| 112 | + title: 'My post', |
| 113 | + content: 'My post content' |
| 114 | + }); |
| 115 | + expect(res.body.data.type).to.equal('posts'); |
| 116 | + expect(res.body.data.id).to.equal('1'); |
| 117 | + expect(res.body.data.links.self).to.match(/^http.*\/posts\/1$/); |
| 118 | + done(); |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('GET /comments/1/relationships/post', function () { |
| 124 | + it('should return status code 200 OK', function (done) { |
| 125 | + request(app).get('/comments/1/relationships/post') |
| 126 | + .expect(200) |
| 127 | + .end(done); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should display a single resource object keyed by `data`', function (done) { |
| 131 | + request(app).get('/comments/1/relationships/post') |
| 132 | + .end(function (err, res) { |
| 133 | + expect(res.body).to.be.an('object'); |
| 134 | + expect(res.body.links).to.be.an('object'); |
| 135 | + expect(res.body.links.self).to.match(/comments\/1\/relationships\/post/); |
| 136 | + expect(res.body.links.related).to.match(/comments\/1\/post/); |
| 137 | + expect(res.body.data).to.deep.equal({ |
| 138 | + type: 'posts', |
| 139 | + id: '1' |
| 140 | + }); |
| 141 | + done(); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments