@@ -305,6 +305,40 @@ describe('REST server tests', () => {
305
305
} ) ;
306
306
} ) ;
307
307
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 :
'[email protected] ' } ,
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 :
'[email protected] ' } ,
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
+
308
342
it ( 'fetches a related resource with a compound ID' , async ( ) => {
309
343
await prisma . user . create ( {
310
344
data : {
@@ -1427,7 +1461,21 @@ describe('REST server tests', () => {
1427
1461
expect ( r . status ) . toBe ( 201 ) ;
1428
1462
expect ( r . body ) . toMatchObject ( {
1429
1463
jsonapi : { version : '1.1' } ,
1430
- data :
{ type :
'user' , id :
'user1' , attributes :
{ email :
'[email protected] ' } } ,
1464
+ data : {
1465
+ type : 'user' ,
1466
+ id : 'user1' ,
1467
+ attributes :
{ email :
'[email protected] ' } ,
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
+ } ,
1431
1479
} ) ;
1432
1480
} ) ;
1433
1481
@@ -1785,6 +1833,54 @@ describe('REST server tests', () => {
1785
1833
} ) ;
1786
1834
} ) ;
1787
1835
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
+
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 :
'[email protected] ' } ,
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
+
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
+
1788
1884
it ( 'returns 404 if the user does not exist' , async ( ) => {
1789
1885
const r = await handler ( {
1790
1886
method : 'put' ,
0 commit comments