|
| 1 | +/* eslint-env jest */ |
| 2 | +import projectStateReducer from '../../../src/reducers/project-state'; |
| 3 | +import { |
| 4 | + LoadingState, |
| 5 | + onCreated, |
| 6 | + onError, |
| 7 | + onFetchedProjectData, |
| 8 | + onLoadedProject, |
| 9 | + onProjectUploadStarted, |
| 10 | + onUpdated, |
| 11 | + requestNewProject, |
| 12 | + saveProject, |
| 13 | + setProjectId |
| 14 | +} from '../../../src/reducers/project-state'; |
| 15 | + |
| 16 | +test('initialState', () => { |
| 17 | + let defaultState; |
| 18 | + /* projectStateReducer(state, action) */ |
| 19 | + expect(projectStateReducer(defaultState, {type: 'anything'})).toBeDefined(); |
| 20 | + expect(projectStateReducer(defaultState, {type: 'anything'}).errStr).toBe(null); |
| 21 | + expect(projectStateReducer(defaultState, {type: 'anything'}).projectData).toBe(null); |
| 22 | + expect(projectStateReducer(defaultState, {type: 'anything'}).projectId).toBe(null); |
| 23 | + expect(projectStateReducer(defaultState, {type: 'anything'}).loadingState).toBe(LoadingState.NOT_LOADED); |
| 24 | +}); |
| 25 | + |
| 26 | +test('onCreated with projectId type string', () => { |
| 27 | + const initialState = { |
| 28 | + projectId: null, |
| 29 | + loadingState: LoadingState.CREATING_NEW |
| 30 | + }; |
| 31 | + const action = onCreated('100'); |
| 32 | + const resultState = projectStateReducer(initialState, action); |
| 33 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 34 | + expect(resultState.projectId).toBe('100'); |
| 35 | +}); |
| 36 | + |
| 37 | +test('onCreated with projectId type number', () => { |
| 38 | + const initialState = { |
| 39 | + projectId: null, |
| 40 | + loadingState: LoadingState.CREATING_NEW |
| 41 | + }; |
| 42 | + const action = onCreated(100); |
| 43 | + const resultState = projectStateReducer(initialState, action); |
| 44 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 45 | + expect(resultState.projectId).toBe(100); |
| 46 | +}); |
| 47 | + |
| 48 | +test('onFetchedProjectData with id', () => { |
| 49 | + const initialState = { |
| 50 | + projectData: null, |
| 51 | + loadingState: LoadingState.FETCHING_WITH_ID |
| 52 | + }; |
| 53 | + const action = onFetchedProjectData('1010101', initialState.loadingState); |
| 54 | + const resultState = projectStateReducer(initialState, action); |
| 55 | + expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_WITH_ID); |
| 56 | + expect(resultState.projectData).toBe('1010101'); |
| 57 | +}); |
| 58 | + |
| 59 | +test('onFetchedProjectData new', () => { |
| 60 | + const initialState = { |
| 61 | + projectData: null, |
| 62 | + loadingState: LoadingState.FETCHING_NEW_DEFAULT |
| 63 | + }; |
| 64 | + const action = onFetchedProjectData('1010101', initialState.loadingState); |
| 65 | + const resultState = projectStateReducer(initialState, action); |
| 66 | + expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_NEW_DEFAULT); |
| 67 | + expect(resultState.projectData).toBe('1010101'); |
| 68 | +}); |
| 69 | + |
| 70 | +test('onFetchedProjectData new, to save', () => { |
| 71 | + const initialState = { |
| 72 | + projectData: null, |
| 73 | + loadingState: LoadingState.FETCHING_NEW_DEFAULT_TO_SAVE |
| 74 | + }; |
| 75 | + const action = onFetchedProjectData('1010101', initialState.loadingState); |
| 76 | + const resultState = projectStateReducer(initialState, action); |
| 77 | + expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_NEW_DEFAULT_TO_SAVE); |
| 78 | + expect(resultState.projectData).toBe('1010101'); |
| 79 | +}); |
| 80 | + |
| 81 | +test('onLoadedProject upload', () => { |
| 82 | + const initialState = { |
| 83 | + loadingState: LoadingState.LOADING_VM_FILE_UPLOAD |
| 84 | + }; |
| 85 | + const action = onLoadedProject(initialState.loadingState); |
| 86 | + const resultState = projectStateReducer(initialState, action); |
| 87 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID); |
| 88 | +}); |
| 89 | + |
| 90 | +test('onLoadedProject with id', () => { |
| 91 | + const initialState = { |
| 92 | + loadingState: LoadingState.LOADING_VM_WITH_ID |
| 93 | + }; |
| 94 | + const action = onLoadedProject(initialState.loadingState); |
| 95 | + const resultState = projectStateReducer(initialState, action); |
| 96 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 97 | +}); |
| 98 | + |
| 99 | +test('onLoadedProject new', () => { |
| 100 | + const initialState = { |
| 101 | + loadingState: LoadingState.LOADING_VM_NEW_DEFAULT |
| 102 | + }; |
| 103 | + const action = onLoadedProject(initialState.loadingState); |
| 104 | + const resultState = projectStateReducer(initialState, action); |
| 105 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID); |
| 106 | +}); |
| 107 | + |
| 108 | +test('onLoadedProject new, to save', () => { |
| 109 | + const initialState = { |
| 110 | + loadingState: LoadingState.LOADING_VM_NEW_DEFAULT_TO_SAVE |
| 111 | + }; |
| 112 | + const action = onLoadedProject(initialState.loadingState); |
| 113 | + const resultState = projectStateReducer(initialState, action); |
| 114 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 115 | +}); |
| 116 | + |
| 117 | +test('onUpdated with id', () => { |
| 118 | + const initialState = { |
| 119 | + loadingState: LoadingState.SAVING_WITH_ID |
| 120 | + }; |
| 121 | + const action = onUpdated(initialState.loadingState); |
| 122 | + const resultState = projectStateReducer(initialState, action); |
| 123 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 124 | +}); |
| 125 | + |
| 126 | +test('onUpdated with id, before new', () => { |
| 127 | + const initialState = { |
| 128 | + loadingState: LoadingState.SAVING_WITH_ID_BEFORE_NEW |
| 129 | + }; |
| 130 | + const action = onUpdated(initialState.loadingState); |
| 131 | + const resultState = projectStateReducer(initialState, action); |
| 132 | + expect(resultState.loadingState).toBe(LoadingState.FETCHING_NEW_DEFAULT_TO_SAVE); |
| 133 | +}); |
| 134 | + |
| 135 | +test('setProjectId with same id as before, should not fetch', () => { |
| 136 | + const initialState = { |
| 137 | + projectId: 100, |
| 138 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 139 | + }; |
| 140 | + const action = setProjectId(100); |
| 141 | + const resultState = projectStateReducer(initialState, action); |
| 142 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 143 | + expect(resultState.projectId).toBe(100); |
| 144 | +}); |
| 145 | + |
| 146 | +test('setProjectId with different id as before, should fetch', () => { |
| 147 | + const initialState = { |
| 148 | + projectId: 99, |
| 149 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 150 | + }; |
| 151 | + const action = setProjectId(100); |
| 152 | + const resultState = projectStateReducer(initialState, action); |
| 153 | + expect(resultState.loadingState).toBe(LoadingState.FETCHING_WITH_ID); |
| 154 | + expect(resultState.projectId).toBe(100); |
| 155 | +}); |
| 156 | + |
| 157 | +test('setProjectId with same id as before, but not same type, should not fetch', () => { |
| 158 | + const initialState = { |
| 159 | + projectId: '100', |
| 160 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 161 | + }; |
| 162 | + const action = setProjectId(100); |
| 163 | + const resultState = projectStateReducer(initialState, action); |
| 164 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 165 | + expect(resultState.projectId).toBe('100'); |
| 166 | +}); |
| 167 | + |
| 168 | +test('setProjectId provided a null id should not fetch', () => { |
| 169 | + const initialState = { |
| 170 | + projectId: '100', |
| 171 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 172 | + }; |
| 173 | + const action = setProjectId(null); |
| 174 | + const resultState = projectStateReducer(initialState, action); |
| 175 | + expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID); |
| 176 | + expect(resultState.projectId).toBe('100'); |
| 177 | +}); |
| 178 | + |
| 179 | +test('requestNewProject when can\'t save', () => { |
| 180 | + const initialState = { |
| 181 | + loadingState: LoadingState.SHOWING_WITHOUT_ID |
| 182 | + }; |
| 183 | + const action = requestNewProject(false); |
| 184 | + const resultState = projectStateReducer(initialState, action); |
| 185 | + expect(resultState.loadingState).toBe(LoadingState.FETCHING_NEW_DEFAULT); |
| 186 | +}); |
| 187 | + |
| 188 | +test('requestNewProject when can save', () => { |
| 189 | + const initialState = { |
| 190 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 191 | + }; |
| 192 | + const action = requestNewProject(true); |
| 193 | + const resultState = projectStateReducer(initialState, action); |
| 194 | + expect(resultState.loadingState).toBe(LoadingState.SAVING_WITH_ID_BEFORE_NEW); |
| 195 | +}); |
| 196 | + |
| 197 | +test('onProjectUploadStarted initial load', () => { |
| 198 | + const initialState = { |
| 199 | + loadingState: LoadingState.NOT_LOADED |
| 200 | + }; |
| 201 | + const action = onProjectUploadStarted(); |
| 202 | + const resultState = projectStateReducer(initialState, action); |
| 203 | + expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_FILE_UPLOAD); |
| 204 | +}); |
| 205 | + |
| 206 | +test('onProjectUploadStarted when showing project with id', () => { |
| 207 | + const initialState = { |
| 208 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 209 | + }; |
| 210 | + const action = onProjectUploadStarted(); |
| 211 | + const resultState = projectStateReducer(initialState, action); |
| 212 | + expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_FILE_UPLOAD); |
| 213 | +}); |
| 214 | + |
| 215 | +test('onProjectUploadStarted when showing project without id', () => { |
| 216 | + const initialState = { |
| 217 | + loadingState: LoadingState.SHOWING_WITHOUT_ID |
| 218 | + }; |
| 219 | + const action = onProjectUploadStarted(); |
| 220 | + const resultState = projectStateReducer(initialState, action); |
| 221 | + expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_FILE_UPLOAD); |
| 222 | +}); |
| 223 | + |
| 224 | +test('saveProject', () => { |
| 225 | + const initialState = { |
| 226 | + loadingState: LoadingState.SHOWING_WITH_ID |
| 227 | + }; |
| 228 | + const action = saveProject(); |
| 229 | + const resultState = projectStateReducer(initialState, action); |
| 230 | + expect(resultState.loadingState).toBe(LoadingState.SAVING_WITH_ID); |
| 231 | +}); |
| 232 | + |
| 233 | +test('onError from unloaded state', () => { |
| 234 | + const initialState = { |
| 235 | + errStr: null, |
| 236 | + loadingState: LoadingState.NOT_LOADED |
| 237 | + }; |
| 238 | + const action = onError('Error string'); |
| 239 | + const resultState = projectStateReducer(initialState, action); |
| 240 | + expect(resultState.loadingState).toBe(LoadingState.ERROR); |
| 241 | + expect(resultState.errStr).toBe('Error string'); |
| 242 | +}); |
| 243 | + |
| 244 | +test('onError from showing project', () => { |
| 245 | + const initialState = { |
| 246 | + errStr: null, |
| 247 | + loadingState: LoadingState.FETCHING_WITH_ID |
| 248 | + }; |
| 249 | + const action = onError('Error string'); |
| 250 | + const resultState = projectStateReducer(initialState, action); |
| 251 | + expect(resultState.loadingState).toBe(LoadingState.ERROR); |
| 252 | + expect(resultState.errStr).toBe('Error string'); |
| 253 | +}); |
0 commit comments