@@ -20,7 +20,7 @@ describe('loopback json api component find methods', function () {
2020 JSONAPIComponent ( app ) ;
2121 } ) ;
2222
23- describe ( 'headers ' , function ( ) {
23+ describe ( 'Headers ' , function ( ) {
2424 it ( 'GET /models should have the JSON API Content-Type header set on collection responses' , function ( done ) {
2525 request ( app ) . get ( '/posts' )
2626 . expect ( 200 )
@@ -36,7 +36,7 @@ describe('loopback json api component find methods', function () {
3636 } ) ;
3737 } ) ;
3838
39- describe ( 'relationship objects' , function ( ) {
39+ describe ( 'Relationship objects' , function ( ) {
4040 beforeEach ( function ( done ) {
4141 Post . create ( {
4242 title : 'my post' ,
@@ -77,42 +77,65 @@ describe('loopback json api component find methods', function () {
7777 Post . create ( {
7878 title : 'my post 2' ,
7979 content : 'my post content 2'
80- } , done ) ;
80+ } , function ( ) {
81+ Post . create ( {
82+ title : 'my post 3' ,
83+ content : 'my post content 3'
84+ } , done ) ;
85+ } ) ;
8186 } ) ;
8287 } ) ;
8388
8489 //TODO: see https://github.com/digitalsadhu/loopback-component-jsonapi/issues/11
85- it ( 'should produce correct top level self links' ) ;
90+ it ( 'GET /posts should produce top level self links' , function ( done ) {
91+ request ( app ) . get ( '/posts' )
92+ . expect ( 200 )
93+ . end ( function ( err , res ) {
94+ expect ( err ) . to . equal ( null ) ;
95+ expect ( res . body ) . to . have . deep . property ( 'links.self' ) ;
96+ expect ( res . body . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s $ / ) ;
97+ done ( ) ;
98+ } ) ;
99+ } ) ;
86100
87- it ( 'should produce resource level self links' , function ( done ) {
101+ it ( 'GET /posts/1 should produce resource level self links' , function ( done ) {
88102 request ( app ) . get ( '/posts/1' )
89103 . expect ( 200 )
90104 . end ( function ( err , res ) {
91105 expect ( err ) . to . equal ( null ) ;
92106 expect ( res . body ) . to . have . deep . property ( 'data.links.self' ) ;
93- expect ( res . body . data . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s \/ 1 / ) ;
107+ expect ( res . body . data . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s \/ 1 $ / ) ;
94108 done ( ) ;
95109 } ) ;
96110 } ) ;
97111
98- it ( 'should produce correct resource level self links for individual resources' , function ( done ) {
112+ it ( 'GET /posts/2 should produce correct resource level self links for individual resources' , function ( done ) {
99113 request ( app ) . get ( '/posts/2' )
100114 . expect ( 200 )
101115 . end ( function ( err , res ) {
102116 expect ( err ) . to . equal ( null ) ;
103117 expect ( res . body ) . to . have . deep . property ( 'data.links.self' ) ;
104- expect ( res . body . data . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s \/ 2 / ) ;
118+ expect ( res . body . data . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s \/ 2 $ / ) ;
105119 done ( ) ;
106120 } ) ;
107121 } ) ;
108122
109- it ( 'should produce correct resource level self links for collections' , function ( done ) {
123+ it ( 'GET /posts should produce correct resource level self links for collections' , function ( done ) {
110124 request ( app ) . get ( '/posts' )
111125 . expect ( 200 )
112126 . end ( function ( err , res ) {
127+ var index = 1 ;
113128 expect ( err ) . to . equal ( null ) ;
114- expect ( res . body . data [ 1 ] ) . to . have . deep . property ( 'links.self' ) ;
115- expect ( res . body . data [ 1 ] . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s \/ 2 / ) ;
129+ expect ( res . body . data ) . to . be . a ( 'array' ) ;
130+ expect ( res . body . links . self ) . to . match ( / h t t p : \/ \/ 1 2 7 \. 0 \. 0 \. 1 .* \/ p o s t s $ / ) ;
131+
132+ res . body . data . forEach ( function ( resource ) {
133+ expect ( resource . links . self ) . to . match ( new RegExp ( '^http://127.0.0.1.*/posts/' + index ) ) ;
134+ index ++ ;
135+ } ) ;
136+
137+ // Make sure we have tested all 3
138+ expect ( index - 1 ) . to . equal ( 3 ) ;
116139 done ( ) ;
117140 } ) ;
118141 } ) ;
0 commit comments