@@ -33,6 +33,90 @@ describe('NgUndoStateActions', () => {
33
33
34
34
expect ( nextState ) . toBe ( currentState ) ; // Ensure the current state is returned
35
35
} ) ;
36
+
37
+ test ( 'should return the currentState if snapshot is undefined' , ( ) => {
38
+ const key = 'test' ;
39
+ const settings = {
40
+ path : [ ] ,
41
+ filter : ( ) => true ,
42
+ limit : 5
43
+ } ;
44
+
45
+ const currentState = { [ HISTORY_STATE_KEY ] : { [ key ] : { past : [ ] } } } ;
46
+ const snapshot = undefined ;
47
+ const actions = new NgUndoStateActions ( key , settings ) ;
48
+ const result = actions . insert ( currentState , snapshot ) ;
49
+
50
+
51
+ expect ( result ) . toBe ( currentState ) ;
52
+ } ) ;
53
+
54
+ test ( 'should return the currentState if the filter function returns false' , ( ) => {
55
+ const key = 'test' ;
56
+ const settings = {
57
+ path : [ ] ,
58
+ filter : ( ) => false ,
59
+ limit : 5
60
+ } ;
61
+ const currentState = { [ HISTORY_STATE_KEY ] : { [ key ] : { past : [ ] } } } ;
62
+ const snapshot = 'snapshot' ;
63
+ const actions = new NgUndoStateActions ( key , settings ) ;
64
+ const result = actions . insert ( currentState , snapshot ) ;
65
+
66
+ expect ( result ) . toBe ( currentState ) ;
67
+ } ) ;
68
+
69
+ test ( 'should insert the snapshot into the past array and update the state' , ( ) => {
70
+ const key = 'test' ;
71
+ const settings = {
72
+ path : [ ] ,
73
+ filter : ( ) => true ,
74
+ limit : 5
75
+ } ;
76
+ const currentState = { [ HISTORY_STATE_KEY ] : { [ key ] : { past : [ 'past1' , 'past2' ] } } } ;
77
+ const snapshot = 'snapshot' ;
78
+ const expectedState = {
79
+ [ HISTORY_STATE_KEY ] : {
80
+ [ key ] : {
81
+ past : [ 'past1' , 'past2' , 'snapshot' ] ,
82
+ future : [ ]
83
+ }
84
+ }
85
+ } ;
86
+
87
+ const actions = new NgUndoStateActions ( key , settings ) ;
88
+ const result = actions . insert ( currentState , snapshot ) ;
89
+
90
+ expect ( result ) . toEqual ( expectedState ) ;
91
+ } ) ;
92
+
93
+ test ( 'should remove the oldest past snapshot if the past array exceeds the limit' , ( ) => {
94
+ const key = 'test' ;
95
+ const settings = {
96
+ path : [ ] ,
97
+ filter : ( ) => true ,
98
+ limit : 2
99
+ } ;
100
+ const currentState = {
101
+ [ HISTORY_STATE_KEY ] : {
102
+ [ key ] : { past : [ 'past1' , 'past2' , 'past3' ] }
103
+ }
104
+ } ;
105
+ const snapshot = 'snapshot' ;
106
+ const expectedState = {
107
+ [ HISTORY_STATE_KEY ] : {
108
+ [ key ] : {
109
+ past : [ 'past2' , 'past3' , 'snapshot' ] ,
110
+ future : [ ]
111
+ }
112
+ }
113
+ } ;
114
+
115
+ const actions = new NgUndoStateActions ( key , settings ) ;
116
+ const result = actions . insert ( currentState , snapshot ) ;
117
+
118
+ expect ( result ) . toEqual ( expectedState ) ;
119
+ } ) ;
36
120
} ) ;
37
121
38
122
describe ( 'undo' , ( ) => {
0 commit comments