1- import { describe , it , expect , vi } from "vitest" ;
1+ import { describe , it , expect , vi , afterAll , beforeAll } from "vitest" ;
22
33import { batch , reactiveMap } from "../../src" ;
44
@@ -37,6 +37,14 @@ describe("ReactiveMap", () => {
3737 expect ( map . get ( "foo" ) ) . toEqual ( 1 ) ;
3838 } ) ;
3939
40+ it ( "should update existing value" , ( ) => {
41+ const map = reactiveMap < string , number > ( ) ;
42+ map . set ( "foo" , 1 ) ;
43+ expect ( map . get ( "foo" ) ) . toEqual ( 1 ) ;
44+ map . set ( "foo" , 2 ) ;
45+ expect ( map . get ( "foo" ) ) . toEqual ( 2 ) ;
46+ } ) ;
47+
4048 it ( "should notify on set" , ( ) => {
4149 const map = reactiveMap < string , number > ( ) ;
4250
@@ -449,7 +457,17 @@ describe("ReactiveMap", () => {
449457 } ) ;
450458 } ) ;
451459
452- describe ( "dispose" , ( ) => {
460+ describe . each ( [ "test" , "production" ] ) ( "dispose [%s]" , NODE_ENV => {
461+ const originalEnv = process . env . NODE_ENV ;
462+
463+ beforeAll ( ( ) => {
464+ process . env . NODE_ENV = NODE_ENV ;
465+ } ) ;
466+
467+ afterAll ( ( ) => {
468+ process . env . NODE_ENV = originalEnv ;
469+ } ) ;
470+
453471 it ( "should clear the map and dispose of resources" , ( ) => {
454472 const consoleErrorMock = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => void 0 ) ;
455473
@@ -495,6 +513,14 @@ describe("ReactiveMap", () => {
495513
496514 consoleErrorMock . mockRestore ( ) ;
497515 } ) ;
516+
517+ it ( "should allow disposing multiple times" , ( ) => {
518+ const map = reactiveMap < string , number > ( ) ;
519+ expect ( ( ) => {
520+ map . dispose ( ) ;
521+ map . dispose ( ) ;
522+ } ) . not . toThrow ( ) ;
523+ } ) ;
498524 } ) ;
499525
500526 describe ( "onChanged" , ( ) => {
@@ -521,6 +547,10 @@ describe("ReactiveMap", () => {
521547 } ) ;
522548
523549 it ( "should not notify listeners after dispose" , ( ) => {
550+ const consoleErrorMock = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => void 0 ) ;
551+
552+ expect ( consoleErrorMock ) . not . toBeCalled ( ) ;
553+
524554 const map = reactiveMap < string , number > ( ) ;
525555 const listener = vi . fn ( ) ;
526556
@@ -529,6 +559,9 @@ describe("ReactiveMap", () => {
529559
530560 map . set ( "foo" , 1 ) ;
531561 expect ( listener ) . toHaveBeenCalledTimes ( 0 ) ;
562+ expect ( consoleErrorMock ) . toBeCalled ( ) ;
563+
564+ consoleErrorMock . mockRestore ( ) ;
532565 } ) ;
533566
534567 it ( "should not notify disposed listeners" , ( ) => {
@@ -541,6 +574,28 @@ describe("ReactiveMap", () => {
541574 map . set ( "foo" , 1 ) ;
542575 expect ( listener ) . toHaveBeenCalledTimes ( 0 ) ;
543576 } ) ;
577+
578+ it ( "should not notify removed listeners" , ( ) => {
579+ const map = reactiveMap < string , number > ( ) ;
580+ const listener1 = vi . fn ( ) ;
581+ const dispose = map . onChanged ( listener1 ) ;
582+
583+ map . set ( "foo" , 1 ) ;
584+ expect ( listener1 ) . toHaveBeenCalledTimes ( 1 ) ;
585+
586+ listener1 . mockClear ( ) ;
587+
588+ dispose ( ) ;
589+ map . set ( "bar" , 2 ) ;
590+ expect ( listener1 ) . toHaveBeenCalledTimes ( 0 ) ;
591+
592+ const listener2 = vi . fn ( ) ;
593+ map . onChanged ( listener2 ) ;
594+
595+ map . set ( "baz" , 3 ) ;
596+ expect ( listener2 ) . toHaveBeenCalledWith ( { upsert : [ [ "baz" , 3 ] ] , delete : [ ] } ) ;
597+ expect ( listener1 ) . toHaveBeenCalledTimes ( 0 ) ;
598+ } ) ;
544599 } ) ;
545600
546601 describe ( "onDisposeValue" , ( ) => {
@@ -567,5 +622,23 @@ describe("ReactiveMap", () => {
567622 expect ( listener1 ) . toHaveBeenCalledWith ( 2 ) ;
568623 expect ( listener2 ) . toHaveBeenCalledTimes ( 0 ) ;
569624 } ) ;
625+
626+ it ( "should not notify removed listeners" , ( ) => {
627+ const map = reactiveMap < string , number > ( ) ;
628+ const listener = vi . fn ( ) ;
629+ const dispose = map . onDisposeValue ( listener ) ;
630+
631+ map . set ( "foo" , 1 ) ;
632+ map . delete ( "foo" ) ;
633+ expect ( listener ) . toHaveBeenCalledTimes ( 1 ) ;
634+ expect ( listener ) . toHaveBeenCalledWith ( 1 ) ;
635+
636+ listener . mockClear ( ) ;
637+
638+ dispose ( ) ;
639+ map . set ( "bar" , 2 ) ;
640+ map . delete ( "bar" ) ;
641+ expect ( listener ) . toHaveBeenCalledTimes ( 0 ) ;
642+ } ) ;
570643 } ) ;
571644} ) ;
0 commit comments