@@ -650,6 +650,103 @@ describe("ReactiveArray", () => {
650650 } ) ;
651651 } ) ;
652652
653+ describe ( "replace" , ( ) => {
654+ it ( "should replace the array with the specified elements" , ( ) => {
655+ const arr = reactiveArray ( [ 1 , 2 , 3 ] ) ;
656+ const spy = vi . fn ( ) ;
657+ arr . $ . reaction ( spy ) ;
658+
659+ expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
660+
661+ arr . replace ( [ 4 , 5 , 6 ] ) ;
662+
663+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
664+
665+ expect ( arr ) . toEqual ( [ 4 , 5 , 6 ] ) ;
666+ } ) ;
667+
668+ it ( "should replace the array with less elements" , ( ) => {
669+ const arr = reactiveArray ( [ 1 , 2 , 3 ] ) ;
670+ const spy = vi . fn ( ) ;
671+ arr . $ . reaction ( spy ) ;
672+
673+ expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
674+
675+ arr . replace ( [ 1 ] ) ;
676+
677+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
678+
679+ expect ( arr ) . toEqual ( [ 1 ] ) ;
680+ } ) ;
681+
682+ it ( "should replace the array with different order" , ( ) => {
683+ const arr = reactiveArray ( [ 1 , 2 , 3 ] ) ;
684+ const spy = vi . fn ( ) ;
685+ arr . $ . reaction ( spy ) ;
686+
687+ expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
688+
689+ arr . replace ( [ 3 , 2 , 1 ] ) ;
690+
691+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
692+
693+ expect ( arr ) . toEqual ( [ 3 , 2 , 1 ] ) ;
694+ } ) ;
695+
696+ it ( "should replace an empty array with the specified elements" , ( ) => {
697+ const arr = reactiveArray < number > ( [ ] ) ;
698+
699+ const spy = vi . fn ( ) ;
700+ arr . $ . reaction ( spy ) ;
701+
702+ expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
703+
704+ arr . replace ( [ 1 , 2 , 3 ] ) ;
705+
706+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
707+
708+ expect ( arr ) . toEqual ( [ 1 , 2 , 3 ] ) ;
709+ } ) ;
710+
711+ it ( "should notify on replace" , ( ) => {
712+ const arr = reactiveArray ( [ "a" , "b" , "c" ] ) ;
713+ const mockNotify = vi . fn ( ) ;
714+ const dispose = arr . $ . reaction ( mockNotify ) ;
715+
716+ arr . replace ( [ "x" , "y" , "z" ] ) ;
717+ expect ( mockNotify ) . toHaveBeenCalledTimes ( 1 ) ;
718+ expect ( mockNotify ) . lastCalledWith ( arr ) ;
719+
720+ dispose ( ) ;
721+ } ) ;
722+
723+ it ( "should not notify if not changed" , ( ) => {
724+ const arr = reactiveArray ( [ 1 ] ) ;
725+ const mockNotify = vi . fn ( ) ;
726+ const dispose = arr . $ . reaction ( mockNotify ) ;
727+
728+ arr . replace ( [ 2 , 3 ] ) ;
729+ expect ( mockNotify ) . toHaveBeenCalledTimes ( 1 ) ;
730+ expect ( mockNotify ) . lastCalledWith ( arr ) ;
731+
732+ dispose ( ) ;
733+ } ) ;
734+
735+ it ( "should notify if some keys are removed" , ( ) => {
736+ const arr = reactiveArray ( [ 1 , 2 , 3 ] ) ;
737+ const mockNotify = vi . fn ( ) ;
738+ const dispose = arr . $ . reaction ( mockNotify ) ;
739+
740+ expect ( arr ) . toEqual ( [ 1 , 2 , 3 ] ) ;
741+
742+ arr . replace ( [ 1 , 2 ] ) ;
743+ expect ( mockNotify ) . toHaveBeenCalledTimes ( 1 ) ;
744+ expect ( arr ) . toEqual ( [ 1 , 2 ] ) ;
745+
746+ dispose ( ) ;
747+ } ) ;
748+ } ) ;
749+
653750 describe . each ( [ "test" , "production" ] ) ( "dispose [%s]" , NODE_ENV => {
654751 const originalEnv = process . env . NODE_ENV ;
655752
0 commit comments