project-state-reducer.test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* eslint-env jest */
  2. import projectStateReducer from '../../../src/reducers/project-state';
  3. import {
  4. LoadingState,
  5. autoUpdateProject,
  6. doneCreatingProject,
  7. doneUpdatingProject,
  8. manualUpdateProject,
  9. onFetchedProjectData,
  10. onLoadedProject,
  11. projectError,
  12. remixProject,
  13. requestNewProject,
  14. requestProjectUpload,
  15. saveProjectAsCopy,
  16. setProjectId
  17. } from '../../../src/reducers/project-state';
  18. test('initialState', () => {
  19. let defaultState;
  20. /* projectStateReducer(state, action) */
  21. expect(projectStateReducer(defaultState, {type: 'anything'})).toBeDefined();
  22. expect(projectStateReducer(defaultState, {type: 'anything'}).error).toBe(null);
  23. expect(projectStateReducer(defaultState, {type: 'anything'}).projectData).toBe(null);
  24. expect(projectStateReducer(defaultState, {type: 'anything'}).projectId).toBe(null);
  25. expect(projectStateReducer(defaultState, {type: 'anything'}).loadingState).toBe(LoadingState.NOT_LOADED);
  26. });
  27. test('doneCreatingProject for new project with projectId type string shows project with that id', () => {
  28. const initialState = {
  29. projectId: null,
  30. loadingState: LoadingState.CREATING_NEW
  31. };
  32. const action = doneCreatingProject('100', initialState.loadingState);
  33. const resultState = projectStateReducer(initialState, action);
  34. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  35. expect(resultState.projectId).toBe('100');
  36. });
  37. test('doneCreatingProject for new project with projectId type number shows project with id of type number', () => {
  38. const initialState = {
  39. projectId: null,
  40. loadingState: LoadingState.CREATING_NEW
  41. };
  42. const action = doneCreatingProject(100, initialState.loadingState);
  43. const resultState = projectStateReducer(initialState, action);
  44. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  45. expect(resultState.projectId).toBe(100);
  46. });
  47. test('doneCreatingProject for remix shows project with that id', () => {
  48. const initialState = {
  49. projectId: null,
  50. loadingState: LoadingState.REMIXING
  51. };
  52. const action = doneCreatingProject('100', initialState.loadingState);
  53. const resultState = projectStateReducer(initialState, action);
  54. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  55. expect(resultState.projectId).toBe('100');
  56. });
  57. test('doneCreatingProject for save as copy shows project with that id', () => {
  58. const initialState = {
  59. projectId: null,
  60. loadingState: LoadingState.CREATING_COPY
  61. };
  62. const action = doneCreatingProject('100', initialState.loadingState);
  63. const resultState = projectStateReducer(initialState, action);
  64. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  65. expect(resultState.projectId).toBe('100');
  66. });
  67. test('onFetchedProjectData with id loads project data into vm', () => {
  68. const initialState = {
  69. projectData: null,
  70. loadingState: LoadingState.FETCHING_WITH_ID
  71. };
  72. const action = onFetchedProjectData('1010101', initialState.loadingState);
  73. const resultState = projectStateReducer(initialState, action);
  74. expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_WITH_ID);
  75. expect(resultState.projectData).toBe('1010101');
  76. });
  77. test('onFetchedProjectData new loads project data into vm', () => {
  78. const initialState = {
  79. projectData: null,
  80. loadingState: LoadingState.FETCHING_NEW_DEFAULT
  81. };
  82. const action = onFetchedProjectData('1010101', initialState.loadingState);
  83. const resultState = projectStateReducer(initialState, action);
  84. expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_NEW_DEFAULT);
  85. expect(resultState.projectData).toBe('1010101');
  86. });
  87. test('onLoadedProject upload, with canSave false, shows without id', () => {
  88. const initialState = {
  89. loadingState: LoadingState.LOADING_VM_FILE_UPLOAD
  90. };
  91. const action = onLoadedProject(initialState.loadingState, false, true);
  92. const resultState = projectStateReducer(initialState, action);
  93. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID);
  94. });
  95. test('onLoadedProject upload, with canSave true, prepares to save', () => {
  96. const initialState = {
  97. loadingState: LoadingState.LOADING_VM_FILE_UPLOAD
  98. };
  99. const action = onLoadedProject(initialState.loadingState, true, true);
  100. const resultState = projectStateReducer(initialState, action);
  101. expect(resultState.loadingState).toBe(LoadingState.AUTO_UPDATING);
  102. });
  103. test('onLoadedProject with id shows with id', () => {
  104. const initialState = {
  105. loadingState: LoadingState.LOADING_VM_WITH_ID
  106. };
  107. const action = onLoadedProject(initialState.loadingState, true, true);
  108. const resultState = projectStateReducer(initialState, action);
  109. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  110. });
  111. test('onLoadedProject new shows without id', () => {
  112. const initialState = {
  113. loadingState: LoadingState.LOADING_VM_NEW_DEFAULT
  114. };
  115. const action = onLoadedProject(initialState.loadingState, false, true);
  116. const resultState = projectStateReducer(initialState, action);
  117. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID);
  118. });
  119. test('onLoadedProject new, to save shows without id', () => {
  120. const initialState = {
  121. loadingState: LoadingState.LOADING_VM_NEW_DEFAULT
  122. };
  123. const action = onLoadedProject(initialState.loadingState, true, true);
  124. const resultState = projectStateReducer(initialState, action);
  125. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID);
  126. });
  127. test('onLoadedProject with success false, no project id, shows without id', () => {
  128. const initialState = {
  129. loadingState: LoadingState.LOADING_VM_WITH_ID,
  130. projectId: null
  131. };
  132. const action = onLoadedProject(initialState.loadingState, false, false);
  133. const resultState = projectStateReducer(initialState, action);
  134. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID);
  135. });
  136. test('onLoadedProject with success false, valid project id, shows with id', () => {
  137. const initialState = {
  138. loadingState: LoadingState.LOADING_VM_WITH_ID,
  139. projectId: '12345'
  140. };
  141. const action = onLoadedProject(initialState.loadingState, false, false);
  142. const resultState = projectStateReducer(initialState, action);
  143. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  144. });
  145. test('doneUpdatingProject with id shows with id', () => {
  146. const initialState = {
  147. loadingState: LoadingState.MANUAL_UPDATING
  148. };
  149. const action = doneUpdatingProject(initialState.loadingState);
  150. const resultState = projectStateReducer(initialState, action);
  151. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  152. });
  153. test('doneUpdatingProject with id, before copy, creates copy', () => {
  154. const initialState = {
  155. loadingState: LoadingState.UPDATING_BEFORE_COPY
  156. };
  157. const action = doneUpdatingProject(initialState.loadingState);
  158. const resultState = projectStateReducer(initialState, action);
  159. expect(resultState.loadingState).toBe(LoadingState.CREATING_COPY);
  160. });
  161. test('doneUpdatingProject with id, before new, fetches default project', () => {
  162. const initialState = {
  163. loadingState: LoadingState.UPDATING_BEFORE_NEW
  164. };
  165. const action = doneUpdatingProject(initialState.loadingState);
  166. const resultState = projectStateReducer(initialState, action);
  167. expect(resultState.loadingState).toBe(LoadingState.FETCHING_NEW_DEFAULT);
  168. });
  169. test('setProjectId, with same id as before, should show with id, not fetch', () => {
  170. const initialState = {
  171. projectId: '100',
  172. loadingState: LoadingState.SHOWING_WITH_ID
  173. };
  174. const action = setProjectId('100');
  175. const resultState = projectStateReducer(initialState, action);
  176. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  177. expect(resultState.projectId).toBe('100');
  178. });
  179. test('setProjectId, with different id as before, should fetch with id, not show with id', () => {
  180. const initialState = {
  181. projectId: 99,
  182. loadingState: LoadingState.SHOWING_WITH_ID
  183. };
  184. const action = setProjectId('100');
  185. const resultState = projectStateReducer(initialState, action);
  186. expect(resultState.loadingState).toBe(LoadingState.FETCHING_WITH_ID);
  187. expect(resultState.projectId).toBe('100');
  188. });
  189. test('setProjectId, with same id as before, but not same type, should fetch because not ===', () => {
  190. const initialState = {
  191. projectId: '100',
  192. loadingState: LoadingState.SHOWING_WITH_ID
  193. };
  194. const action = setProjectId(100);
  195. const resultState = projectStateReducer(initialState, action);
  196. expect(resultState.loadingState).toBe(LoadingState.FETCHING_WITH_ID);
  197. expect(resultState.projectId).toBe(100);
  198. });
  199. test('requestNewProject, when can\'t create new, should fetch default project without id', () => {
  200. const initialState = {
  201. loadingState: LoadingState.SHOWING_WITHOUT_ID
  202. };
  203. const action = requestNewProject(false);
  204. const resultState = projectStateReducer(initialState, action);
  205. expect(resultState.loadingState).toBe(LoadingState.FETCHING_NEW_DEFAULT);
  206. });
  207. test('requestNewProject, when can create new, should save and prepare to fetch default project', () => {
  208. const initialState = {
  209. loadingState: LoadingState.SHOWING_WITH_ID
  210. };
  211. const action = requestNewProject(true);
  212. const resultState = projectStateReducer(initialState, action);
  213. expect(resultState.loadingState).toBe(LoadingState.UPDATING_BEFORE_NEW);
  214. });
  215. test('requestProjectUpload when project not loaded should load', () => {
  216. const initialState = {
  217. loadingState: LoadingState.NOT_LOADED
  218. };
  219. const action = requestProjectUpload(initialState.loadingState);
  220. const resultState = projectStateReducer(initialState, action);
  221. expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_FILE_UPLOAD);
  222. });
  223. test('requestProjectUpload when showing project with id should load', () => {
  224. const initialState = {
  225. loadingState: LoadingState.SHOWING_WITH_ID
  226. };
  227. const action = requestProjectUpload(initialState.loadingState);
  228. const resultState = projectStateReducer(initialState, action);
  229. expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_FILE_UPLOAD);
  230. });
  231. test('requestProjectUpload when showing project without id should load', () => {
  232. const initialState = {
  233. loadingState: LoadingState.SHOWING_WITHOUT_ID
  234. };
  235. const action = requestProjectUpload(initialState.loadingState);
  236. const resultState = projectStateReducer(initialState, action);
  237. expect(resultState.loadingState).toBe(LoadingState.LOADING_VM_FILE_UPLOAD);
  238. });
  239. test('manualUpdateProject should prepare to update', () => {
  240. const initialState = {
  241. loadingState: LoadingState.SHOWING_WITH_ID
  242. };
  243. const action = manualUpdateProject();
  244. const resultState = projectStateReducer(initialState, action);
  245. expect(resultState.loadingState).toBe(LoadingState.MANUAL_UPDATING);
  246. });
  247. test('autoUpdateProject should prepare to update', () => {
  248. const initialState = {
  249. loadingState: LoadingState.SHOWING_WITH_ID
  250. };
  251. const action = autoUpdateProject();
  252. const resultState = projectStateReducer(initialState, action);
  253. expect(resultState.loadingState).toBe(LoadingState.AUTO_UPDATING);
  254. });
  255. test('saveProjectAsCopy should save, before preparing to save as a copy', () => {
  256. const initialState = {
  257. loadingState: LoadingState.SHOWING_WITH_ID
  258. };
  259. const action = saveProjectAsCopy();
  260. const resultState = projectStateReducer(initialState, action);
  261. expect(resultState.loadingState).toBe(LoadingState.UPDATING_BEFORE_COPY);
  262. });
  263. test('remixProject should prepare to remix', () => {
  264. const initialState = {
  265. loadingState: LoadingState.SHOWING_WITH_ID
  266. };
  267. const action = remixProject();
  268. const resultState = projectStateReducer(initialState, action);
  269. expect(resultState.loadingState).toBe(LoadingState.REMIXING);
  270. });
  271. test('projectError from various states should show error', () => {
  272. const startStates = [
  273. LoadingState.AUTO_UPDATING,
  274. LoadingState.CREATING_NEW,
  275. LoadingState.FETCHING_NEW_DEFAULT,
  276. LoadingState.FETCHING_WITH_ID,
  277. LoadingState.LOADING_VM_NEW_DEFAULT,
  278. LoadingState.LOADING_VM_WITH_ID,
  279. LoadingState.MANUAL_UPDATING,
  280. LoadingState.REMIXING,
  281. LoadingState.CREATING_COPY,
  282. LoadingState.UPDATING_BEFORE_NEW
  283. ];
  284. for (const startState of startStates) {
  285. const initialState = {
  286. error: null,
  287. loadingState: startState
  288. };
  289. const action = projectError('Error string');
  290. const resultState = projectStateReducer(initialState, action);
  291. expect(resultState.error).toEqual('Error string');
  292. }
  293. });
  294. test('fatal projectError should show error state', () => {
  295. const startStates = [
  296. LoadingState.FETCHING_NEW_DEFAULT,
  297. LoadingState.FETCHING_WITH_ID,
  298. LoadingState.LOADING_VM_NEW_DEFAULT,
  299. LoadingState.LOADING_VM_WITH_ID
  300. ];
  301. for (const startState of startStates) {
  302. const initialState = {
  303. error: null,
  304. loadingState: startState
  305. };
  306. const action = projectError('Error string');
  307. const resultState = projectStateReducer(initialState, action);
  308. expect(resultState.loadingState).toBe(LoadingState.ERROR);
  309. }
  310. });
  311. test('non-fatal projectError should show normal state', () => {
  312. const startStates = [
  313. LoadingState.AUTO_UPDATING,
  314. LoadingState.CREATING_COPY,
  315. LoadingState.MANUAL_UPDATING,
  316. LoadingState.REMIXING,
  317. LoadingState.UPDATING_BEFORE_NEW
  318. ];
  319. for (const startState of startStates) {
  320. const initialState = {
  321. error: null,
  322. loadingState: startState
  323. };
  324. const action = projectError('Error string');
  325. const resultState = projectStateReducer(initialState, action);
  326. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  327. }
  328. });
  329. test('projectError when creating new while viewing project with id should show project with id', () => {
  330. const initialState = {
  331. error: null,
  332. loadingState: LoadingState.CREATING_NEW,
  333. projectId: '12345'
  334. };
  335. const action = projectError('Error string');
  336. const resultState = projectStateReducer(initialState, action);
  337. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITH_ID);
  338. });
  339. test('projectError when creating new while logged out, looking at default project should show default project', () => {
  340. const initialState = {
  341. error: null,
  342. loadingState: LoadingState.CREATING_NEW,
  343. projectId: '0'
  344. };
  345. const action = projectError('Error string');
  346. const resultState = projectStateReducer(initialState, action);
  347. expect(resultState.loadingState).toBe(LoadingState.SHOWING_WITHOUT_ID);
  348. });
  349. test('projectError from showing project should show error', () => {
  350. const initialState = {
  351. error: null,
  352. loadingState: LoadingState.FETCHING_WITH_ID
  353. };
  354. const action = projectError('Error string');
  355. const resultState = projectStateReducer(initialState, action);
  356. expect(resultState.loadingState).toBe(LoadingState.ERROR);
  357. expect(resultState.error).toEqual('Error string');
  358. });