@@ -305,6 +305,40 @@ describe('REST server tests', () => {
305305 } ) ;
306306 } ) ;
307307
308+ it ( 'returns an empty data array when loading empty related resources' , async ( ) => {
309+ // Create a user first
310+ await prisma . user . create ( {
311+ data : { myId : 'user1' , email : 'user1@abc.com' } ,
312+ } ) ;
313+
314+ const r = await handler ( {
315+ method : 'get' ,
316+ path : '/user/user1' ,
317+ prisma,
318+ } ) ;
319+
320+ expect ( r . status ) . toBe ( 200 ) ;
321+ expect ( r . body ) . toMatchObject ( {
322+ data : {
323+ type : 'user' ,
324+ id : 'user1' ,
325+ attributes : { email : 'user1@abc.com' } ,
326+ links : {
327+ self : 'http://localhost/api/user/user1' ,
328+ } ,
329+ relationships : {
330+ posts : {
331+ links : {
332+ self : 'http://localhost/api/user/user1/relationships/posts' ,
333+ related : 'http://localhost/api/user/user1/posts' ,
334+ } ,
335+ data : [ ] ,
336+ } ,
337+ } ,
338+ } ,
339+ } ) ;
340+ } ) ;
341+
308342 it ( 'fetches a related resource with a compound ID' , async ( ) => {
309343 await prisma . user . create ( {
310344 data : {
@@ -1427,7 +1461,21 @@ describe('REST server tests', () => {
14271461 expect ( r . status ) . toBe ( 201 ) ;
14281462 expect ( r . body ) . toMatchObject ( {
14291463 jsonapi : { version : '1.1' } ,
1430- data : { type : 'user' , id : 'user1' , attributes : { email : 'user1@abc.com' } } ,
1464+ data : {
1465+ type : 'user' ,
1466+ id : 'user1' ,
1467+ attributes : { email : 'user1@abc.com' } ,
1468+ relationships : {
1469+ posts : {
1470+ links : {
1471+ self : 'http://localhost/api/user/user1/relationships/posts' ,
1472+ related : 'http://localhost/api/user/user1/posts' ,
1473+ } ,
1474+ data : [ ] ,
1475+ } ,
1476+ } ,
1477+ links : { self : 'http://localhost/api/user/user1' } ,
1478+ } ,
14311479 } ) ;
14321480 } ) ;
14331481
@@ -1785,6 +1833,54 @@ describe('REST server tests', () => {
17851833 } ) ;
17861834 } ) ;
17871835
1836+ it ( "returns an empty data list in relationships if it's empty" , async ( ) => {
1837+ await prisma . user . create ( {
1838+ data : {
1839+ myId : 'user1' ,
1840+ email : 'user1@abc.com' ,
1841+ } ,
1842+ } ) ;
1843+
1844+ const r = await handler ( {
1845+ method : 'put' ,
1846+ path : '/user/user1' ,
1847+ query : { } ,
1848+ requestBody : {
1849+ data : {
1850+ type : 'user' ,
1851+ attributes : { email : 'user2@abc.com' } ,
1852+ } ,
1853+ } ,
1854+ prisma,
1855+ } ) ;
1856+
1857+ expect ( r . status ) . toBe ( 200 ) ;
1858+ expect ( r . body ) . toMatchObject ( {
1859+ links : {
1860+ self : 'http://localhost/api/user/user1' ,
1861+ } ,
1862+ data : {
1863+ type : 'user' ,
1864+ id : 'user1' ,
1865+ attributes : {
1866+ email : 'user2@abc.com' ,
1867+ } ,
1868+ links : {
1869+ self : 'http://localhost/api/user/user1' ,
1870+ } ,
1871+ relationships : {
1872+ posts : {
1873+ links : {
1874+ self : 'http://localhost/api/user/user1/relationships/posts' ,
1875+ related : 'http://localhost/api/user/user1/posts' ,
1876+ } ,
1877+ data : [ ] ,
1878+ } ,
1879+ } ,
1880+ } ,
1881+ } ) ;
1882+ } ) ;
1883+
17881884 it ( 'returns 404 if the user does not exist' , async ( ) => {
17891885 const r = await handler ( {
17901886 method : 'put' ,
0 commit comments