@@ -378,6 +378,45 @@ describe("ReactiveMap", () => {
378378
379379 dispose ( ) ;
380380 } ) ;
381+ test ( ".clear() notifies only listeners of existing members" , ( ) =>
382+ createRoot ( dispose => {
383+ const map = new ReactiveMap ( [
384+ [ 1 , "a" ] ,
385+ [ 2 , "b" ] ,
386+ [ 3 , "c" ] ,
387+ ] ) ;
388+
389+ const existingKey = vi . fn ( ) ;
390+ createComputed ( ( ) => existingKey ( map . has ( 2 ) ) ) ;
391+
392+ const existingValue = vi . fn ( ) ;
393+ createComputed ( ( ) => existingValue ( map . get ( 2 ) ) ) ;
394+
395+ const nonexistingKey = vi . fn ( ) ;
396+ createComputed ( ( ) => nonexistingKey ( map . has ( 4 ) ) ) ;
397+
398+ const nonexistingValue = vi . fn ( ) ;
399+ createComputed ( ( ) => nonexistingValue ( map . get ( 4 ) ) ) ;
400+
401+ expect ( existingKey ) . toHaveBeenNthCalledWith ( 1 , true ) ;
402+ expect ( existingValue ) . toHaveBeenNthCalledWith ( 1 , "b" ) ;
403+
404+ expect ( nonexistingKey ) . toHaveBeenNthCalledWith ( 1 , false ) ;
405+ expect ( nonexistingValue ) . toHaveBeenNthCalledWith ( 1 , undefined ) ;
406+
407+ map . clear ( ) ;
408+
409+ expect ( existingKey ) . toHaveBeenCalledTimes ( 2 ) ;
410+ expect ( existingKey ) . toHaveBeenNthCalledWith ( 2 , false ) ;
411+
412+ expect ( existingValue ) . toHaveBeenCalledTimes ( 2 ) ;
413+ expect ( existingValue ) . toHaveBeenNthCalledWith ( 2 , undefined ) ;
414+
415+ expect ( nonexistingKey ) . toHaveBeenCalledTimes ( 1 ) ;
416+ expect ( nonexistingValue ) . toHaveBeenCalledTimes ( 1 ) ;
417+
418+ dispose ( ) ;
419+ } ) ) ;
381420} ) ;
382421
383422describe ( "ReactiveWeakMap" , ( ) => {
0 commit comments