├── .husky ├── .gitignore └── pre-commit ├── .prettierrc ├── jest.config.js ├── .editorconfig ├── tsconfig.json ├── LICENSE ├── .gitignore ├── package.json ├── index.ts ├── README.md └── __test__ ├── index.spec.tsx └── checkpoint.spec.tsx /.husky/.gitignore: -------------------------------------------------------------------------------- 1 | _ 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "es5", 3 | "singleQuote": true 4 | } 5 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | npm run pretty-quick 5 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | preset: 'ts-jest', 3 | testEnvironment: 'jsdom', 4 | }; 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | indent_size = 2 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": false, 4 | "module": "ESNext", 5 | "target": "ESNext", 6 | "lib": ["dom", "esnext"], 7 | "esModuleInterop": true, 8 | "moduleResolution": "node", 9 | "outDir": "lib", 10 | "jsx": "react", 11 | "noUnusedLocals": true, 12 | "noUnusedParameters": true, 13 | "preserveConstEnums": true, 14 | "sourceMap": false, 15 | "strict": true 16 | }, 17 | "exclude": ["node_modules", "lib"] 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-present Homer Chen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | 63 | # lib 64 | lib/ 65 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "use-undo", 3 | "version": "1.1.1", 4 | "description": "undo/redo functionality with React Hooks", 5 | "license": "MIT", 6 | "author": "homerchen19", 7 | "homepage": "https://github.com/homerchen19/use-undo#readme", 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/homerchen19/use-undo.git" 11 | }, 12 | "bugs": { 13 | "url": "https://github.com/homerchen19/use-undo/issues" 14 | }, 15 | "source": "index.ts", 16 | "main": "lib/use-undo.js", 17 | "umd:main": "lib/use-undo.umd.js", 18 | "module": "lib/use-undo.m.js", 19 | "types": "lib/index.d.ts", 20 | "scripts": { 21 | "build": "rimraf lib && microbundle -o lib/ --name use-undo --sourcemap false --no-compress", 22 | "prepublishOnly": "yarn build", 23 | "preversion": "yarn test:cov", 24 | "test": "jest", 25 | "test:cov": "jest --coverage --runInBand --forceExit", 26 | "test:watch": "jest --watch", 27 | "pre-commit": "pretty-quick --staged", 28 | "pretty-quick": "pretty-quick", 29 | "prepare": "husky install" 30 | }, 31 | "files": [ 32 | "lib" 33 | ], 34 | "devDependencies": { 35 | "@testing-library/react": "^11.2.7", 36 | "@types/jest": "^27", 37 | "@types/node": "^15.6.1", 38 | "@types/react": "^17.0.8", 39 | "@types/react-dom": "^17.0.5", 40 | "husky": "^6.0.0", 41 | "jest": "^27.0.3", 42 | "microbundle": "^0.13.1", 43 | "prettier": "^2.3.0", 44 | "pretty-quick": "^3.1.0", 45 | "react": "^17.0.2", 46 | "react-dom": "^17.0.2", 47 | "rimraf": "^3.0.2", 48 | "ts-jest": "^27.0.1", 49 | "typescript": "^4.3.2" 50 | }, 51 | "peerDependencies": { 52 | "react": ">=16.8.6", 53 | "react-dom": ">=16.8.6" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /index.ts: -------------------------------------------------------------------------------- 1 | import { useReducer, useCallback } from 'react'; 2 | 3 | enum ActionType { 4 | Undo = 'UNDO', 5 | Redo = 'REDO', 6 | Set = 'SET', 7 | Reset = 'RESET', 8 | } 9 | 10 | export interface Actions { 11 | set: (newPresent: T, checkpoint?: boolean) => void; 12 | reset: (newPresent: T) => void; 13 | undo: () => void; 14 | redo: () => void; 15 | canUndo: boolean; 16 | canRedo: boolean; 17 | } 18 | 19 | interface Action { 20 | type: ActionType; 21 | historyCheckpoint?: boolean; 22 | newPresent?: T; 23 | } 24 | 25 | export interface State { 26 | past: T[]; 27 | present: T; 28 | future: T[]; 29 | } 30 | 31 | const initialState = { 32 | past: [], 33 | present: null, 34 | future: [], 35 | }; 36 | 37 | type Options = { 38 | useCheckpoints?: boolean; 39 | }; 40 | 41 | const useUndo = ( 42 | initialPresent: T, 43 | opts: Options = {} 44 | ): [State, Actions] => { 45 | const { useCheckpoints }: Options = { 46 | useCheckpoints: false, 47 | ...opts, 48 | }; 49 | 50 | const reducer = (state: State, action: Action) => { 51 | const { past, present, future } = state; 52 | 53 | switch (action.type) { 54 | case ActionType.Undo: { 55 | if (past.length === 0) { 56 | return state; 57 | } 58 | 59 | const previous = past[past.length - 1]; 60 | const newPast = past.slice(0, past.length - 1); 61 | 62 | return { 63 | past: newPast, 64 | present: previous, 65 | future: [present, ...future], 66 | }; 67 | } 68 | 69 | case ActionType.Redo: { 70 | if (future.length === 0) { 71 | return state; 72 | } 73 | const next = future[0]; 74 | const newFuture = future.slice(1); 75 | 76 | return { 77 | past: [...past, present], 78 | present: next, 79 | future: newFuture, 80 | }; 81 | } 82 | 83 | case ActionType.Set: { 84 | const isNewCheckpoint = useCheckpoints 85 | ? !!action.historyCheckpoint 86 | : true; 87 | const { newPresent } = action; 88 | 89 | if (newPresent === present) { 90 | return state; 91 | } 92 | 93 | return { 94 | past: isNewCheckpoint === false ? past : [...past, present], 95 | present: newPresent, 96 | future: [], 97 | }; 98 | } 99 | 100 | case ActionType.Reset: { 101 | const { newPresent } = action; 102 | 103 | return { 104 | past: [], 105 | present: newPresent, 106 | future: [], 107 | }; 108 | } 109 | } 110 | }; 111 | 112 | const [state, dispatch] = useReducer(reducer, { 113 | ...initialState, 114 | present: initialPresent, 115 | }) as [State, React.Dispatch>]; 116 | 117 | const canUndo = state.past.length !== 0; 118 | const canRedo = state.future.length !== 0; 119 | const undo = useCallback(() => { 120 | if (canUndo) { 121 | dispatch({ type: ActionType.Undo }); 122 | } 123 | }, [canUndo]); 124 | const redo = useCallback(() => { 125 | if (canRedo) { 126 | dispatch({ type: ActionType.Redo }); 127 | } 128 | }, [canRedo]); 129 | const set = useCallback((newPresent: T, checkpoint = false) => { 130 | dispatch({ 131 | type: ActionType.Set, 132 | newPresent, 133 | historyCheckpoint: checkpoint, 134 | }); 135 | }, []); 136 | const reset = useCallback( 137 | (newPresent: T) => dispatch({ type: ActionType.Reset, newPresent }), 138 | [] 139 | ); 140 | 141 | return [state, { set, reset, undo, redo, canUndo, canRedo }]; 142 | }; 143 | 144 | export default useUndo; 145 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ♻️ use-undo 2 | 3 | undo/redo functionality with React [Hooks](https://reactjs.org/docs/hooks-intro.html). 4 | 5 |

6 | 7 | 8 |

9 | 10 |

11 | screensho 12 |

13 | 14 | ## Installation 15 | 16 | ```sh 17 | yarn add use-undo 18 | ``` 19 | 20 | ## Usage 21 | 22 | [![Edit use-undo-demo](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/use-undo-demo-hifeo?fontsize=14&hidenavigation=1&theme=dark&view=editor) 23 | 24 | ```js 25 | import React from 'react'; 26 | import ReactDOM from 'react-dom'; 27 | import useUndo from 'use-undo'; 28 | 29 | const App = () => { 30 | const [ 31 | countState, 32 | { 33 | set: setCount, 34 | reset: resetCount, 35 | undo: undoCount, 36 | redo: redoCount, 37 | canUndo, 38 | canRedo, 39 | }, 40 | ] = useUndo(0); 41 | const { present: presentCount } = countState; 42 | 43 | return ( 44 |
45 |

You clicked {presentCount} times

46 | 49 | 52 | 55 | 58 | 61 |
62 | ); 63 | }; 64 | ``` 65 | 66 | ## Manual Checkpoints 67 | 68 | Manual checkpoints are helpful also when you want manual control over checkpoints. For example it is more helpful when you want to handle input type html tag where value needs to be handled alongside the undo and redo functionality should be handled over some conditions. 69 | 70 | ```js 71 | import React from 'react'; 72 | import ReactDOM from 'react-dom'; 73 | import useUndo from 'use-undo'; 74 | 75 | const App = () => { 76 | const [ 77 | countState, 78 | { 79 | set: setCount, 80 | reset: resetCount, 81 | undo: undoCount, 82 | redo: redoCount, 83 | canUndo, 84 | canRedo, 85 | }, 86 | ] = useUndo(0, { useCheckpoints: true }); 87 | const { present: presentCount } = countState; 88 | 89 | return ( 90 |
91 |

You clicked {presentCount} times

92 | 95 | 98 | 101 | 104 | 107 | 110 | 113 |
114 | ); 115 | }; 116 | ``` 117 | 118 | ## API 119 | 120 | ### useUndo 121 | 122 | ```js 123 | const [state, actions] = useUndo(initialState); 124 | ``` 125 | 126 | #### state 127 | 128 | ##### Type: `Object` 129 | 130 | | Key | Type | Description | 131 | | ------- | :-----: | ------------------ | 132 | | past | `Array` | The undo stack. | 133 | | present | `Any` | The present state. | 134 | | future | `Array` | The redo stack. | 135 | 136 | #### actions 137 | 138 | ##### Type: `Object` 139 | 140 | | Key | Type | Description | 141 | | ------- | :--------: | ------------------------------------------------------------------------------------------ | 142 | | set | `function` | Assign a new value to `present`. | 143 | | reset | `function` | Clear `past` array and `future` array. Assign a new value to `present`. | 144 | | undo | `function` | See [handling-undo](https://redux.js.org/recipes/implementing-undo-history#handling-undo). | 145 | | redo | `function` | See [handling-redo](https://redux.js.org/recipes/implementing-undo-history#handling-redo). | 146 | | canUndo | `boolean` | Check whether `state.undo.length` is `0`. | 147 | | canRedo | `boolean` | Check whether `state.redo.length` is `0`. | 148 | 149 | ## How does it work? 150 | 151 | Refer to [_Redux Implementing Undo History_](hhttps://redux.js.org/recipes/implementing-undo-history), `use-undo` implements the same concect with [`useReducer`](https://reactjs.org/docs/hooks-reference.html#usereducer). 152 | The state structure looks like: 153 | 154 | ```js 155 | { 156 | past: Array, 157 | present: , 158 | future: Array 159 | } 160 | ``` 161 | 162 | It stores all states we need. To operate on this state, there are three functions in [`actions`](#actions) (`set`, `undo` and `redo`) that dispatch defined types and necessary value. 163 | 164 | ## Related repo 165 | 166 | - [omnidan/redux-undo](https://github.com/omnidan/redux-undo) 167 | 168 | ## License 169 | 170 | MIT © [homerchen19](https://github.com/homerchen19) 171 | -------------------------------------------------------------------------------- /__test__/index.spec.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render, cleanup, fireEvent } from '@testing-library/react'; 3 | 4 | import useUndo from '../index'; 5 | 6 | const UndoComponent = ({ disabled }: { disabled: boolean }) => { 7 | const [ 8 | countState, 9 | { 10 | set: setCount, 11 | reset: resetCount, 12 | undo: undoCount, 13 | redo: redoCount, 14 | canUndo, 15 | canRedo, 16 | }, 17 | ] = useUndo(0); 18 | const { present: presentCount } = countState; 19 | const doubleRedoCount = () => { 20 | redoCount(); 21 | redoCount(); 22 | }; 23 | const doubleUndoCount = () => { 24 | undoCount(); 25 | undoCount(); 26 | }; 27 | 28 | return ( 29 |
30 |

count: {presentCount}

31 | 38 | 45 | 52 | 60 | 68 | 76 | 84 | 87 |
88 | ); 89 | }; 90 | 91 | const setup = (defaultDisabled = true) => { 92 | const { getByTestId } = render(); 93 | 94 | const count = getByTestId('count'); 95 | const unchangeButton = getByTestId('unchange') as HTMLButtonElement; 96 | const incrementButton = getByTestId('increment') as HTMLButtonElement; 97 | const decrementButton = getByTestId('decrement') as HTMLButtonElement; 98 | const undoButton = getByTestId('undo') as HTMLButtonElement; 99 | const redoButton = getByTestId('redo') as HTMLButtonElement; 100 | const doubleUndoButton = getByTestId('double undo') as HTMLButtonElement; 101 | const doubleRedoButton = getByTestId('double redo') as HTMLButtonElement; 102 | const resetButton = getByTestId('reset') as HTMLButtonElement; 103 | 104 | return { 105 | count, 106 | unchangeButton, 107 | incrementButton, 108 | decrementButton, 109 | undoButton, 110 | redoButton, 111 | doubleUndoButton, 112 | doubleRedoButton, 113 | resetButton, 114 | }; 115 | }; 116 | 117 | describe('use-undo', () => { 118 | afterEach(cleanup); 119 | 120 | it('should exist', () => { 121 | expect(useUndo).toBeDefined(); 122 | }); 123 | 124 | it('should work', () => { 125 | const { 126 | count, 127 | unchangeButton, 128 | incrementButton, 129 | decrementButton, 130 | undoButton, 131 | redoButton, 132 | resetButton, 133 | } = setup(); 134 | 135 | expect(count.textContent).toBe('count: 0'); 136 | expect(undoButton.disabled).toBe(true); 137 | expect(redoButton.disabled).toBe(true); 138 | 139 | fireEvent.click(incrementButton); 140 | fireEvent.click(incrementButton); 141 | fireEvent.click(incrementButton); 142 | 143 | expect(count.textContent).toBe('count: 3'); 144 | expect(undoButton.disabled).toBe(false); 145 | expect(redoButton.disabled).toBe(true); 146 | 147 | fireEvent.click(unchangeButton); 148 | 149 | expect(count.textContent).toBe('count: 3'); 150 | expect(undoButton.disabled).toBe(false); 151 | expect(redoButton.disabled).toBe(true); 152 | 153 | fireEvent.click(decrementButton); 154 | fireEvent.click(decrementButton); 155 | fireEvent.click(decrementButton); 156 | 157 | expect(count.textContent).toBe('count: 0'); 158 | expect(undoButton.disabled).toBe(false); 159 | expect(redoButton.disabled).toBe(true); 160 | 161 | fireEvent.click(undoButton); 162 | fireEvent.click(undoButton); 163 | fireEvent.click(undoButton); 164 | 165 | expect(count.textContent).toBe('count: 3'); 166 | expect(undoButton.disabled).toBe(false); 167 | expect(redoButton.disabled).toBe(false); 168 | 169 | fireEvent.click(redoButton); 170 | fireEvent.click(redoButton); 171 | fireEvent.click(redoButton); 172 | 173 | expect(count.textContent).toBe('count: 0'); 174 | expect(undoButton.disabled).toBe(false); 175 | expect(redoButton.disabled).toBe(true); 176 | 177 | fireEvent.click(undoButton); 178 | fireEvent.click(undoButton); 179 | fireEvent.click(undoButton); 180 | fireEvent.click(undoButton); 181 | fireEvent.click(undoButton); 182 | fireEvent.click(undoButton); 183 | 184 | expect(count.textContent).toBe('count: 0'); 185 | expect(undoButton.disabled).toBe(true); 186 | expect(redoButton.disabled).toBe(false); 187 | 188 | fireEvent.click(incrementButton); 189 | fireEvent.click(incrementButton); 190 | fireEvent.click(resetButton); 191 | 192 | expect(count.textContent).toBe('count: 0'); 193 | expect(undoButton.disabled).toBe(true); 194 | expect(redoButton.disabled).toBe(true); 195 | }); 196 | 197 | it('present count should not be changed when canUndo or canRedo is false', () => { 198 | const { count, undoButton, redoButton } = setup(false); 199 | 200 | expect(count.textContent).toBe('count: 0'); 201 | expect(undoButton.disabled).toBe(false); 202 | expect(redoButton.disabled).toBe(false); 203 | 204 | fireEvent.click(undoButton); 205 | 206 | expect(count.textContent).toBe('count: 0'); 207 | 208 | fireEvent.click(redoButton); 209 | 210 | expect(count.textContent).toBe('count: 0'); 211 | }); 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 | }); 242 | }); 243 | -------------------------------------------------------------------------------- /__test__/checkpoint.spec.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render, cleanup, fireEvent } from '@testing-library/react'; 3 | 4 | import useUndo from '../index'; 5 | 6 | const UndoComponent = ({ 7 | disabled, 8 | manualCheckpoints, 9 | }: { 10 | disabled: boolean; 11 | manualCheckpoints: boolean; 12 | }) => { 13 | const [ 14 | countState, 15 | { 16 | set: setCount, 17 | reset: resetCount, 18 | undo: undoCount, 19 | redo: redoCount, 20 | canUndo, 21 | canRedo, 22 | }, 23 | ] = useUndo(0, { useCheckpoints: manualCheckpoints }); 24 | const { present: presentCount } = countState; 25 | const doubleRedoCount = () => { 26 | redoCount(); 27 | redoCount(); 28 | }; 29 | const doubleUndoCount = () => { 30 | undoCount(); 31 | undoCount(); 32 | }; 33 | 34 | return ( 35 |
36 |

count: {presentCount}

37 | 44 | 51 | 58 | 65 | 72 | 80 | 88 | 96 | 104 | 107 |
108 | ); 109 | }; 110 | 111 | type SetupArg = { 112 | defaultDisabled: boolean; 113 | manualCheckpoints: boolean; 114 | }; 115 | 116 | const setup = (opts: Partial = {}) => { 117 | const { defaultDisabled, manualCheckpoints }: Partial = { 118 | defaultDisabled: true, 119 | manualCheckpoints: false, 120 | ...opts, 121 | }; 122 | const { getByTestId } = render( 123 | 127 | ); 128 | 129 | const count = getByTestId('count'); 130 | const unchangeButton = getByTestId('unchange') as HTMLButtonElement; 131 | 132 | const withcheckincrementButton = getByTestId( 133 | 'withcheck-increment' 134 | ) as HTMLButtonElement; 135 | const withcheckdecrementButton = getByTestId( 136 | 'withcheck-decrement' 137 | ) as HTMLButtonElement; 138 | const nocheckincrementButton = getByTestId( 139 | 'nocheckincrement' 140 | ) as HTMLButtonElement; 141 | const nocheckdecrementButton = getByTestId( 142 | 'nocheckdecrement' 143 | ) as HTMLButtonElement; 144 | const undoButton = getByTestId('undo') as HTMLButtonElement; 145 | const redoButton = getByTestId('redo') as HTMLButtonElement; 146 | const doubleUndoButton = getByTestId('double undo') as HTMLButtonElement; 147 | const doubleRedoButton = getByTestId('double redo') as HTMLButtonElement; 148 | const resetButton = getByTestId('reset') as HTMLButtonElement; 149 | 150 | return { 151 | count, 152 | unchangeButton, 153 | nocheckincrementButton, 154 | nocheckdecrementButton, 155 | withcheckincrementButton, 156 | withcheckdecrementButton, 157 | undoButton, 158 | redoButton, 159 | doubleUndoButton, 160 | doubleRedoButton, 161 | resetButton, 162 | }; 163 | }; 164 | 165 | describe('use-undo', () => { 166 | afterEach(cleanup); 167 | 168 | it('should exist', () => { 169 | expect(useUndo).toBeDefined(); 170 | }); 171 | 172 | it('should work with checkpoints', () => { 173 | const { 174 | count, 175 | unchangeButton, 176 | withcheckincrementButton, 177 | withcheckdecrementButton, 178 | undoButton, 179 | redoButton, 180 | resetButton, 181 | } = setup({ manualCheckpoints: true }); 182 | 183 | // with checkpoints test cases 184 | expect(count.textContent).toBe('count: 0'); 185 | expect(undoButton.disabled).toBe(true); 186 | expect(redoButton.disabled).toBe(true); 187 | 188 | fireEvent.click(withcheckincrementButton); 189 | fireEvent.click(withcheckincrementButton); 190 | fireEvent.click(withcheckincrementButton); 191 | 192 | expect(count.textContent).toBe('count: 3'); 193 | expect(undoButton.disabled).toBe(false); 194 | expect(redoButton.disabled).toBe(true); 195 | 196 | fireEvent.click(unchangeButton); 197 | 198 | expect(count.textContent).toBe('count: 3'); 199 | expect(undoButton.disabled).toBe(false); 200 | expect(redoButton.disabled).toBe(true); 201 | 202 | fireEvent.click(withcheckdecrementButton); 203 | fireEvent.click(withcheckdecrementButton); 204 | fireEvent.click(withcheckdecrementButton); 205 | 206 | expect(count.textContent).toBe('count: 0'); 207 | expect(undoButton.disabled).toBe(false); 208 | expect(redoButton.disabled).toBe(true); 209 | 210 | fireEvent.click(undoButton); 211 | fireEvent.click(undoButton); 212 | fireEvent.click(undoButton); 213 | 214 | expect(count.textContent).toBe('count: 3'); 215 | expect(undoButton.disabled).toBe(false); 216 | expect(redoButton.disabled).toBe(false); 217 | 218 | fireEvent.click(redoButton); 219 | fireEvent.click(redoButton); 220 | fireEvent.click(redoButton); 221 | 222 | expect(count.textContent).toBe('count: 0'); 223 | expect(undoButton.disabled).toBe(false); 224 | expect(redoButton.disabled).toBe(true); 225 | 226 | fireEvent.click(undoButton); 227 | fireEvent.click(undoButton); 228 | fireEvent.click(undoButton); 229 | fireEvent.click(undoButton); 230 | fireEvent.click(undoButton); 231 | fireEvent.click(undoButton); 232 | 233 | expect(count.textContent).toBe('count: 0'); 234 | expect(undoButton.disabled).toBe(true); 235 | expect(redoButton.disabled).toBe(false); 236 | 237 | fireEvent.click(withcheckincrementButton); 238 | fireEvent.click(withcheckincrementButton); 239 | fireEvent.click(resetButton); 240 | 241 | expect(count.textContent).toBe('count: 0'); 242 | expect(undoButton.disabled).toBe(true); 243 | expect(redoButton.disabled).toBe(true); 244 | }); 245 | 246 | it('should work without checkpoints', () => { 247 | const { 248 | count, 249 | unchangeButton, 250 | nocheckincrementButton, 251 | nocheckdecrementButton, 252 | undoButton, 253 | redoButton, 254 | resetButton, 255 | } = setup({ manualCheckpoints: true }); 256 | 257 | // without checkpoints test cases 258 | expect(count.textContent).toBe('count: 0'); 259 | expect(undoButton.disabled).toBe(true); 260 | expect(redoButton.disabled).toBe(true); 261 | 262 | fireEvent.click(nocheckincrementButton); 263 | fireEvent.click(nocheckincrementButton); 264 | fireEvent.click(nocheckincrementButton); 265 | 266 | expect(count.textContent).toBe('count: 3'); 267 | expect(undoButton.disabled).toBe(true); 268 | expect(redoButton.disabled).toBe(true); 269 | 270 | fireEvent.click(unchangeButton); 271 | 272 | expect(count.textContent).toBe('count: 3'); 273 | expect(undoButton.disabled).toBe(true); 274 | expect(redoButton.disabled).toBe(true); 275 | 276 | fireEvent.click(nocheckdecrementButton); 277 | fireEvent.click(nocheckdecrementButton); 278 | fireEvent.click(nocheckdecrementButton); 279 | 280 | expect(count.textContent).toBe('count: 0'); 281 | expect(undoButton.disabled).toBe(true); 282 | expect(redoButton.disabled).toBe(true); 283 | 284 | fireEvent.click(nocheckincrementButton); 285 | fireEvent.click(nocheckincrementButton); 286 | fireEvent.click(resetButton); 287 | 288 | expect(count.textContent).toBe('count: 0'); 289 | expect(undoButton.disabled).toBe(true); 290 | expect(redoButton.disabled).toBe(true); 291 | }); 292 | 293 | it('present count should not be changed when canUndo or canRedo is false', () => { 294 | const { count, undoButton, redoButton } = setup({ defaultDisabled: false }); 295 | 296 | expect(count.textContent).toBe('count: 0'); 297 | expect(undoButton.disabled).toBe(false); 298 | expect(redoButton.disabled).toBe(false); 299 | 300 | fireEvent.click(undoButton); 301 | 302 | expect(count.textContent).toBe('count: 0'); 303 | 304 | fireEvent.click(redoButton); 305 | 306 | expect(count.textContent).toBe('count: 0'); 307 | }); 308 | 309 | describe('when it can undo once', () => { 310 | describe('when calling undo multiple times in succession', () => { 311 | it('should return to the initial state', () => { 312 | const { count, doubleUndoButton, withcheckincrementButton } = setup(); 313 | 314 | fireEvent.click(withcheckincrementButton); 315 | 316 | fireEvent.click(doubleUndoButton); 317 | 318 | expect(count.textContent).toBe('count: 0'); 319 | }); 320 | }); 321 | }); 322 | 323 | describe('when it can redo once', () => { 324 | describe('when calling redo multiple times in succession', () => { 325 | it('should return to the last state', () => { 326 | const { 327 | count, 328 | undoButton, 329 | doubleRedoButton, 330 | withcheckincrementButton, 331 | } = setup(); 332 | 333 | fireEvent.click(withcheckincrementButton); 334 | fireEvent.click(undoButton); 335 | 336 | fireEvent.click(doubleRedoButton); 337 | 338 | expect(count.textContent).toBe('count: 1'); 339 | }); 340 | }); 341 | }); 342 | }); 343 | --------------------------------------------------------------------------------