@@ -13,6 +13,8 @@ const service = new Service(db)
1313
1414const POSTS = 'posts'
1515const COMMENTS = 'comments'
16+ const OBJECT = 'object'
17+
1618const UNKNOWN_RESOURCE = 'xxx'
1719const UNKNOWN_ID = 'xxx'
1820
@@ -40,10 +42,15 @@ const post3 = {
4042const comment1 = { id : '1' , title : 'a' , postId : '1' }
4143const items = 3
4244
45+ const obj = {
46+ f1 : 'foo' ,
47+ }
48+
4349function reset ( ) {
4450 db . data = structuredClone ( {
4551 posts : [ post1 , post2 , post3 ] ,
4652 comments : [ comment1 ] ,
53+ object : obj ,
4754 } )
4855}
4956
@@ -57,6 +64,8 @@ type Test = {
5764
5865await test ( 'findById' , ( ) => {
5966 reset ( )
67+ if ( ! Array . isArray ( db . data ?. [ POSTS ] ) )
68+ throw new Error ( 'posts should be an array' )
6069 assert . deepEqual ( service . findById ( POSTS , '1' , { } ) , db . data ?. [ POSTS ] ?. [ 0 ] )
6170 assert . equal ( service . findById ( POSTS , UNKNOWN_ID , { } ) , undefined )
6271 assert . deepEqual ( service . findById ( POSTS , '1' , { _embed : [ 'comments' ] } ) , {
@@ -236,6 +245,10 @@ await test('find', async (t) => {
236245 name : UNKNOWN_RESOURCE ,
237246 res : undefined ,
238247 } ,
248+ {
249+ name : OBJECT ,
250+ res : obj ,
251+ } ,
239252 ]
240253 for ( const tc of arr ) {
241254 await t . test ( `${ tc . name } ${ JSON . stringify ( tc . params ) } ` , ( ) => {
@@ -261,44 +274,65 @@ await test('create', async () => {
261274} )
262275
263276await test ( 'update' , async ( ) => {
277+ reset ( )
278+ const obj = { f1 : 'bar' }
279+ const res = await service . update ( OBJECT , obj )
280+ assert . equal ( res , obj )
281+
282+ assert . equal (
283+ await service . update ( UNKNOWN_RESOURCE , obj ) ,
284+ undefined ,
285+ 'should ignore unknown resources' ,
286+ )
287+ assert . equal (
288+ await service . update ( POSTS , { } ) ,
289+ undefined ,
290+ 'should ignore arrays' ,
291+ )
292+ } )
293+
294+ await test ( 'updateById' , async ( ) => {
264295 reset ( )
265296 const post = { id : 'xxx' , title : 'updated post' }
266- const res = await service . update ( POSTS , post1 . id , post )
297+ const res = await service . updateById ( POSTS , post1 . id , post )
267298 assert . equal ( res ?. [ 'id' ] , post1 . id , 'id should not change' )
268299 assert . equal ( res ?. [ 'title' ] , post . title )
269300
270301 assert . equal (
271- await service . update ( UNKNOWN_RESOURCE , post1 . id , post ) ,
302+ await service . updateById ( UNKNOWN_RESOURCE , post1 . id , post ) ,
272303 undefined ,
273304 )
274- assert . equal ( await service . update ( POSTS , UNKNOWN_ID , post ) , undefined )
305+ assert . equal ( await service . updateById ( POSTS , UNKNOWN_ID , post ) , undefined )
275306} )
276307
277- await test ( 'patch ' , async ( ) => {
308+ await test ( 'patchById ' , async ( ) => {
278309 reset ( )
279310 const post = { id : 'xxx' , title : 'updated post' }
280- const res = await service . patch ( POSTS , post1 . id , post )
311+ const res = await service . patchById ( POSTS , post1 . id , post )
281312 assert . notEqual ( res , undefined )
282313 assert . equal ( res ?. [ 'id' ] , post1 . id )
283314 assert . equal ( res ?. [ 'title' ] , post . title )
284315
285- assert . equal ( await service . patch ( UNKNOWN_RESOURCE , post1 . id , post ) , undefined )
286- assert . equal ( await service . patch ( POSTS , UNKNOWN_ID , post ) , undefined )
316+ assert . equal (
317+ await service . patchById ( UNKNOWN_RESOURCE , post1 . id , post ) ,
318+ undefined ,
319+ )
320+ assert . equal ( await service . patchById ( POSTS , UNKNOWN_ID , post ) , undefined )
287321} )
288322
289323await test ( 'destroy' , async ( ) => {
290324 reset ( )
291- let prevLength = db . data ?. [ POSTS ] ?. length || 0
292- await service . destroy ( POSTS , post1 . id )
325+ let prevLength = Number ( db . data ?. [ POSTS ] ?. length ) || 0
326+ await service . destroyById ( POSTS , post1 . id )
293327 assert . equal ( db . data ?. [ POSTS ] ?. length , prevLength - 1 )
294328 assert . deepEqual ( db . data ?. [ COMMENTS ] , [ { ...comment1 , postId : null } ] )
295329
296330 reset ( )
297331 prevLength = db . data ?. [ POSTS ] ?. length || 0
298- await service . destroy ( POSTS , post1 . id , [ COMMENTS ] )
332+ await service . destroyById ( POSTS , post1 . id , [ COMMENTS ] )
299333 assert . equal ( db . data [ POSTS ] . length , prevLength - 1 )
300334 assert . equal ( db . data [ COMMENTS ] . length , 0 )
301335
302- assert . equal ( await service . destroy ( UNKNOWN_RESOURCE , post1 . id ) , undefined )
303- assert . equal ( await service . destroy ( POSTS , UNKNOWN_ID ) , undefined )
336+ assert . equal ( await service . destroyById ( UNKNOWN_RESOURCE , post1 . id ) , undefined )
337+ assert . equal ( await service . destroyById ( POSTS , UNKNOWN_ID ) , undefined )
304338} )
0 commit comments