@@ -1546,6 +1546,116 @@ describe(`Collection`, () => {
15461546 const state = await collection . stateWhenReady ( )
15471547 expect ( state . size ) . toBe ( 3 )
15481548 } )
1549+
1550+ it ( `should allow deleting a row by passing only the key to write function` , async ( ) => {
1551+ let testSyncFunctions : any = null
1552+
1553+ const collection = createCollection < { id : number ; value : string } > ( {
1554+ id : `delete-by-key` ,
1555+ getKey : ( item ) => item . id ,
1556+ startSync : true ,
1557+ sync : {
1558+ sync : ( { begin, write, commit, markReady } ) => {
1559+ // Store the sync functions for testing
1560+ testSyncFunctions = { begin, write, commit, markReady }
1561+ } ,
1562+ } ,
1563+ } )
1564+
1565+ // Collection should start in loading state
1566+ expect ( collection . status ) . toBe ( `loading` )
1567+ expect ( collection . size ) . toBe ( 0 )
1568+
1569+ const { begin, write, commit, markReady } = testSyncFunctions
1570+
1571+ // Insert some initial data
1572+ begin ( )
1573+ write ( { type : `insert` , value : { id : 1 , value : `item 1` } } )
1574+ write ( { type : `insert` , value : { id : 2 , value : `item 2` } } )
1575+ write ( { type : `insert` , value : { id : 3 , value : `item 3` } } )
1576+ commit ( )
1577+
1578+ // Verify data was inserted
1579+ expect ( collection . size ) . toBe ( 3 )
1580+ expect ( collection . state . get ( 1 ) ) . toEqual ( { id : 1 , value : `item 1` } )
1581+ expect ( collection . state . get ( 2 ) ) . toEqual ( { id : 2 , value : `item 2` } )
1582+ expect ( collection . state . get ( 3 ) ) . toEqual ( { id : 3 , value : `item 3` } )
1583+
1584+ // Delete a row by passing only the key (no value)
1585+ begin ( )
1586+ write ( { type : `delete` , key : 2 } )
1587+ commit ( )
1588+
1589+ // Verify the row is gone
1590+ expect ( collection . size ) . toBe ( 2 )
1591+ expect ( collection . state . get ( 1 ) ) . toEqual ( { id : 1 , value : `item 1` } )
1592+ expect ( collection . state . get ( 2 ) ) . toBeUndefined ( )
1593+ expect ( collection . state . get ( 3 ) ) . toEqual ( { id : 3 , value : `item 3` } )
1594+
1595+ // Delete another row by key only
1596+ begin ( )
1597+ write ( { type : `delete` , key : 1 } )
1598+ commit ( )
1599+
1600+ // Verify both rows are gone
1601+ expect ( collection . size ) . toBe ( 1 )
1602+ expect ( collection . state . get ( 1 ) ) . toBeUndefined ( )
1603+ expect ( collection . state . get ( 2 ) ) . toBeUndefined ( )
1604+ expect ( collection . state . get ( 3 ) ) . toEqual ( { id : 3 , value : `item 3` } )
1605+
1606+ // Mark as ready
1607+ markReady ( )
1608+
1609+ // Verify final state
1610+ expect ( collection . status ) . toBe ( `ready` )
1611+ expect ( collection . size ) . toBe ( 1 )
1612+ expect ( Array . from ( collection . state . keys ( ) ) ) . toEqual ( [ 3 ] )
1613+ } )
1614+
1615+ it ( `should allow deleting a row by key with string keys` , async ( ) => {
1616+ let testSyncFunctions : any = null
1617+
1618+ const collection = createCollection < { id : string ; name : string } > ( {
1619+ id : `delete-by-string-key` ,
1620+ getKey : ( item ) => item . id ,
1621+ startSync : true ,
1622+ sync : {
1623+ sync : ( { begin, write, commit, markReady } ) => {
1624+ // Store the sync functions for testing
1625+ testSyncFunctions = { begin, write, commit, markReady }
1626+ } ,
1627+ } ,
1628+ } )
1629+
1630+ const { begin, write, commit, markReady } = testSyncFunctions
1631+
1632+ // Insert initial data
1633+ begin ( )
1634+ write ( { type : `insert` , value : { id : `a` , name : `Alice` } } )
1635+ write ( { type : `insert` , value : { id : `b` , name : `Bob` } } )
1636+ write ( { type : `insert` , value : { id : `c` , name : `Charlie` } } )
1637+ commit ( )
1638+
1639+ // Verify data was inserted
1640+ expect ( collection . size ) . toBe ( 3 )
1641+ expect ( collection . state . get ( `a` ) ) . toEqual ( { id : `a` , name : `Alice` } )
1642+ expect ( collection . state . get ( `b` ) ) . toEqual ( { id : `b` , name : `Bob` } )
1643+ expect ( collection . state . get ( `c` ) ) . toEqual ( { id : `c` , name : `Charlie` } )
1644+
1645+ // Delete by key only
1646+ begin ( )
1647+ write ( { type : `delete` , key : `b` } )
1648+ commit ( )
1649+
1650+ // Verify the row is gone
1651+ expect ( collection . size ) . toBe ( 2 )
1652+ expect ( collection . state . get ( `a` ) ) . toEqual ( { id : `a` , name : `Alice` } )
1653+ expect ( collection . state . get ( `b` ) ) . toBeUndefined ( )
1654+ expect ( collection . state . get ( `c` ) ) . toEqual ( { id : `c` , name : `Charlie` } )
1655+
1656+ markReady ( )
1657+ expect ( collection . status ) . toBe ( `ready` )
1658+ } )
15491659} )
15501660
15511661describe ( `Collection isLoadingSubset property` , ( ) => {
0 commit comments