|
|
|
import undoReducer, { |
|
undoSnapshot, undo, redo, clearUndoState, MAX_STACK_SIZE |
|
} from '../../src/reducers/undo'; |
|
|
|
test('initialState', () => { |
|
let defaultState; |
|
|
|
expect(undoReducer(defaultState , {type: 'anything'} )).toBeDefined(); |
|
expect(undoReducer(defaultState , {type: 'anything'} ).pointer).toEqual(-1); |
|
expect(undoReducer(defaultState , {type: 'anything'} ).stack).toHaveLength(0); |
|
}); |
|
|
|
test('snapshot', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
const state2 = {state: 2}; |
|
|
|
let reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
expect(reduxState.pointer).toEqual(0); |
|
expect(reduxState.stack).toHaveLength(1); |
|
expect(reduxState.stack[0]).toEqual(state1); |
|
|
|
reduxState = undoReducer(reduxState , undoSnapshot([state2]) ); |
|
expect(reduxState.pointer).toEqual(1); |
|
expect(reduxState.stack).toHaveLength(2); |
|
expect(reduxState.stack[0]).toEqual(state1); |
|
expect(reduxState.stack[1]).toEqual(state2); |
|
}); |
|
|
|
test('invalidSnapshot', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
|
|
const reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
const newReduxState = undoReducer(reduxState , undoSnapshot() ); |
|
expect(reduxState).toEqual(newReduxState); |
|
}); |
|
|
|
test('clearUndoState', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
const state2 = {state: 2}; |
|
|
|
|
|
const reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
undoReducer(reduxState , undoSnapshot([state2]) ); |
|
const newReduxState = undoReducer(reduxState , clearUndoState() ); |
|
|
|
expect(newReduxState.pointer).toEqual(-1); |
|
expect(newReduxState.stack).toHaveLength(0); |
|
}); |
|
|
|
test('cantUndo', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
|
|
|
|
let reduxState = undoReducer(defaultState , undo() ); |
|
|
|
expect(reduxState.pointer).toEqual(-1); |
|
expect(reduxState.stack).toHaveLength(0); |
|
|
|
|
|
reduxState = undoReducer(reduxState , undoSnapshot([state1]) ); |
|
reduxState = undoReducer(reduxState , undo() ); |
|
|
|
expect(reduxState.pointer).toEqual(0); |
|
expect(reduxState.stack).toHaveLength(1); |
|
}); |
|
|
|
test('cantRedo', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
|
|
let reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
|
|
|
|
reduxState = undoReducer(reduxState , redo() ); |
|
|
|
expect(reduxState.pointer).toEqual(0); |
|
expect(reduxState.stack).toHaveLength(1); |
|
}); |
|
|
|
test('undo', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
const state2 = {state: 2}; |
|
|
|
|
|
let reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
reduxState = undoReducer(reduxState , undoSnapshot([state2]) ); |
|
reduxState = undoReducer(reduxState , undo() ); |
|
|
|
expect(reduxState.pointer).toEqual(0); |
|
expect(reduxState.stack).toHaveLength(2); |
|
expect(reduxState.stack[0]).toEqual(state1); |
|
expect(reduxState.stack[1]).toEqual(state2); |
|
}); |
|
|
|
test('redo', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
const state2 = {state: 2}; |
|
|
|
|
|
let reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
reduxState = undoReducer(reduxState , undoSnapshot([state2]) ); |
|
let newReduxState = undoReducer(reduxState , undo() ); |
|
|
|
|
|
newReduxState = undoReducer(newReduxState , redo() ); |
|
expect(newReduxState.pointer).toEqual(reduxState.pointer); |
|
expect(newReduxState.stack).toHaveLength(reduxState.stack.length); |
|
expect(newReduxState.stack[0]).toEqual(reduxState.stack[0]); |
|
expect(reduxState.stack[1]).toEqual(reduxState.stack[1]); |
|
}); |
|
|
|
test('undoSnapshotCantRedo', () => { |
|
let defaultState; |
|
const state1 = {state: 1}; |
|
const state2 = {state: 2}; |
|
const state3 = {state: 3}; |
|
|
|
|
|
let reduxState = undoReducer(defaultState , undoSnapshot([state1]) ); |
|
reduxState = undoReducer(reduxState , undoSnapshot([state2]) ); |
|
reduxState = undoReducer(reduxState , undo() ); |
|
|
|
expect(reduxState.pointer).toEqual(0); |
|
expect(reduxState.stack).toHaveLength(2); |
|
|
|
|
|
reduxState = undoReducer(reduxState , undoSnapshot([state3]) ); |
|
|
|
const newReduxState = undoReducer(reduxState , redo() ); |
|
|
|
expect(newReduxState.pointer).toEqual(reduxState.pointer); |
|
expect(newReduxState.stack).toHaveLength(reduxState.stack.length); |
|
expect(newReduxState.stack[0]).toEqual(reduxState.stack[0]); |
|
expect(newReduxState.stack[1]).toEqual(state3); |
|
}); |
|
|
|
test('snapshotAtMaxStackSize', () => { |
|
let defaultState; |
|
const getState = function (num) { |
|
return {state: num}; |
|
}; |
|
|
|
let num = 1; |
|
let reduxState = undoReducer(defaultState , undoSnapshot([getState(num)]) ); |
|
for (num = 2; num <= MAX_STACK_SIZE; num++) { |
|
reduxState = undoReducer(reduxState , undoSnapshot([getState(num)]) ); |
|
} |
|
|
|
expect(reduxState.pointer).toEqual(MAX_STACK_SIZE - 1); |
|
expect(reduxState.stack).toHaveLength(MAX_STACK_SIZE); |
|
expect(reduxState.stack[0].state).toEqual(1); |
|
|
|
|
|
reduxState = undoReducer(reduxState , undoSnapshot([getState(num)]) ); |
|
|
|
|
|
expect(reduxState.pointer).toEqual(MAX_STACK_SIZE - 1); |
|
expect(reduxState.stack).toHaveLength(MAX_STACK_SIZE); |
|
expect(reduxState.stack[0].state).toEqual(2); |
|
expect(reduxState.stack[MAX_STACK_SIZE - 1].state).toEqual(MAX_STACK_SIZE + 1); |
|
}); |
|
|
|
test('undoRedoAtMaxStackSize', () => { |
|
let defaultState; |
|
const getState = function (num) { |
|
return {state: num}; |
|
}; |
|
|
|
let num = 1; |
|
let reduxState = undoReducer(defaultState , undoSnapshot([getState(num)]) ); |
|
for (num = 2; num <= MAX_STACK_SIZE; num++) { |
|
reduxState = undoReducer(reduxState , undoSnapshot([getState(num)]) ); |
|
} |
|
|
|
|
|
reduxState = undoReducer(reduxState , undo() ); |
|
reduxState = undoReducer(reduxState , undo() ); |
|
reduxState = undoReducer(reduxState , redo() ); |
|
|
|
expect(reduxState.pointer).toEqual(MAX_STACK_SIZE - 2); |
|
expect(reduxState.stack).toHaveLength(MAX_STACK_SIZE); |
|
expect(reduxState.stack[0].state).toEqual(1); |
|
}); |
|
|
|
test('undoSnapshotAtMaxStackSize', () => { |
|
let defaultState; |
|
const getState = function (num) { |
|
return {state: num}; |
|
}; |
|
|
|
let num = 1; |
|
let reduxState = undoReducer(defaultState , undoSnapshot([getState(num)]) ); |
|
for (num = 2; num <= MAX_STACK_SIZE; num++) { |
|
reduxState = undoReducer(reduxState , undoSnapshot([getState(num)]) ); |
|
} |
|
|
|
|
|
reduxState = undoReducer(reduxState , undo() ); |
|
reduxState = undoReducer(reduxState , undo() ); |
|
reduxState = undoReducer(reduxState , undoSnapshot([getState(num)]) ); |
|
|
|
expect(reduxState.pointer).toEqual(MAX_STACK_SIZE - 2); |
|
expect(reduxState.stack).toHaveLength(MAX_STACK_SIZE - 1); |
|
expect(reduxState.stack[0].state).toEqual(1); |
|
expect(reduxState.stack[MAX_STACK_SIZE - 2].state).toEqual(MAX_STACK_SIZE + 1); |
|
expect(reduxState.stack[MAX_STACK_SIZE - 3].state).toEqual(MAX_STACK_SIZE - 2); |
|
}); |
|
|