|
| 1 | +import { renderHook, act } from '@testing-library/react'; |
| 2 | +import useDeckState, { DeckView } from './use-deck-state'; |
| 3 | + |
| 4 | +describe('useDeckState', () => { |
| 5 | + const initialState: DeckView = { |
| 6 | + slideIndex: 1, |
| 7 | + stepIndex: 1 |
| 8 | + }; |
| 9 | + |
| 10 | + /** |
| 11 | + * The INITIALIZE_TO should set the active and pending views |
| 12 | + * to the values provided in the payload. |
| 13 | + */ |
| 14 | + it('should handle INITIALIZE_TO action', () => { |
| 15 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 16 | + |
| 17 | + act(() => { |
| 18 | + result.current.initializeTo({ slideIndex: 2, stepIndex: 2 }); |
| 19 | + }); |
| 20 | + |
| 21 | + expect(result.current.activeView).toEqual({ slideIndex: 2, stepIndex: 2 }); |
| 22 | + expect(result.current.pendingView).toEqual({ slideIndex: 2, stepIndex: 2 }); |
| 23 | + expect(result.current.initialized).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + /** |
| 27 | + * The SKIP_TO action should set the pending view slide index to |
| 28 | + * the slide index provided by the payload and set the navigation |
| 29 | + * direction based on a delta of the previous and pending slides. |
| 30 | + */ |
| 31 | + it('should handle SKIP_TO action', () => { |
| 32 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 33 | + |
| 34 | + act(() => { |
| 35 | + result.current.skipTo({ slideIndex: 3 }); |
| 36 | + }); |
| 37 | + |
| 38 | + expect(result.current.navigationDirection).toBe(1); |
| 39 | + expect(result.current.pendingView.slideIndex).toBe(3); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle SKIP_TO action in reverse', () => { |
| 43 | + const { result } = renderHook(() => |
| 44 | + useDeckState({ slideIndex: 5, stepIndex: 0, slideId: 0 }) |
| 45 | + ); |
| 46 | + |
| 47 | + act(() => { |
| 48 | + result.current.skipTo({ slideIndex: 3 }); |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result.current.navigationDirection).toBe(-1); |
| 52 | + expect(result.current.pendingView.slideIndex).toBe(3); |
| 53 | + }); |
| 54 | + |
| 55 | + /** |
| 56 | + * The STEP_FORWARD action should increment the pending slide index by 1 |
| 57 | + * and have a positive navigation direction. |
| 58 | + */ |
| 59 | + it('should handle STEP_FORWARD action', () => { |
| 60 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 61 | + |
| 62 | + act(() => { |
| 63 | + result.current.stepForward(); |
| 64 | + }); |
| 65 | + |
| 66 | + expect(result.current.pendingView.stepIndex).toBe( |
| 67 | + initialState.stepIndex + 1 |
| 68 | + ); |
| 69 | + expect(result.current.navigationDirection).toBe(1); |
| 70 | + }); |
| 71 | + |
| 72 | + /** |
| 73 | + * The STEP_FORWARD action should decrement the pending slide index by 1 |
| 74 | + * and have a negative navigation direction. |
| 75 | + */ |
| 76 | + it('should handle STEP_BACKWARD action', () => { |
| 77 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 78 | + |
| 79 | + act(() => { |
| 80 | + result.current.stepBackward(); |
| 81 | + }); |
| 82 | + |
| 83 | + expect(result.current.pendingView.stepIndex).toBe( |
| 84 | + initialState.stepIndex - 1 |
| 85 | + ); |
| 86 | + expect(result.current.navigationDirection).toBe(-1); |
| 87 | + }); |
| 88 | + |
| 89 | + /** |
| 90 | + * The ADVANCE_SLIDE action should increment the pending slide index by 1, |
| 91 | + * reset the step index, and have a positive navigation direction. |
| 92 | + */ |
| 93 | + it('should handle ADVANCE_SLIDE action', () => { |
| 94 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 95 | + |
| 96 | + act(() => { |
| 97 | + result.current.advanceSlide(); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(result.current.pendingView.slideIndex).toBe( |
| 101 | + initialState.slideIndex + 1 |
| 102 | + ); |
| 103 | + expect(result.current.pendingView.stepIndex).toBe(0); |
| 104 | + expect(result.current.navigationDirection).toBe(1); |
| 105 | + }); |
| 106 | + |
| 107 | + /** |
| 108 | + * The REGRESS_SLIDE action should decrement the pending slide index by 1, |
| 109 | + * reset the step index, and have a negative navigation direction. |
| 110 | + */ |
| 111 | + it('should handle REGRESS_SLIDE action', () => { |
| 112 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 113 | + |
| 114 | + act(() => { |
| 115 | + result.current.regressSlide({ stepIndex: 0 }); |
| 116 | + }); |
| 117 | + |
| 118 | + expect(result.current.pendingView.slideIndex).toBe( |
| 119 | + initialState.slideIndex - 1 |
| 120 | + ); |
| 121 | + expect(result.current.pendingView.stepIndex).toBe(0); |
| 122 | + expect(result.current.navigationDirection).toBe(-1); |
| 123 | + }); |
| 124 | + |
| 125 | + /** |
| 126 | + * The COMMIT_TRANSITION action should set the active slide view |
| 127 | + * and pending slide view to the payload values. |
| 128 | + */ |
| 129 | + it('should handle COMMIT_TRANSITION action', () => { |
| 130 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 131 | + |
| 132 | + act(() => { |
| 133 | + result.current.commitTransition({ slideIndex: 2, stepIndex: 2 }); |
| 134 | + }); |
| 135 | + |
| 136 | + expect(result.current.activeView).toEqual({ slideIndex: 2, stepIndex: 2 }); |
| 137 | + expect(result.current.pendingView).toEqual({ slideIndex: 2, stepIndex: 2 }); |
| 138 | + }); |
| 139 | + |
| 140 | + /** |
| 141 | + * The CANCEL_TRANSITION action should cancel the slide transition |
| 142 | + * by reverting the pending view values to what the current active slide values are. |
| 143 | + */ |
| 144 | + it('should handle CANCEL_TRANSITION action', () => { |
| 145 | + const { result } = renderHook(() => useDeckState(initialState)); |
| 146 | + |
| 147 | + act(() => { |
| 148 | + result.current.cancelTransition(); |
| 149 | + }); |
| 150 | + |
| 151 | + expect(result.current.pendingView).toEqual(result.current.activeView); |
| 152 | + }); |
| 153 | +}); |
0 commit comments