|
| 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 ds; |
| 7 | +var Post; |
| 8 | +var File; |
| 9 | + |
| 10 | +describe('loopback json api hasMany polymorphic relationships', function () { |
| 11 | + beforeEach(function () { |
| 12 | + app = loopback(); |
| 13 | + app.set('legacyExplorer', false); |
| 14 | + ds = loopback.createDataSource('memory'); |
| 15 | + |
| 16 | + File = ds.createModel('file', { |
| 17 | + id: {type: Number, id: true}, |
| 18 | + fileName: String, |
| 19 | + parentId: Number, |
| 20 | + parentType: String |
| 21 | + }); |
| 22 | + File.settings.plural = 'files'; |
| 23 | + app.model(File); |
| 24 | + |
| 25 | + Post = ds.createModel('post', { |
| 26 | + id: {type: Number, id: true}, |
| 27 | + title: String, |
| 28 | + content: String |
| 29 | + }); |
| 30 | + Post.settings.plural = 'posts'; |
| 31 | + Post.hasMany(File, { |
| 32 | + as: 'files', |
| 33 | + polymorphic: 'parent' |
| 34 | + }); |
| 35 | + app.model(Post); |
| 36 | + |
| 37 | + app.use(loopback.rest()); |
| 38 | + JSONAPIComponent(app); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('Post hasMany Files', function () { |
| 42 | + beforeEach(function (done) { |
| 43 | + Post.create({ |
| 44 | + title: 'Post One', |
| 45 | + content: 'Content' |
| 46 | + }, function (err, post) { |
| 47 | + expect(err).to.equal(null); |
| 48 | + File.create({ |
| 49 | + fileName: 'blah.jpg', |
| 50 | + parentId: post.id, |
| 51 | + parentType: 'post' |
| 52 | + }, done); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should have a relationship to Files', function (done) { |
| 57 | + request(app).get('/posts/1') |
| 58 | + .end(function (err, res) { |
| 59 | + expect(err).to.equal(null); |
| 60 | + expect(res.body.errors).to.equal(undefined); |
| 61 | + expect(res.body.data.relationships.files).to.be.an('object'); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return the Files that belong to this Post when included flag is present', function (done) { |
| 67 | + request(app).get('/posts/1?include=files') |
| 68 | + .end(function (err, res) { |
| 69 | + expect(err).to.equal(null); |
| 70 | + expect(res.body.errors).to.equal(undefined); |
| 71 | + expect(res.body.included).to.be.an('array'); |
| 72 | + expect(res.body.included[0].type).to.equal('files'); |
| 73 | + expect(res.body.included[0].id).to.equal('1'); |
| 74 | + done(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return the Files that belong to this Post when following the relationship link', function (done) { |
| 79 | + request(app).get('/posts/1') |
| 80 | + .end(function (err, res) { |
| 81 | + expect(err).to.equal(null); |
| 82 | + expect(res.body.errors).to.equal(undefined); |
| 83 | + request(app).get(res.body.data.relationships.files.links.related.split('api')[1]) |
| 84 | + .end(function (err, res) { |
| 85 | + expect(err).to.equal(null); |
| 86 | + expect(res.body.errors).to.equal(undefined); |
| 87 | + expect(res.body.data).to.be.an('array'); |
| 88 | + expect(res.body.data[0].type).to.equal('files'); |
| 89 | + expect(res.body.data[0].id).to.equal('1'); |
| 90 | + done(); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments