Skip to content

Commit c29bc90

Browse files
committed
add action to inset() for use in the filter settings.
1 parent 64b77e3 commit c29bc90

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

projects/angular-redux2/undo/src/components/undo-state.component.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('NgUndoStateActions', () => {
1414
};
1515

1616
const snapshot = 'new snapshot';
17-
const nextState = undoStateActions.insert(currentState, snapshot);
17+
const nextState = undoStateActions.insert({}, currentState, snapshot);
1818

1919
expect(nextState).not.toBe(currentState);
2020
expect(nextState.data).toBe(currentState.data);
@@ -29,7 +29,7 @@ describe('NgUndoStateActions', () => {
2929
};
3030

3131
const snapshot = undefined;
32-
const nextState = undoStateActions.insert(currentState, snapshot);
32+
const nextState = undoStateActions.insert({}, currentState, snapshot);
3333

3434
expect(nextState).toBe(currentState); // Ensure the current state is returned
3535
});
@@ -45,7 +45,7 @@ describe('NgUndoStateActions', () => {
4545
const currentState = { [HISTORY_STATE_KEY]: { [key]: { past: [] } } };
4646
const snapshot = undefined;
4747
const actions = new NgUndoStateActions(key, settings);
48-
const result = actions.insert(currentState, snapshot);
48+
const result = actions.insert({}, currentState, snapshot);
4949

5050

5151
expect(result).toBe(currentState);
@@ -61,7 +61,7 @@ describe('NgUndoStateActions', () => {
6161
const currentState = { [HISTORY_STATE_KEY]: { [key]: { past: [] } } };
6262
const snapshot = 'snapshot';
6363
const actions = new NgUndoStateActions(key, settings);
64-
const result = actions.insert(currentState, snapshot);
64+
const result = actions.insert({}, currentState, snapshot);
6565

6666
expect(result).toBe(currentState);
6767
});
@@ -85,7 +85,7 @@ describe('NgUndoStateActions', () => {
8585
};
8686

8787
const actions = new NgUndoStateActions(key, settings);
88-
const result = actions.insert(currentState, snapshot);
88+
const result = actions.insert({}, currentState, snapshot);
8989

9090
expect(result).toEqual(expectedState);
9191
});
@@ -113,7 +113,7 @@ describe('NgUndoStateActions', () => {
113113
};
114114

115115
const actions = new NgUndoStateActions(key, settings);
116-
const result = actions.insert(currentState, snapshot);
116+
const result = actions.insert({}, currentState, snapshot);
117117

118118
expect(result).toEqual(expectedState);
119119
});

projects/angular-redux2/undo/src/components/undo-state.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ export class NgUndoStateActions {
3131
protected key: string;
3232

3333
/**
34-
*
35-
* @protected
34+
* Represents the settings for an undo action.
3635
*/
3736

3837
protected settings: {
39-
filter: () => boolean;
38+
filter: (action: any, currentState: any, snapshot: any) => boolean;
4039
path: Array<string | number>
4140
limit: number;
4241
};
@@ -71,13 +70,14 @@ export class NgUndoStateActions {
7170
* It adds the given snapshot to the past states of the history.
7271
* If the snapshot is undefined, it returns the current state unchanged.
7372
*
73+
* @param {NgUndoAction} action - The dispatched undo action object.
7474
* @param {any} currentState - The current state object.
7575
* @param {any} snapshot - The snapshot to insert into the history.
7676
* @returns {any} - The state object with the snapshot inserted into the history.
7777
*/
7878

79-
insert(currentState: any, snapshot: any): any {
80-
if (snapshot === undefined || !this.settings.filter()) {
79+
insert(action: any, currentState: any, snapshot: any): any {
80+
if (snapshot === undefined || !this.settings.filter(action, currentState, snapshot)) {
8181
return currentState;
8282
}
8383

projects/angular-redux2/undo/src/interfaces/undo.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface NgUndoAction {
112112
export interface StateWatchMap {
113113
[key: string]: {
114114
path: string;
115-
filter?: () => boolean;
115+
filter?: (action: any, currentState: any, snapshot: any) => boolean;
116116
limit?: number;
117117
}
118118
}

projects/angular-redux2/undo/src/services/undo.service.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import { ngUndoMiddleware, UndoService } from './undo.service';
66
import { UNDO_REDUCER_PREFIX, UndoActions } from '../interfaces/undo.interface';
7-
import { NgUndoStateActions } from '../components/undo-state.component';
87

98
jest.mock('@angular-redux2/store', () => ({
109
get: jest.fn()
@@ -26,11 +25,15 @@ describe('UndoService', () => {
2625
});
2726

2827
describe('watcherState', () => {
28+
const next = jest.fn();
29+
30+
afterEach(() => {
31+
jest.resetAllMocks();
32+
});
33+
2934
test('should call watcherAction and detectChange methods', () => {
3035
const state = { /* mock state object */ };
3136
const action = { type: UNDO_REDUCER_PREFIX };
32-
const next = jest.fn();
33-
3437
const undoService = new UndoService({});
3538

3639
(undoService as any).watcherAction = jest.fn().mockReturnValue(state);
@@ -39,7 +42,7 @@ describe('UndoService', () => {
3942
const updatedState = undoService.watcherState(state, action, next);
4043

4144
expect((undoService as any).watcherAction).toHaveBeenCalledWith(state, action);
42-
expect((undoService as any).detectChange).toHaveBeenCalledWith(state, next(state, action));
45+
expect((undoService as any).detectChange).toHaveBeenCalledWith(action, state, next(state, action));
4346
expect(updatedState).toBe(state);
4447
expect(next).toHaveBeenCalledWith(state, action);
4548
});
@@ -48,8 +51,6 @@ describe('UndoService', () => {
4851
// Mock input data
4952
const state = { /* mock state object */ };
5053
const action = { type: 'SOME_OTHER_ACTION' };
51-
const next = jest.fn();
52-
5354
const undoService = new UndoService({});
5455

5556
(undoService as any).watcherAction = jest.fn().mockReturnValue(state);
@@ -58,7 +59,7 @@ describe('UndoService', () => {
5859
const updatedState = undoService.watcherState(state, action, next);
5960

6061
expect((undoService as any).watcherAction).not.toHaveBeenCalled();
61-
expect((undoService as any).detectChange).toHaveBeenCalledWith(state, next(state, action));
62+
expect((undoService as any).detectChange).toHaveBeenCalledWith(action, state, next(state, action));
6263
expect(updatedState).toBe(state);
6364
expect(next).toHaveBeenCalledWith(state, action);
6465
});

projects/angular-redux2/undo/src/services/undo.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class UndoService {
6666
state = this.watcherAction(state, action);
6767
}
6868

69-
return this.detectChange(state, next(state, action));
69+
return this.detectChange(action, state, next(state, action));
7070
}
7171

7272
/**
@@ -108,20 +108,21 @@ export class UndoService {
108108
* Detects changes in the state and inserts snapshots into the undo history when necessary.
109109
*
110110
* @protected
111+
* @param {any} action - The dispatched action object.
111112
* @param {any} state - The current state object.
112113
* @param {any} newState - The updated state object.
113114
* @returns {any} - The updated state with inserted snapshots.
114115
*/
115116

116-
protected detectChange(state: any, newState: any): any {
117+
protected detectChange(action: any, state: any, newState: any): any {
117118
for (const propertyName in this.watchStateMap) {
118119
const undoAction = this.watchStateMap[propertyName];
119120

120121
const sliceState = get(state, undoAction.path);
121122
const sliceNewState = get(newState, undoAction.path);
122123

123124
if (sliceNewState !== sliceState) {
124-
newState = undoAction.insert(newState, sliceState);
125+
newState = undoAction.insert(action, newState, sliceState);
125126
}
126127
}
127128

0 commit comments

Comments
 (0)