|
| 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 Rule; |
| 7 | +var Action; |
| 8 | +var ds; |
| 9 | + |
| 10 | +describe('Rule `hasMany` Action relationships', function () { |
| 11 | + beforeEach(function () { |
| 12 | + app = loopback(); |
| 13 | + app.set('legacyExplorer', false); |
| 14 | + ds = loopback.createDataSource('memory'); |
| 15 | + |
| 16 | + Action = ds.createModel('action', { |
| 17 | + name: String |
| 18 | + }); |
| 19 | + Action.settings.plural = 'actions'; |
| 20 | + app.model(Action); |
| 21 | + |
| 22 | + Rule = ds.createModel('rule', { |
| 23 | + name: String |
| 24 | + }); |
| 25 | + Rule.settings.plural = 'rules'; |
| 26 | + app.model(Rule); |
| 27 | + |
| 28 | + Rule.hasMany(Action, {as: 'actions', foreignKey: 'ruleId'}); |
| 29 | + app.use(loopback.rest()); |
| 30 | + JSONAPIComponent(app); |
| 31 | + }); |
| 32 | + |
| 33 | + describe.only('updating relationships using a PATCH', function (done) { |
| 34 | + beforeEach(function (done) { |
| 35 | + Action.create([ |
| 36 | + {name: 'action 1', ruleId: 10}, |
| 37 | + {name: 'action 2', ruleId: 10}, |
| 38 | + {name: 'action 3', ruleId: 10} |
| 39 | + ], function (err, actions) { |
| 40 | + if (err) console.error(err); |
| 41 | + Rule.create([ |
| 42 | + {name: 'rule 1'}, |
| 43 | + {name: 'rule 2'} |
| 44 | + ], done); |
| 45 | + }); |
| 46 | + }); |
| 47 | + it('should update foreign keys on Action model', function () { |
| 48 | + request(app).patch('/rules/2') |
| 49 | + .send({ |
| 50 | + data: { |
| 51 | + id: 2, |
| 52 | + attributes: { |
| 53 | + name: 'enter 2' |
| 54 | + }, |
| 55 | + relationships: { |
| 56 | + actions: { |
| 57 | + data: [ |
| 58 | + {type: 'actions', id: 2}, |
| 59 | + {type: 'actions', id: 3} |
| 60 | + ] |
| 61 | + } |
| 62 | + }, |
| 63 | + type: 'rules' |
| 64 | + } |
| 65 | + }) |
| 66 | + .set('Accept', 'application/vnd.api+json') |
| 67 | + .set('Content-Type', 'application/json') |
| 68 | + .end(function (err, res) { |
| 69 | + expect(err).to.equal(null); |
| 70 | + expect(res.status).to.equal(200); |
| 71 | + expect(res.body).not.to.have.keys('errors'); |
| 72 | + Rule.find(function (err, rules) { |
| 73 | + if (err) console.error(err); |
| 74 | + Action.find(function (err, actions) { |
| 75 | + if (err) console.error(err); |
| 76 | + expect(actions[0].ruleId).to.equal(10); |
| 77 | + expect(actions[1].ruleId).to.equal(2); |
| 78 | + expect(actions[2].ruleId).to.equal(2); |
| 79 | + done(); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments