/spec/frontend/vue_shared/components/filtered_search_bar/store/modules/filters/actions_spec.js

https://gitlab.com/klml/gitlab-ee · JavaScript · 448 lines · 408 code · 40 blank · 0 comment · 0 complexity · ce017543010dd6b8912f79f49dfc5f30 MD5 · raw file

  1. import axios from 'axios';
  2. import MockAdapter from 'axios-mock-adapter';
  3. import testAction from 'helpers/vuex_action_helper';
  4. import { mockBranches } from 'jest/vue_shared/components/filtered_search_bar/mock_data';
  5. import Api from '~/api';
  6. import { deprecatedCreateFlash as createFlash } from '~/flash';
  7. import httpStatusCodes from '~/lib/utils/http_status';
  8. import * as actions from '~/vue_shared/components/filtered_search_bar/store/modules/filters/actions';
  9. import * as types from '~/vue_shared/components/filtered_search_bar/store/modules/filters/mutation_types';
  10. import initialState from '~/vue_shared/components/filtered_search_bar/store/modules/filters/state';
  11. import { filterMilestones, filterUsers, filterLabels } from './mock_data';
  12. const milestonesEndpoint = 'fake_milestones_endpoint';
  13. const labelsEndpoint = 'fake_labels_endpoint';
  14. const groupEndpoint = 'fake_group_endpoint';
  15. const projectEndpoint = 'fake_project_endpoint';
  16. jest.mock('~/flash');
  17. describe('Filters actions', () => {
  18. let state;
  19. let mock;
  20. let mockDispatch;
  21. let mockCommit;
  22. beforeEach(() => {
  23. state = initialState();
  24. mock = new MockAdapter(axios);
  25. mockDispatch = jest.fn().mockResolvedValue();
  26. mockCommit = jest.fn();
  27. });
  28. afterEach(() => {
  29. mock.restore();
  30. });
  31. describe('initialize', () => {
  32. const initialData = {
  33. milestonesEndpoint,
  34. labelsEndpoint,
  35. groupEndpoint,
  36. projectEndpoint,
  37. selectedAuthor: 'Mr cool',
  38. selectedMilestone: 'NEXT',
  39. };
  40. it('does not dispatch', () => {
  41. const result = actions.initialize(
  42. {
  43. state,
  44. dispatch: mockDispatch,
  45. commit: mockCommit,
  46. },
  47. initialData,
  48. );
  49. expect(result).toBeUndefined();
  50. expect(mockDispatch).not.toHaveBeenCalled();
  51. });
  52. it(`commits the ${types.SET_SELECTED_FILTERS}`, () => {
  53. actions.initialize(
  54. {
  55. state,
  56. dispatch: mockDispatch,
  57. commit: mockCommit,
  58. },
  59. initialData,
  60. );
  61. expect(mockCommit).toHaveBeenCalledWith(types.SET_SELECTED_FILTERS, initialData);
  62. });
  63. });
  64. describe('setFilters', () => {
  65. const nextFilters = {
  66. selectedAuthor: 'Mr cool',
  67. selectedMilestone: 'NEXT',
  68. };
  69. it('dispatches the root/setFilters action', () => {
  70. return testAction(
  71. actions.setFilters,
  72. nextFilters,
  73. state,
  74. [
  75. {
  76. payload: nextFilters,
  77. type: types.SET_SELECTED_FILTERS,
  78. },
  79. ],
  80. [
  81. {
  82. type: 'setFilters',
  83. payload: nextFilters,
  84. },
  85. ],
  86. );
  87. });
  88. });
  89. describe('setEndpoints', () => {
  90. it('sets the api paths', () => {
  91. return testAction(
  92. actions.setEndpoints,
  93. { milestonesEndpoint, labelsEndpoint, groupEndpoint, projectEndpoint },
  94. state,
  95. [
  96. { payload: 'fake_milestones_endpoint', type: types.SET_MILESTONES_ENDPOINT },
  97. { payload: 'fake_labels_endpoint', type: types.SET_LABELS_ENDPOINT },
  98. { payload: 'fake_group_endpoint', type: types.SET_GROUP_ENDPOINT },
  99. { payload: 'fake_project_endpoint', type: types.SET_PROJECT_ENDPOINT },
  100. ],
  101. [],
  102. );
  103. });
  104. });
  105. describe('fetchBranches', () => {
  106. describe('success', () => {
  107. beforeEach(() => {
  108. const url = Api.buildUrl(Api.createBranchPath).replace(
  109. ':id',
  110. encodeURIComponent(projectEndpoint),
  111. );
  112. mock.onGet(url).replyOnce(httpStatusCodes.OK, mockBranches);
  113. });
  114. it('dispatches RECEIVE_BRANCHES_SUCCESS with received data', () => {
  115. return testAction(
  116. actions.fetchBranches,
  117. null,
  118. { ...state, projectEndpoint },
  119. [
  120. { type: types.REQUEST_BRANCHES },
  121. { type: types.RECEIVE_BRANCHES_SUCCESS, payload: mockBranches },
  122. ],
  123. [],
  124. ).then(({ data }) => {
  125. expect(data).toBe(mockBranches);
  126. });
  127. });
  128. });
  129. describe('error', () => {
  130. beforeEach(() => {
  131. mock.onAny().replyOnce(httpStatusCodes.SERVICE_UNAVAILABLE);
  132. });
  133. it('dispatches RECEIVE_BRANCHES_ERROR', () => {
  134. return testAction(
  135. actions.fetchBranches,
  136. null,
  137. state,
  138. [
  139. { type: types.REQUEST_BRANCHES },
  140. {
  141. type: types.RECEIVE_BRANCHES_ERROR,
  142. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  143. },
  144. ],
  145. [],
  146. ).then(() => expect(createFlash).toHaveBeenCalled());
  147. });
  148. });
  149. });
  150. describe('fetchAuthors', () => {
  151. let restoreVersion;
  152. beforeEach(() => {
  153. restoreVersion = gon.api_version;
  154. gon.api_version = 'v1';
  155. });
  156. afterEach(() => {
  157. gon.api_version = restoreVersion;
  158. });
  159. describe('success', () => {
  160. beforeEach(() => {
  161. mock.onAny().replyOnce(httpStatusCodes.OK, filterUsers);
  162. });
  163. it('dispatches RECEIVE_AUTHORS_SUCCESS with received data and groupEndpoint set', () => {
  164. return testAction(
  165. actions.fetchAuthors,
  166. null,
  167. { ...state, groupEndpoint },
  168. [
  169. { type: types.REQUEST_AUTHORS },
  170. { type: types.RECEIVE_AUTHORS_SUCCESS, payload: filterUsers },
  171. ],
  172. [],
  173. ).then(({ data }) => {
  174. expect(mock.history.get[0].url).toBe('/api/v1/groups/fake_group_endpoint/members');
  175. expect(data).toBe(filterUsers);
  176. });
  177. });
  178. it('dispatches RECEIVE_AUTHORS_SUCCESS with received data and projectEndpoint set', () => {
  179. return testAction(
  180. actions.fetchAuthors,
  181. null,
  182. { ...state, projectEndpoint },
  183. [
  184. { type: types.REQUEST_AUTHORS },
  185. { type: types.RECEIVE_AUTHORS_SUCCESS, payload: filterUsers },
  186. ],
  187. [],
  188. ).then(({ data }) => {
  189. expect(mock.history.get[0].url).toBe('/api/v1/projects/fake_project_endpoint/users');
  190. expect(data).toBe(filterUsers);
  191. });
  192. });
  193. });
  194. describe('error', () => {
  195. beforeEach(() => {
  196. mock.onAny().replyOnce(httpStatusCodes.SERVICE_UNAVAILABLE);
  197. });
  198. it('dispatches RECEIVE_AUTHORS_ERROR and groupEndpoint set', () => {
  199. return testAction(
  200. actions.fetchAuthors,
  201. null,
  202. { ...state, groupEndpoint },
  203. [
  204. { type: types.REQUEST_AUTHORS },
  205. {
  206. type: types.RECEIVE_AUTHORS_ERROR,
  207. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  208. },
  209. ],
  210. [],
  211. ).then(() => {
  212. expect(mock.history.get[0].url).toBe('/api/v1/groups/fake_group_endpoint/members');
  213. expect(createFlash).toHaveBeenCalled();
  214. });
  215. });
  216. it('dispatches RECEIVE_AUTHORS_ERROR and projectEndpoint set', () => {
  217. return testAction(
  218. actions.fetchAuthors,
  219. null,
  220. { ...state, projectEndpoint },
  221. [
  222. { type: types.REQUEST_AUTHORS },
  223. {
  224. type: types.RECEIVE_AUTHORS_ERROR,
  225. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  226. },
  227. ],
  228. [],
  229. ).then(() => {
  230. expect(mock.history.get[0].url).toBe('/api/v1/projects/fake_project_endpoint/users');
  231. expect(createFlash).toHaveBeenCalled();
  232. });
  233. });
  234. });
  235. });
  236. describe('fetchMilestones', () => {
  237. describe('success', () => {
  238. beforeEach(() => {
  239. mock.onGet(milestonesEndpoint).replyOnce(httpStatusCodes.OK, filterMilestones);
  240. });
  241. it('dispatches RECEIVE_MILESTONES_SUCCESS with received data', () => {
  242. return testAction(
  243. actions.fetchMilestones,
  244. null,
  245. { ...state, milestonesEndpoint },
  246. [
  247. { type: types.REQUEST_MILESTONES },
  248. { type: types.RECEIVE_MILESTONES_SUCCESS, payload: filterMilestones },
  249. ],
  250. [],
  251. ).then(({ data }) => {
  252. expect(data).toBe(filterMilestones);
  253. });
  254. });
  255. });
  256. describe('error', () => {
  257. beforeEach(() => {
  258. mock.onAny().replyOnce(httpStatusCodes.SERVICE_UNAVAILABLE);
  259. });
  260. it('dispatches RECEIVE_MILESTONES_ERROR', () => {
  261. return testAction(
  262. actions.fetchMilestones,
  263. null,
  264. state,
  265. [
  266. { type: types.REQUEST_MILESTONES },
  267. {
  268. type: types.RECEIVE_MILESTONES_ERROR,
  269. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  270. },
  271. ],
  272. [],
  273. ).then(() => expect(createFlash).toHaveBeenCalled());
  274. });
  275. });
  276. });
  277. describe('fetchAssignees', () => {
  278. describe('success', () => {
  279. let restoreVersion;
  280. beforeEach(() => {
  281. mock.onAny().replyOnce(httpStatusCodes.OK, filterUsers);
  282. restoreVersion = gon.api_version;
  283. gon.api_version = 'v1';
  284. });
  285. afterEach(() => {
  286. gon.api_version = restoreVersion;
  287. });
  288. it('dispatches RECEIVE_ASSIGNEES_SUCCESS with received data and groupEndpoint set', () => {
  289. return testAction(
  290. actions.fetchAssignees,
  291. null,
  292. { ...state, milestonesEndpoint, groupEndpoint },
  293. [
  294. { type: types.REQUEST_ASSIGNEES },
  295. { type: types.RECEIVE_ASSIGNEES_SUCCESS, payload: filterUsers },
  296. ],
  297. [],
  298. ).then(({ data }) => {
  299. expect(mock.history.get[0].url).toBe('/api/v1/groups/fake_group_endpoint/members');
  300. expect(data).toBe(filterUsers);
  301. });
  302. });
  303. it('dispatches RECEIVE_ASSIGNEES_SUCCESS with received data and projectEndpoint set', () => {
  304. return testAction(
  305. actions.fetchAssignees,
  306. null,
  307. { ...state, milestonesEndpoint, projectEndpoint },
  308. [
  309. { type: types.REQUEST_ASSIGNEES },
  310. { type: types.RECEIVE_ASSIGNEES_SUCCESS, payload: filterUsers },
  311. ],
  312. [],
  313. ).then(({ data }) => {
  314. expect(mock.history.get[0].url).toBe('/api/v1/projects/fake_project_endpoint/users');
  315. expect(data).toBe(filterUsers);
  316. });
  317. });
  318. });
  319. describe('error', () => {
  320. let restoreVersion;
  321. beforeEach(() => {
  322. mock.onAny().replyOnce(httpStatusCodes.SERVICE_UNAVAILABLE);
  323. restoreVersion = gon.api_version;
  324. gon.api_version = 'v1';
  325. });
  326. afterEach(() => {
  327. gon.api_version = restoreVersion;
  328. });
  329. it('dispatches RECEIVE_ASSIGNEES_ERROR and groupEndpoint set', () => {
  330. return testAction(
  331. actions.fetchAssignees,
  332. null,
  333. { ...state, groupEndpoint },
  334. [
  335. { type: types.REQUEST_ASSIGNEES },
  336. {
  337. type: types.RECEIVE_ASSIGNEES_ERROR,
  338. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  339. },
  340. ],
  341. [],
  342. ).then(() => {
  343. expect(mock.history.get[0].url).toBe('/api/v1/groups/fake_group_endpoint/members');
  344. expect(createFlash).toHaveBeenCalled();
  345. });
  346. });
  347. it('dispatches RECEIVE_ASSIGNEES_ERROR and projectEndpoint set', () => {
  348. return testAction(
  349. actions.fetchAssignees,
  350. null,
  351. { ...state, projectEndpoint },
  352. [
  353. { type: types.REQUEST_ASSIGNEES },
  354. {
  355. type: types.RECEIVE_ASSIGNEES_ERROR,
  356. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  357. },
  358. ],
  359. [],
  360. ).then(() => {
  361. expect(mock.history.get[0].url).toBe('/api/v1/projects/fake_project_endpoint/users');
  362. expect(createFlash).toHaveBeenCalled();
  363. });
  364. });
  365. });
  366. });
  367. describe('fetchLabels', () => {
  368. describe('success', () => {
  369. beforeEach(() => {
  370. mock.onGet(labelsEndpoint).replyOnce(httpStatusCodes.OK, filterLabels);
  371. });
  372. it('dispatches RECEIVE_LABELS_SUCCESS with received data', () => {
  373. return testAction(
  374. actions.fetchLabels,
  375. null,
  376. { ...state, labelsEndpoint },
  377. [
  378. { type: types.REQUEST_LABELS },
  379. { type: types.RECEIVE_LABELS_SUCCESS, payload: filterLabels },
  380. ],
  381. [],
  382. ).then(({ data }) => {
  383. expect(data).toBe(filterLabels);
  384. });
  385. });
  386. });
  387. describe('error', () => {
  388. beforeEach(() => {
  389. mock.onAny().replyOnce(httpStatusCodes.SERVICE_UNAVAILABLE);
  390. });
  391. it('dispatches RECEIVE_LABELS_ERROR', () => {
  392. return testAction(
  393. actions.fetchLabels,
  394. null,
  395. state,
  396. [
  397. { type: types.REQUEST_LABELS },
  398. {
  399. type: types.RECEIVE_LABELS_ERROR,
  400. payload: httpStatusCodes.SERVICE_UNAVAILABLE,
  401. },
  402. ],
  403. [],
  404. ).then(() => expect(createFlash).toHaveBeenCalled());
  405. });
  406. });
  407. });
  408. });