@@ -106,4 +106,101 @@ describe('loopback json api belongsTo polymorphic relationships', function () {
106106 }
107107 )
108108 } )
109+
110+ describe ( 'File with no relationship to a Post' , function ( ) {
111+ beforeEach ( function ( done ) {
112+ FileModel . create (
113+ {
114+ fileName : 'blah.jpg'
115+ } ,
116+ done
117+ )
118+ } )
119+
120+ it ( 'should return a null value for relationship data' , function ( done ) {
121+ request ( app ) . get ( '/fileModels/1' ) . end ( function ( err , res ) {
122+ expect ( err ) . to . equal ( null )
123+ expect ( res . body ) . to . not . have . key ( 'errors' )
124+ expect ( res . body . data . relationships . parent ) . to . be . an ( 'object' )
125+ expect ( res . body . data . relationships . parent . data ) . to . equal ( null )
126+ done ( )
127+ } )
128+ } )
129+ } )
130+
131+ describe ( 'Create relationship via API' , function ( ) {
132+ beforeEach ( function ( done ) {
133+ Post . create (
134+ {
135+ title : 'Post One' ,
136+ content : 'Content'
137+ } ,
138+ done
139+ )
140+ } )
141+
142+ it ( 'should define a relationship to Post when file is created' , function (
143+ done
144+ ) {
145+ request ( app )
146+ . post ( '/fileModels' )
147+ . send ( {
148+ data : {
149+ type : 'fileModels' ,
150+ attributes : {
151+ fileName : 'blah.jpg'
152+ } ,
153+ relationships : {
154+ parent : {
155+ data : {
156+ id : 1 ,
157+ type : 'posts'
158+ }
159+ }
160+ }
161+ }
162+ } )
163+ . set ( 'accept' , 'application/vnd.api+json' )
164+ . set ( 'content-type' , 'application/json' )
165+ . expect ( 201 )
166+ . end ( function ( err , res ) {
167+ expect ( err ) . to . equal ( null )
168+ expect ( res . body ) . to . not . have . key ( 'errors' )
169+ expect ( res . body . data . relationships . parent ) . to . be . an ( 'object' )
170+ expect ( res . body . data . relationships . parent . data ) . to . be . an ( 'object' )
171+ done ( )
172+ } )
173+ } )
174+
175+ it ( 'should ignore relationships with an invalid type' , function ( done ) {
176+ request ( app )
177+ . post ( '/fileModels' )
178+ . send ( {
179+ data : {
180+ type : 'fileModels' ,
181+ attributes : {
182+ fileName : 'blah.jpg'
183+ } ,
184+ relationships : {
185+ parent : {
186+ data : {
187+ id : 1 ,
188+ type : 'invalidType'
189+ }
190+ }
191+ }
192+ }
193+ } )
194+ . set ( 'accept' , 'application/vnd.api+json' )
195+ . set ( 'content-type' , 'application/json' )
196+ . expect ( 201 )
197+ . end ( function ( err , res ) {
198+ expect ( err ) . to . equal ( null )
199+ expect ( res . body ) . to . not . have . key ( 'errors' )
200+ expect ( res . body . data . relationships . parent ) . to . be . an ( 'object' )
201+ expect ( res . body . data . relationships . parent . data ) . to . equal ( null )
202+ done ( )
203+ } )
204+ } )
205+ } )
109206} )
0 commit comments