/spec/javascripts/jobs/store/actions_spec.js

https://gitlab.com/artofhuman/gitlab-ce · JavaScript · 512 lines · 460 code · 52 blank · 0 comment · 0 complexity · 93875e068e57a03a5f4d8eab38ddf2cb MD5 · raw file

  1. import MockAdapter from 'axios-mock-adapter';
  2. import axios from '~/lib/utils/axios_utils';
  3. import {
  4. setJobEndpoint,
  5. setTraceOptions,
  6. clearEtagPoll,
  7. stopPolling,
  8. requestJob,
  9. fetchJob,
  10. receiveJobSuccess,
  11. receiveJobError,
  12. scrollTop,
  13. scrollBottom,
  14. requestTrace,
  15. fetchTrace,
  16. stopPollingTrace,
  17. receiveTraceSuccess,
  18. receiveTraceError,
  19. requestStages,
  20. fetchStages,
  21. receiveStagesSuccess,
  22. receiveStagesError,
  23. requestJobsForStage,
  24. fetchJobsForStage,
  25. receiveJobsForStageSuccess,
  26. receiveJobsForStageError,
  27. hideSidebar,
  28. showSidebar,
  29. toggleSidebar,
  30. } from '~/jobs/store/actions';
  31. import state from '~/jobs/store/state';
  32. import * as types from '~/jobs/store/mutation_types';
  33. import testAction from 'spec/helpers/vuex_action_helper';
  34. import { TEST_HOST } from 'spec/test_constants';
  35. describe('Job State actions', () => {
  36. let mockedState;
  37. beforeEach(() => {
  38. mockedState = state();
  39. });
  40. describe('setJobEndpoint', () => {
  41. it('should commit SET_JOB_ENDPOINT mutation', done => {
  42. testAction(
  43. setJobEndpoint,
  44. 'job/872324.json',
  45. mockedState,
  46. [{ type: types.SET_JOB_ENDPOINT, payload: 'job/872324.json' }],
  47. [],
  48. done,
  49. );
  50. });
  51. });
  52. describe('setTraceOptions', () => {
  53. it('should commit SET_TRACE_OPTIONS mutation', done => {
  54. testAction(
  55. setTraceOptions,
  56. { pagePath: 'job/872324/trace.json' },
  57. mockedState,
  58. [{ type: types.SET_TRACE_OPTIONS, payload: { pagePath: 'job/872324/trace.json' } }],
  59. [],
  60. done,
  61. );
  62. });
  63. });
  64. describe('hideSidebar', () => {
  65. it('should commit HIDE_SIDEBAR mutation', done => {
  66. testAction(hideSidebar, null, mockedState, [{ type: types.HIDE_SIDEBAR }], [], done);
  67. });
  68. });
  69. describe('showSidebar', () => {
  70. it('should commit HIDE_SIDEBAR mutation', done => {
  71. testAction(showSidebar, null, mockedState, [{ type: types.SHOW_SIDEBAR }], [], done);
  72. });
  73. });
  74. describe('toggleSidebar', () => {
  75. describe('when isSidebarOpen is true', () => {
  76. it('should dispatch hideSidebar', done => {
  77. testAction(toggleSidebar, null, mockedState, [], [{ type: 'hideSidebar' }], done);
  78. });
  79. });
  80. describe('when isSidebarOpen is false', () => {
  81. it('should dispatch showSidebar', done => {
  82. mockedState.isSidebarOpen = false;
  83. testAction(toggleSidebar, null, mockedState, [], [{ type: 'showSidebar' }], done);
  84. });
  85. });
  86. });
  87. describe('requestJob', () => {
  88. it('should commit REQUEST_JOB mutation', done => {
  89. testAction(requestJob, null, mockedState, [{ type: types.REQUEST_JOB }], [], done);
  90. });
  91. });
  92. describe('fetchJob', () => {
  93. let mock;
  94. beforeEach(() => {
  95. mockedState.jobEndpoint = `${TEST_HOST}/endpoint.json`;
  96. mock = new MockAdapter(axios);
  97. });
  98. afterEach(() => {
  99. mock.restore();
  100. stopPolling();
  101. clearEtagPoll();
  102. });
  103. describe('success', () => {
  104. it('dispatches requestJob and receiveJobSuccess ', done => {
  105. mock.onGet(`${TEST_HOST}/endpoint.json`).replyOnce(200, { id: 121212, name: 'karma' });
  106. testAction(
  107. fetchJob,
  108. null,
  109. mockedState,
  110. [],
  111. [
  112. {
  113. type: 'requestJob',
  114. },
  115. {
  116. payload: { id: 121212, name: 'karma' },
  117. type: 'receiveJobSuccess',
  118. },
  119. ],
  120. done,
  121. );
  122. });
  123. });
  124. describe('error', () => {
  125. beforeEach(() => {
  126. mock.onGet(`${TEST_HOST}/endpoint.json`).reply(500);
  127. });
  128. it('dispatches requestJob and receiveJobError ', done => {
  129. testAction(
  130. fetchJob,
  131. null,
  132. mockedState,
  133. [],
  134. [
  135. {
  136. type: 'requestJob',
  137. },
  138. {
  139. type: 'receiveJobError',
  140. },
  141. ],
  142. done,
  143. );
  144. });
  145. });
  146. });
  147. describe('receiveJobSuccess', () => {
  148. it('should commit RECEIVE_JOB_SUCCESS mutation', done => {
  149. testAction(
  150. receiveJobSuccess,
  151. { id: 121232132 },
  152. mockedState,
  153. [{ type: types.RECEIVE_JOB_SUCCESS, payload: { id: 121232132 } }],
  154. [],
  155. done,
  156. );
  157. });
  158. });
  159. describe('receiveJobError', () => {
  160. it('should commit RECEIVE_JOB_ERROR mutation', done => {
  161. testAction(receiveJobError, null, mockedState, [{ type: types.RECEIVE_JOB_ERROR }], [], done);
  162. });
  163. });
  164. describe('scrollTop', () => {
  165. it('should dispatch toggleScrollButtons action', done => {
  166. testAction(scrollTop, null, mockedState, [], [{ type: 'toggleScrollButtons' }], done);
  167. });
  168. });
  169. describe('scrollBottom', () => {
  170. it('should dispatch toggleScrollButtons action', done => {
  171. testAction(scrollBottom, null, mockedState, [], [{ type: 'toggleScrollButtons' }], done);
  172. });
  173. });
  174. describe('requestTrace', () => {
  175. it('should commit REQUEST_TRACE mutation', done => {
  176. testAction(requestTrace, null, mockedState, [{ type: types.REQUEST_TRACE }], [], done);
  177. });
  178. });
  179. describe('fetchTrace', () => {
  180. let mock;
  181. beforeEach(() => {
  182. mockedState.traceEndpoint = `${TEST_HOST}/endpoint`;
  183. mock = new MockAdapter(axios);
  184. });
  185. afterEach(() => {
  186. mock.restore();
  187. stopPolling();
  188. clearEtagPoll();
  189. });
  190. describe('success', () => {
  191. it('dispatches requestTrace, receiveTraceSuccess and stopPollingTrace when job is complete', done => {
  192. mock.onGet(`${TEST_HOST}/endpoint/trace.json`).replyOnce(200, {
  193. html: 'I, [2018-08-17T22:57:45.707325 #1841] INFO -- :',
  194. complete: true,
  195. });
  196. testAction(
  197. fetchTrace,
  198. null,
  199. mockedState,
  200. [],
  201. [
  202. {
  203. type: 'toggleScrollisInBottom',
  204. payload: true,
  205. },
  206. {
  207. payload: {
  208. html: 'I, [2018-08-17T22:57:45.707325 #1841] INFO -- :',
  209. complete: true,
  210. },
  211. type: 'receiveTraceSuccess',
  212. },
  213. {
  214. type: 'stopPollingTrace',
  215. },
  216. ],
  217. done,
  218. );
  219. });
  220. });
  221. describe('error', () => {
  222. beforeEach(() => {
  223. mock.onGet(`${TEST_HOST}/endpoint/trace.json`).reply(500);
  224. });
  225. it('dispatches requestTrace and receiveTraceError ', done => {
  226. testAction(
  227. fetchTrace,
  228. null,
  229. mockedState,
  230. [],
  231. [
  232. {
  233. type: 'receiveTraceError',
  234. },
  235. ],
  236. done,
  237. );
  238. });
  239. });
  240. });
  241. describe('stopPollingTrace', () => {
  242. it('should commit STOP_POLLING_TRACE mutation ', done => {
  243. testAction(
  244. stopPollingTrace,
  245. null,
  246. mockedState,
  247. [{ type: types.STOP_POLLING_TRACE }],
  248. [],
  249. done,
  250. );
  251. });
  252. });
  253. describe('receiveTraceSuccess', () => {
  254. it('should commit RECEIVE_TRACE_SUCCESS mutation ', done => {
  255. testAction(
  256. receiveTraceSuccess,
  257. 'hello world',
  258. mockedState,
  259. [{ type: types.RECEIVE_TRACE_SUCCESS, payload: 'hello world' }],
  260. [],
  261. done,
  262. );
  263. });
  264. });
  265. describe('receiveTraceError', () => {
  266. it('should commit RECEIVE_TRACE_ERROR mutation ', done => {
  267. testAction(
  268. receiveTraceError,
  269. null,
  270. mockedState,
  271. [{ type: types.RECEIVE_TRACE_ERROR }],
  272. [],
  273. done,
  274. );
  275. });
  276. });
  277. describe('requestStages', () => {
  278. it('should commit REQUEST_STAGES mutation ', done => {
  279. testAction(requestStages, null, mockedState, [{ type: types.REQUEST_STAGES }], [], done);
  280. });
  281. });
  282. describe('fetchStages', () => {
  283. let mock;
  284. beforeEach(() => {
  285. mockedState.job.pipeline = {
  286. path: `${TEST_HOST}/endpoint`,
  287. };
  288. mockedState.selectedStage = 'deploy';
  289. mock = new MockAdapter(axios);
  290. });
  291. afterEach(() => {
  292. mock.restore();
  293. });
  294. describe('success', () => {
  295. it('dispatches requestStages and receiveStagesSuccess, fetchJobsForStage ', done => {
  296. mock
  297. .onGet(`${TEST_HOST}/endpoint.json`)
  298. .replyOnce(200, { details: { stages: [{ name: 'build' }, { name: 'deploy' }] } });
  299. testAction(
  300. fetchStages,
  301. null,
  302. mockedState,
  303. [],
  304. [
  305. {
  306. type: 'requestStages',
  307. },
  308. {
  309. payload: [{ name: 'build' }, { name: 'deploy' }],
  310. type: 'receiveStagesSuccess',
  311. },
  312. {
  313. payload: { name: 'deploy' },
  314. type: 'fetchJobsForStage',
  315. },
  316. ],
  317. done,
  318. );
  319. });
  320. });
  321. describe('error', () => {
  322. beforeEach(() => {
  323. mock.onGet(`${TEST_HOST}/endpoint.json`).reply(500);
  324. });
  325. it('dispatches requestStages and receiveStagesError ', done => {
  326. testAction(
  327. fetchStages,
  328. null,
  329. mockedState,
  330. [],
  331. [
  332. {
  333. type: 'requestStages',
  334. },
  335. {
  336. type: 'receiveStagesError',
  337. },
  338. ],
  339. done,
  340. );
  341. });
  342. });
  343. });
  344. describe('receiveStagesSuccess', () => {
  345. it('should commit RECEIVE_STAGES_SUCCESS mutation ', done => {
  346. testAction(
  347. receiveStagesSuccess,
  348. {},
  349. mockedState,
  350. [{ type: types.RECEIVE_STAGES_SUCCESS, payload: {} }],
  351. [],
  352. done,
  353. );
  354. });
  355. });
  356. describe('receiveStagesError', () => {
  357. it('should commit RECEIVE_STAGES_ERROR mutation ', done => {
  358. testAction(
  359. receiveStagesError,
  360. null,
  361. mockedState,
  362. [{ type: types.RECEIVE_STAGES_ERROR }],
  363. [],
  364. done,
  365. );
  366. });
  367. });
  368. describe('requestJobsForStage', () => {
  369. it('should commit REQUEST_JOBS_FOR_STAGE mutation ', done => {
  370. testAction(
  371. requestJobsForStage,
  372. { name: 'deploy' },
  373. mockedState,
  374. [{ type: types.REQUEST_JOBS_FOR_STAGE, payload: { name: 'deploy' } }],
  375. [],
  376. done,
  377. );
  378. });
  379. });
  380. describe('fetchJobsForStage', () => {
  381. let mock;
  382. beforeEach(() => {
  383. mock = new MockAdapter(axios);
  384. });
  385. afterEach(() => {
  386. mock.restore();
  387. });
  388. describe('success', () => {
  389. it('dispatches requestJobsForStage and receiveJobsForStageSuccess ', done => {
  390. mock
  391. .onGet(`${TEST_HOST}/jobs.json`)
  392. .replyOnce(200, { latest_statuses: [{ id: 121212, name: 'build' }], retried: [] });
  393. testAction(
  394. fetchJobsForStage,
  395. { dropdown_path: `${TEST_HOST}/jobs.json` },
  396. mockedState,
  397. [],
  398. [
  399. {
  400. type: 'requestJobsForStage',
  401. payload: { dropdown_path: `${TEST_HOST}/jobs.json` },
  402. },
  403. {
  404. payload: [{ id: 121212, name: 'build' }],
  405. type: 'receiveJobsForStageSuccess',
  406. },
  407. ],
  408. done,
  409. );
  410. });
  411. });
  412. describe('error', () => {
  413. beforeEach(() => {
  414. mock.onGet(`${TEST_HOST}/jobs.json`).reply(500);
  415. });
  416. it('dispatches requestJobsForStage and receiveJobsForStageError', done => {
  417. testAction(
  418. fetchJobsForStage,
  419. { dropdown_path: `${TEST_HOST}/jobs.json` },
  420. mockedState,
  421. [],
  422. [
  423. {
  424. type: 'requestJobsForStage',
  425. payload: { dropdown_path: `${TEST_HOST}/jobs.json` },
  426. },
  427. {
  428. type: 'receiveJobsForStageError',
  429. },
  430. ],
  431. done,
  432. );
  433. });
  434. });
  435. });
  436. describe('receiveJobsForStageSuccess', () => {
  437. it('should commit RECEIVE_JOBS_FOR_STAGE_SUCCESS mutation ', done => {
  438. testAction(
  439. receiveJobsForStageSuccess,
  440. [{ id: 121212, name: 'karma' }],
  441. mockedState,
  442. [{ type: types.RECEIVE_JOBS_FOR_STAGE_SUCCESS, payload: [{ id: 121212, name: 'karma' }] }],
  443. [],
  444. done,
  445. );
  446. });
  447. });
  448. describe('receiveJobsForStageError', () => {
  449. it('should commit RECEIVE_JOBS_FOR_STAGE_ERROR mutation ', done => {
  450. testAction(
  451. receiveJobsForStageError,
  452. null,
  453. mockedState,
  454. [{ type: types.RECEIVE_JOBS_FOR_STAGE_ERROR }],
  455. [],
  456. done,
  457. );
  458. });
  459. });
  460. });