Skip to content

Commit ae5180f

Browse files
brunocalouBruno Calou
andauthored
fix: change reducer to check past and future lengths on Undo and Redo actions (#29)
Co-authored-by: Bruno Calou <[email protected]>
1 parent 00e52ce commit ae5180f

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

__test__/index.spec.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ const UndoComponent = ({ disabled }: { disabled: boolean }) => {
1616
},
1717
] = useUndo(0);
1818
const { present: presentCount } = countState;
19+
const doubleRedoCount = () => {
20+
redoCount();
21+
redoCount();
22+
};
23+
const doubleUndoCount = () => {
24+
undoCount();
25+
undoCount();
26+
};
1927

2028
return (
2129
<div>
@@ -57,6 +65,22 @@ const UndoComponent = ({ disabled }: { disabled: boolean }) => {
5765
>
5866
redo
5967
</button>
68+
<button
69+
key="double undo"
70+
data-testid="double undo"
71+
onClick={doubleUndoCount}
72+
disabled={disabled && !canUndo}
73+
>
74+
double undo
75+
</button>
76+
<button
77+
key="double redo"
78+
data-testid="double redo"
79+
onClick={doubleRedoCount}
80+
disabled={disabled && !canRedo}
81+
>
82+
double redo
83+
</button>
6084
<button key="reset" data-testid="reset" onClick={() => resetCount(0)}>
6185
reset to 0
6286
</button>
@@ -73,6 +97,8 @@ const setup = (defaultDisabled = true) => {
7397
const decrementButton = getByTestId('decrement') as HTMLButtonElement;
7498
const undoButton = getByTestId('undo') as HTMLButtonElement;
7599
const redoButton = getByTestId('redo') as HTMLButtonElement;
100+
const doubleUndoButton = getByTestId('double undo') as HTMLButtonElement;
101+
const doubleRedoButton = getByTestId('double redo') as HTMLButtonElement;
76102
const resetButton = getByTestId('reset') as HTMLButtonElement;
77103

78104
return {
@@ -82,6 +108,8 @@ const setup = (defaultDisabled = true) => {
82108
decrementButton,
83109
undoButton,
84110
redoButton,
111+
doubleUndoButton,
112+
doubleRedoButton,
85113
resetButton,
86114
};
87115
};
@@ -181,4 +209,34 @@ describe('use-undo', () => {
181209

182210
expect(count.textContent).toBe('count: 0');
183211
});
212+
213+
describe('when it can undo once', () => {
214+
describe('when calling undo multiple times in succession', () => {
215+
it('should return to the initial state', () => {
216+
const { count, doubleUndoButton, incrementButton } = setup();
217+
218+
fireEvent.click(incrementButton);
219+
220+
fireEvent.click(doubleUndoButton);
221+
222+
expect(count.textContent).toBe('count: 0');
223+
});
224+
});
225+
});
226+
227+
describe('when it can redo once', () => {
228+
describe('when calling redo multiple times in succession', () => {
229+
it('should return to the last state', () => {
230+
const { count, undoButton, doubleRedoButton, incrementButton } =
231+
setup();
232+
233+
fireEvent.click(incrementButton);
234+
fireEvent.click(undoButton);
235+
236+
fireEvent.click(doubleRedoButton);
237+
238+
expect(count.textContent).toBe('count: 1');
239+
});
240+
});
241+
});
184242
});

index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const reducer = <T>(state: State<T>, action: Action<T>) => {
3838

3939
switch (action.type) {
4040
case ActionType.Undo: {
41+
if (past.length === 0) {
42+
return state;
43+
}
44+
4145
const previous = past[past.length - 1];
4246
const newPast = past.slice(0, past.length - 1);
4347

@@ -49,6 +53,9 @@ const reducer = <T>(state: State<T>, action: Action<T>) => {
4953
}
5054

5155
case ActionType.Redo: {
56+
if (future.length === 0) {
57+
return state;
58+
}
5259
const next = future[0];
5360
const newFuture = future.slice(1);
5461

0 commit comments

Comments
 (0)