@@ -2,6 +2,7 @@ var request = require('supertest');
22var loopback = require ( 'loopback' ) ;
33var expect = require ( 'chai' ) . expect ;
44var JSONAPIComponent = require ( '../' ) ;
5+ var RSVP = require ( 'rsvp' ) ;
56var app , User , Interest , Topic ;
67
78describe ( 'through Model' , function ( ) {
@@ -43,10 +44,10 @@ describe('through Model', function () {
4344
4445 it ( 'should allow interest to be created' , function ( done ) {
4546
46- User . create ( { name : 'User 1' } , function ( err , user ) {
47+ User . create ( { name : 'User 1' } , function ( err , user ) {
4748 expect ( err ) . to . equal ( null ) ;
4849
49- Topic . create ( { name : 'Topic 1' } , function ( err , topic ) {
50+ Topic . create ( { name : 'Topic 1' } , function ( err , topic ) {
5051 expect ( err ) . to . equal ( null ) ;
5152
5253 request ( app )
@@ -73,4 +74,70 @@ describe('through Model', function () {
7374
7475 } ) ;
7576
77+ it ( 'should retrieve user topics via include' , function ( done ) {
78+
79+ makeData ( )
80+ . then ( function ( ) {
81+ request ( app )
82+ . get ( '/users/1?include=topics' )
83+ . end ( function ( err , res ) {
84+ expect ( err ) . to . equal ( null ) ;
85+ expect ( res ) . to . have . deep . property ( 'body.data.relationships.topics.data' ) . and . have . property ( 'length' ) . and . equal ( 2 ) ;
86+ expect ( res ) . to . have . deep . property ( 'body.data.relationships.topics.data[0].type' ) . to . equal ( 'topics' ) ;
87+ expect ( res ) . to . have . deep . property ( 'body.data.relationships.topics.data[1].type' ) . to . equal ( 'topics' ) ;
88+ expect ( res ) . to . have . deep . property ( 'body.included[0].type' ) . and . equal ( 'topics' ) ;
89+ expect ( res ) . to . have . deep . property ( 'body.included[0].id' ) . and . equal ( '1' ) ;
90+ expect ( res ) . to . have . deep . property ( 'body.included[1].type' ) . and . equal ( 'topics' ) ;
91+ expect ( res ) . to . have . deep . property ( 'body.included[1].id' ) . and . equal ( '2' ) ;
92+ done ( err ) ;
93+ } ) ;
94+ } )
95+ . catch ( done ) ;
96+
97+ } ) ;
98+
99+ it ( 'should retrieve topic users via include' , function ( done ) {
100+
101+ makeData ( )
102+ . then ( function ( ) {
103+ request ( app )
104+ . get ( '/topics/1?include=users' )
105+ . end ( function ( err , res ) {
106+ expect ( err ) . to . equal ( null ) ;
107+ expect ( res ) . to . have . deep . property ( 'body.data.relationships.users.data' ) . and . have . property ( 'length' ) . and . equal ( 1 ) ;
108+ expect ( res ) . to . have . deep . property ( 'body.data.relationships.users.data[0].type' ) . to . equal ( 'users' ) ;
109+ expect ( res ) . to . have . deep . property ( 'body.included[0].type' ) . and . equal ( 'users' ) ;
110+ expect ( res ) . to . have . deep . property ( 'body.included[0].id' ) . and . equal ( '1' ) ;
111+ done ( err ) ;
112+ } ) ;
113+ } )
114+ . catch ( done ) ;
115+
116+ } ) ;
117+
76118} ) ;
119+
120+ function makeData ( done ) {
121+ var createUser = denodeifyCreate ( User ) ;
122+ var createTopic = denodeifyCreate ( Topic ) ;
123+ var createInterest = denodeifyCreate ( Interest ) ;
124+
125+ return RSVP . hash ( {
126+ user : createUser ( { name : 'User 1' } ) ,
127+ topics : RSVP . all ( [
128+ createTopic ( { name : 'Topic 1' } ) ,
129+ createTopic ( { name : 'Topic 2' } )
130+ ] )
131+ } )
132+ . then ( function ( models ) {
133+ return RSVP . all ( [
134+ createInterest ( { userId : models . user . id , topicId : models . topics [ 0 ] . id } ) ,
135+ createInterest ( { userId : models . user . id , topicId : models . topics [ 1 ] . id } )
136+ ] ) ;
137+ } ) ;
138+
139+ function denodeifyCreate ( Model ) {
140+ return RSVP . denodeify ( Model . create . bind ( Model ) ) ;
141+ }
142+ }
143+
0 commit comments