1+ var request = require ( 'supertest' ) ;
2+ var loopback = require ( 'loopback' ) ;
3+ var expect = require ( 'chai' ) . expect ;
4+ var JSONAPIComponent = require ( '../' ) ;
5+ var app , Rule , Action , Card , ds ;
6+
7+ describe ( 'belongsTo through hasMany' , function ( ) {
8+ beforeEach ( function ( ) {
9+ app = loopback ( ) ;
10+ app . set ( 'legacyExplorer' , false ) ;
11+ ds = loopback . createDataSource ( 'memory' ) ;
12+
13+ Rule = ds . createModel ( 'rule' , {
14+ id : { type : Number , id : true } ,
15+ name : String
16+ } ) ;
17+
18+ app . model ( Rule ) ;
19+
20+ Action = ds . createModel ( 'action' , {
21+ id : { type : Number , id : true } ,
22+ cardId : Number ,
23+ name : String
24+ } ) ;
25+
26+ app . model ( Action ) ;
27+
28+ Card = ds . createModel ( 'card' , {
29+ id : { type : Number , id : true } ,
30+ title : String
31+ } ) ;
32+
33+ app . model ( Card ) ;
34+
35+ Rule . hasMany ( Action , { as : 'actions' , foreignKey : 'ruleId' } ) ;
36+ Action . belongsTo ( Card , { as : 'card' , foreignKey : 'cardId' } ) ;
37+ Card . hasMany ( Action , { as : 'actions' , foreignKey : 'cardId' } ) ;
38+ app . use ( loopback . rest ( ) ) ;
39+ JSONAPIComponent ( app , { restApiRoot : '/' } ) ;
40+ } ) ;
41+
42+ describe ( 'relationship should point to its relationship' , function ( ) {
43+
44+ beforeEach ( function ( done ) {
45+ Rule . create ( {
46+ name : 'Rule 1'
47+ } , function ( err , rule ) {
48+ expect ( err ) . to . equal ( null ) ;
49+ rule . actions . create ( [
50+ { name : 'Action 1' , cardId : 1 } ,
51+ { name : 'Action 2' , cardId : 1 }
52+ ] , function ( err , res ) {
53+ expect ( err ) . to . equal ( null ) ;
54+ Card . create ( [ { title : 'Card 1' } ] , done ) ;
55+ } ) ;
56+ } ) ;
57+ } ) ;
58+
59+ it ( 'GET /rules/1/actions' , function ( done ) {
60+ request ( app ) . get ( '/rules/1/actions' )
61+ . end ( function ( err , res ) {
62+ expect ( err ) . to . equal ( null ) ;
63+ expect ( res . body . data [ 0 ] . relationships . card ) . to . be . an ( 'object' ) ;
64+ expect ( res . body . data [ 0 ] . relationships . card . links ) . to . be . an ( 'object' ) ;
65+ expect ( res . body . data [ 0 ] . relationships . card . links . related ) . to . match ( / \/ a c t i o n s \/ 1 \/ c a r d / ) ;
66+ expect ( res . body . data [ 1 ] . relationships . card . links . related ) . to . match ( / \/ a c t i o n s \/ 2 \/ c a r d / ) ;
67+ done ( ) ;
68+ } ) ;
69+ } ) ;
70+
71+ } ) ;
72+
73+ } ) ;
0 commit comments