@@ -2,10 +2,7 @@ var request = require('supertest');
22var loopback = require ( 'loopback' ) ;
33var expect = require ( 'chai' ) . expect ;
44var JSONAPIComponent = require ( '../' ) ;
5- var ds ;
6- var app ;
7- var Post ;
8- var Comment ;
5+ var ds , app , Post , Author , Comment , Category ;
96
107describe ( 'include option' , function ( ) {
118 beforeEach ( function ( ) {
@@ -26,11 +23,30 @@ describe('include option', function () {
2623 Comment = ds . createModel ( 'comment' , {
2724 id : { type : Number , id : true } ,
2825 postId : Number ,
26+ authorId : Number ,
2927 title : String ,
3028 comment : String
3129 } ) ;
30+
3231 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+
3347 Post . hasMany ( Comment , { as : 'comments' , foreignKey : 'postId' } ) ;
48+ Post . belongsTo ( Author , { as : 'author' , foreignKey : 'authorId' } ) ;
49+ Post . belongsTo ( Category , { as : 'category' , foreignKey : 'categoryId' } ) ;
3450 Comment . settings . plural = 'comments' ;
3551
3652 app . use ( loopback . rest ( ) ) ;
@@ -51,12 +67,20 @@ describe('include option', function () {
5167 post . comments . create ( {
5268 title : 'My second comment' ,
5369 comment : 'My second comment text'
54- } , done ) ;
70+ } , function ( ) {
71+ post . author . create ( {
72+ name : 'Joe'
73+ } , function ( ) {
74+ post . category . create ( {
75+ name : 'Programming'
76+ } , done ) ;
77+ } ) ;
78+ } ) ;
5579 } ) ;
5680 } ) ;
5781 } ) ;
5882
59- describe ( 'hasMany response' , function ( ) {
83+ describe ( 'response' , function ( ) {
6084
6185 it ( 'should have key `included`' , function ( done ) {
6286 request ( app ) . get ( '/posts/1' )
@@ -76,7 +100,18 @@ describe('include option', function () {
76100 } ) ;
77101 } ) ;
78102
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+ } ) ;
79113 } ) ;
114+
80115 } ) ;
81116
82117} ) ;
0 commit comments