|
| 1 | +import { initializeState, markError, markLoading, markSuccess } from '../../lib/utils/ProgressStateUtils'; |
| 2 | +import { ProgressState } from '../../lib/types/ProgressState'; |
| 3 | + |
| 4 | +describe('ProgressStateUtils', () => { |
| 5 | + describe('initializeState', () => { |
| 6 | + it('should initialize the state correctly', () => { |
| 7 | + const expectedState: ProgressState = { |
| 8 | + isLoading: false, |
| 9 | + isSuccess: false, |
| 10 | + isError: false, |
| 11 | + isComplete: false, |
| 12 | + message: '', |
| 13 | + }; |
| 14 | + |
| 15 | + const result = initializeState(); |
| 16 | + |
| 17 | + expect(result).toEqual(expectedState); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('markLoading', () => { |
| 22 | + it('should update the state to loading', () => { |
| 23 | + const initialState: ProgressState = { |
| 24 | + isLoading: false, |
| 25 | + isSuccess: false, |
| 26 | + isError: false, |
| 27 | + isComplete: false, |
| 28 | + message: '', |
| 29 | + }; |
| 30 | + |
| 31 | + const expectedState: ProgressState = { |
| 32 | + ...initialState, |
| 33 | + isLoading: true, |
| 34 | + }; |
| 35 | + |
| 36 | + const result = markLoading(initialState); |
| 37 | + |
| 38 | + expect(result).toEqual(expectedState); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('markSuccess', () => { |
| 43 | + it('should update the state to success with a message', () => { |
| 44 | + const initialState: ProgressState = { |
| 45 | + isLoading: false, |
| 46 | + isSuccess: false, |
| 47 | + isError: false, |
| 48 | + isComplete: false, |
| 49 | + message: '', |
| 50 | + }; |
| 51 | + |
| 52 | + const message = 'Operation successful'; |
| 53 | + const expectedState: ProgressState = { |
| 54 | + ...initialState, |
| 55 | + isSuccess: true, |
| 56 | + isComplete: true, |
| 57 | + message, |
| 58 | + }; |
| 59 | + |
| 60 | + const result = markSuccess(initialState, message); |
| 61 | + |
| 62 | + expect(result).toEqual(expectedState); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should update the state to success without a message', () => { |
| 66 | + const initialState: ProgressState = { |
| 67 | + isLoading: false, |
| 68 | + isSuccess: false, |
| 69 | + isError: false, |
| 70 | + isComplete: false, |
| 71 | + message: '', |
| 72 | + }; |
| 73 | + |
| 74 | + const expectedState: ProgressState = { |
| 75 | + ...initialState, |
| 76 | + isSuccess: true, |
| 77 | + isComplete: true, |
| 78 | + message: '', |
| 79 | + }; |
| 80 | + |
| 81 | + const result = markSuccess(initialState); |
| 82 | + |
| 83 | + expect(result).toEqual(expectedState); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('markError', () => { |
| 88 | + it('should update the state to error with a message', () => { |
| 89 | + const initialState: ProgressState = { |
| 90 | + isLoading: false, |
| 91 | + isSuccess: false, |
| 92 | + isError: false, |
| 93 | + isComplete: false, |
| 94 | + message: '', |
| 95 | + }; |
| 96 | + |
| 97 | + const message = 'Operation failed'; |
| 98 | + const expectedState: ProgressState = { |
| 99 | + ...initialState, |
| 100 | + isError: true, |
| 101 | + isComplete: true, |
| 102 | + message, |
| 103 | + }; |
| 104 | + |
| 105 | + const result = markError(initialState, message); |
| 106 | + |
| 107 | + expect(result).toEqual(expectedState); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should update the state to error without a message', () => { |
| 111 | + const initialState: ProgressState = { |
| 112 | + isLoading: false, |
| 113 | + isSuccess: false, |
| 114 | + isError: false, |
| 115 | + isComplete: false, |
| 116 | + message: '', |
| 117 | + }; |
| 118 | + |
| 119 | + const expectedState: ProgressState = { |
| 120 | + ...initialState, |
| 121 | + isError: true, |
| 122 | + isComplete: true, |
| 123 | + message: '', |
| 124 | + }; |
| 125 | + |
| 126 | + const result = markError(initialState); |
| 127 | + |
| 128 | + expect(result).toEqual(expectedState); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments