actions_spec.js 17.2 KB
Newer Older
1
import Api from 'ee/api';
2 3
import { ACTION_TYPES, PREV, NEXT, DEFAULT_PAGE_SIZE } from 'ee/geo_replicable/constants';
import buildReplicableTypeQuery from 'ee/geo_replicable/graphql/replicable_type_query_builder';
4 5 6
import * as actions from 'ee/geo_replicable/store/actions';
import * as types from 'ee/geo_replicable/store/mutation_types';
import createState from 'ee/geo_replicable/store/state';
7
import { gqClient } from 'ee/geo_replicable/utils';
8 9
import testAction from 'helpers/vuex_action_helper';
import { deprecatedCreateFlash as flash } from '~/flash';
10 11
import { normalizeHeaders, parseIntPagination } from '~/lib/utils/common_utils';
import toast from '~/vue_shared/plugins/global_toast';
Zack Cuddy's avatar
Zack Cuddy committed
12 13 14 15
import {
  MOCK_BASIC_FETCH_DATA_MAP,
  MOCK_BASIC_FETCH_RESPONSE,
  MOCK_BASIC_POST_RESPONSE,
16
  MOCK_REPLICABLE_TYPE,
17 18
  MOCK_RESTFUL_PAGINATION_DATA,
  MOCK_BASIC_GRAPHQL_QUERY_RESPONSE,
19
  MOCK_GRAPHQL_PAGINATION_DATA,
20
  MOCK_GRAPHQL_REGISTRY,
Zack Cuddy's avatar
Zack Cuddy committed
21
} from '../mock_data';
22 23

jest.mock('~/flash');
24
jest.mock('~/vue_shared/plugins/global_toast');
25

26
describe('GeoReplicable Store Actions', () => {
27
  let state;
Zack Cuddy's avatar
Zack Cuddy committed
28

29
  beforeEach(() => {
30
    state = createState({ replicableType: MOCK_REPLICABLE_TYPE, graphqlFieldName: null });
31 32
  });

33 34 35 36 37 38 39 40 41 42
  describe('requestReplicableItems', () => {
    it('should commit mutation REQUEST_REPLICABLE_ITEMS', done => {
      testAction(
        actions.requestReplicableItems,
        null,
        state,
        [{ type: types.REQUEST_REPLICABLE_ITEMS }],
        [],
        done,
      );
43 44 45
    });
  });

46 47
  describe('receiveReplicableItemsSuccess', () => {
    it('should commit mutation RECEIVE_REPLICABLE_ITEMS_SUCCESS', done => {
48
      testAction(
49
        actions.receiveReplicableItemsSuccess,
50
        { data: MOCK_BASIC_FETCH_DATA_MAP, pagination: MOCK_RESTFUL_PAGINATION_DATA },
51
        state,
52 53 54 55 56 57
        [
          {
            type: types.RECEIVE_REPLICABLE_ITEMS_SUCCESS,
            payload: { data: MOCK_BASIC_FETCH_DATA_MAP, pagination: MOCK_RESTFUL_PAGINATION_DATA },
          },
        ],
58 59 60 61 62 63
        [],
        done,
      );
    });
  });

64 65
  describe('receiveReplicableItemsError', () => {
    it('should commit mutation RECEIVE_REPLICABLE_ITEMS_ERROR', () => {
66
      testAction(
67
        actions.receiveReplicableItemsError,
68 69
        null,
        state,
70
        [{ type: types.RECEIVE_REPLICABLE_ITEMS_ERROR }],
71
        [],
Zack Cuddy's avatar
Zack Cuddy committed
72 73 74
        () => {
          expect(flash).toHaveBeenCalledTimes(1);
        },
75 76 77 78
      );
    });
  });

79
  describe('fetchReplicableItems', () => {
80 81 82 83 84 85 86 87 88 89 90
    describe('with graphql', () => {
      beforeEach(() => {
        state.useGraphQl = true;
      });

      it('calls fetchReplicableItemsGraphQl', done => {
        testAction(
          actions.fetchReplicableItems,
          null,
          state,
          [],
91 92 93 94
          [
            { type: 'requestReplicableItems' },
            { type: 'fetchReplicableItemsGraphQl', payload: null },
          ],
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
          done,
        );
      });
    });

    describe('without graphql', () => {
      beforeEach(() => {
        state.useGraphQl = false;
      });

      it('calls fetchReplicableItemsRestful', done => {
        testAction(
          actions.fetchReplicableItems,
          null,
          state,
          [],
          [{ type: 'requestReplicableItems' }, { type: 'fetchReplicableItemsRestful' }],
          done,
        );
      });
    });
  });

  describe('fetchReplicableItemsGraphQl', () => {
119 120 121 122
    beforeEach(() => {
      state.graphqlFieldName = MOCK_GRAPHQL_REGISTRY;
    });

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    describe('on success with no registry data', () => {
      beforeEach(() => {
        jest.spyOn(gqClient, 'query').mockResolvedValue({
          data: {},
        });
      });

      const direction = null;
      const data = [];

      it('should not error and pass empty values to the mutations', () => {
        testAction(
          actions.fetchReplicableItemsGraphQl,
          direction,
          state,
          [],
          [
            {
              type: 'receiveReplicableItemsSuccess',
              payload: { data, pagination: null },
            },
          ],
          () => {
            expect(gqClient.query).toHaveBeenCalledWith({
              query: buildReplicableTypeQuery(MOCK_GRAPHQL_REGISTRY),
              variables: { before: '', after: '', first: DEFAULT_PAGE_SIZE, last: null },
            });
          },
        );
      });
    });

155 156 157 158 159
    describe('on success', () => {
      beforeEach(() => {
        jest.spyOn(gqClient, 'query').mockResolvedValue({
          data: MOCK_BASIC_GRAPHQL_QUERY_RESPONSE,
        });
160
        state.paginationData = MOCK_GRAPHQL_PAGINATION_DATA;
161
        state.paginationData.page = 1;
162 163
      });

164 165
      describe('with no direction set', () => {
        const direction = null;
166
        const registries = MOCK_BASIC_GRAPHQL_QUERY_RESPONSE.geoNode[MOCK_GRAPHQL_REGISTRY];
Zack Cuddy's avatar
Zack Cuddy committed
167
        const data = registries.nodes;
168

169
        it('should call gqClient with no before/after variables as well as a first variable but no last variable', () => {
170 171 172 173 174 175 176 177 178 179 180 181 182
          testAction(
            actions.fetchReplicableItemsGraphQl,
            direction,
            state,
            [],
            [
              {
                type: 'receiveReplicableItemsSuccess',
                payload: { data, pagination: registries.pageInfo },
              },
            ],
            () => {
              expect(gqClient.query).toHaveBeenCalledWith({
183
                query: buildReplicableTypeQuery(MOCK_GRAPHQL_REGISTRY),
184
                variables: { before: '', after: '', first: DEFAULT_PAGE_SIZE, last: null },
185
              });
186
            },
187 188 189 190 191 192
          );
        });
      });

      describe('with direction set to "next"', () => {
        const direction = NEXT;
193
        const registries = MOCK_BASIC_GRAPHQL_QUERY_RESPONSE.geoNode[MOCK_GRAPHQL_REGISTRY];
Zack Cuddy's avatar
Zack Cuddy committed
194
        const data = registries.nodes;
195

196
        it('should call gqClient with after variable but no before variable as well as a first variable but no last variable', () => {
197 198 199 200 201 202 203 204 205 206 207 208 209
          testAction(
            actions.fetchReplicableItemsGraphQl,
            direction,
            state,
            [],
            [
              {
                type: 'receiveReplicableItemsSuccess',
                payload: { data, pagination: registries.pageInfo },
              },
            ],
            () => {
              expect(gqClient.query).toHaveBeenCalledWith({
210
                query: buildReplicableTypeQuery(MOCK_GRAPHQL_REGISTRY),
211 212 213 214 215 216
                variables: {
                  before: '',
                  after: MOCK_GRAPHQL_PAGINATION_DATA.endCursor,
                  first: DEFAULT_PAGE_SIZE,
                  last: null,
                },
217 218 219 220 221 222 223 224
              });
            },
          );
        });
      });

      describe('with direction set to "prev"', () => {
        const direction = PREV;
225
        const registries = MOCK_BASIC_GRAPHQL_QUERY_RESPONSE.geoNode[MOCK_GRAPHQL_REGISTRY];
Zack Cuddy's avatar
Zack Cuddy committed
226
        const data = registries.nodes;
227

228
        it('should call gqClient with before variable but no after variable as well as a last variable but no first variable', () => {
229 230 231 232 233 234 235 236 237 238 239 240 241
          testAction(
            actions.fetchReplicableItemsGraphQl,
            direction,
            state,
            [],
            [
              {
                type: 'receiveReplicableItemsSuccess',
                payload: { data, pagination: registries.pageInfo },
              },
            ],
            () => {
              expect(gqClient.query).toHaveBeenCalledWith({
242
                query: buildReplicableTypeQuery(MOCK_GRAPHQL_REGISTRY),
243 244 245 246 247 248
                variables: {
                  before: MOCK_GRAPHQL_PAGINATION_DATA.startCursor,
                  after: '',
                  first: null,
                  last: DEFAULT_PAGE_SIZE,
                },
249 250 251 252
              });
            },
          );
        });
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
      });
    });

    describe('on error', () => {
      beforeEach(() => {
        jest.spyOn(gqClient, 'query').mockRejectedValue();
      });

      it('should dispatch the request and error actions', done => {
        testAction(
          actions.fetchReplicableItemsGraphQl,
          null,
          state,
          [],
          [{ type: 'receiveReplicableItemsError' }],
          done,
        );
      });
    });
  });

  describe('fetchReplicableItemsRestful', () => {
    const normalizedHeaders = normalizeHeaders(MOCK_BASIC_FETCH_RESPONSE.headers);
    const pagination = parseIntPagination(normalizedHeaders);

278 279
    describe('on success', () => {
      beforeEach(() => {
280
        jest.spyOn(Api, 'getGeoReplicableItems').mockResolvedValue(MOCK_BASIC_FETCH_RESPONSE);
281 282
      });

283 284 285 286 287 288 289 290 291
      describe('with no params set', () => {
        const defaultParams = {
          page: 1,
          search: null,
          sync_status: null,
        };

        it('should call getGeoReplicableItems with default queryParams', () => {
          testAction(
292
            actions.fetchReplicableItemsRestful,
293 294 295 296
            {},
            state,
            [],
            [
297 298
              {
                type: 'receiveReplicableItemsSuccess',
299
                payload: { data: MOCK_BASIC_FETCH_DATA_MAP, pagination },
300
              },
301 302 303 304 305 306 307 308 309 310
            ],
            () => {
              expect(Api.getGeoReplicableItems).toHaveBeenCalledWith(
                MOCK_REPLICABLE_TYPE,
                defaultParams,
              );
            },
          );
        });
      });
311

312 313
      describe('with params set', () => {
        beforeEach(() => {
314
          state.paginationData.page = 3;
315 316 317 318 319 320
          state.searchFilter = 'test search';
          state.currentFilterIndex = 2;
        });

        it('should call getGeoReplicableItems with default queryParams', () => {
          testAction(
321
            actions.fetchReplicableItemsRestful,
322 323 324 325
            {},
            state,
            [],
            [
326 327
              {
                type: 'receiveReplicableItemsSuccess',
328
                payload: { data: MOCK_BASIC_FETCH_DATA_MAP, pagination },
329
              },
330 331 332 333 334
            ],
            () => {
              expect(Api.getGeoReplicableItems).toHaveBeenCalledWith(MOCK_REPLICABLE_TYPE, {
                page: 3,
                search: 'test search',
Zack Cuddy's avatar
Zack Cuddy committed
335
                sync_status: state.filterOptions[2].value,
336 337 338 339
              });
            },
          );
        });
340 341 342 343 344
      });
    });

    describe('on error', () => {
      beforeEach(() => {
345
        jest.spyOn(Api, 'getGeoReplicableItems').mockRejectedValue(new Error(500));
346 347 348 349
      });

      it('should dispatch the request and error actions', done => {
        testAction(
350
          actions.fetchReplicableItemsRestful,
351 352 353
          {},
          state,
          [],
354
          [{ type: 'receiveReplicableItemsError' }],
355 356 357 358 359 360
          done,
        );
      });
    });
  });

361 362
  describe('requestInitiateAllReplicableSyncs', () => {
    it('should commit mutation REQUEST_INITIATE_ALL_REPLICABLE_SYNCS', done => {
Zack Cuddy's avatar
Zack Cuddy committed
363
      testAction(
364
        actions.requestInitiateAllReplicableSyncs,
Zack Cuddy's avatar
Zack Cuddy committed
365 366
        null,
        state,
367
        [{ type: types.REQUEST_INITIATE_ALL_REPLICABLE_SYNCS }],
Zack Cuddy's avatar
Zack Cuddy committed
368 369 370 371 372 373
        [],
        done,
      );
    });
  });

374
  describe('receiveInitiateAllReplicableSyncsSuccess', () => {
375
    it('should commit mutation RECEIVE_INITIATE_ALL_REPLICABLE_SYNCS_SUCCESS and call fetchReplicableItems and toast', () => {
Zack Cuddy's avatar
Zack Cuddy committed
376
      testAction(
377
        actions.receiveInitiateAllReplicableSyncsSuccess,
378
        { action: ACTION_TYPES.RESYNC },
Zack Cuddy's avatar
Zack Cuddy committed
379
        state,
380
        [{ type: types.RECEIVE_INITIATE_ALL_REPLICABLE_SYNCS_SUCCESS }],
381
        [{ type: 'fetchReplicableItems' }],
382 383 384 385
        () => {
          expect(toast).toHaveBeenCalledTimes(1);
          toast.mockClear();
        },
Zack Cuddy's avatar
Zack Cuddy committed
386 387 388 389
      );
    });
  });

390 391
  describe('receiveInitiateAllReplicableSyncsError', () => {
    it('should commit mutation RECEIVE_INITIATE_ALL_REPLICABLE_SYNCS_ERROR', () => {
Zack Cuddy's avatar
Zack Cuddy committed
392
      testAction(
393
        actions.receiveInitiateAllReplicableSyncsError,
Zack Cuddy's avatar
Zack Cuddy committed
394 395
        ACTION_TYPES.RESYNC,
        state,
396
        [{ type: types.RECEIVE_INITIATE_ALL_REPLICABLE_SYNCS_ERROR }],
Zack Cuddy's avatar
Zack Cuddy committed
397 398 399 400 401 402 403 404
        [],
        () => {
          expect(flash).toHaveBeenCalledTimes(1);
        },
      );
    });
  });

405
  describe('initiateAllReplicableSyncs', () => {
Zack Cuddy's avatar
Zack Cuddy committed
406 407 408 409 410
    let action;

    describe('on success', () => {
      beforeEach(() => {
        action = ACTION_TYPES.RESYNC;
411 412 413
        jest
          .spyOn(Api, 'initiateAllGeoReplicableSyncs')
          .mockResolvedValue(MOCK_BASIC_POST_RESPONSE);
Zack Cuddy's avatar
Zack Cuddy committed
414 415
      });

416
      it('should dispatch the request with correct replicable param and success actions', () => {
Zack Cuddy's avatar
Zack Cuddy committed
417
        testAction(
418
          actions.initiateAllReplicableSyncs,
Zack Cuddy's avatar
Zack Cuddy committed
419 420 421 422
          action,
          state,
          [],
          [
423 424
            { type: 'requestInitiateAllReplicableSyncs' },
            { type: 'receiveInitiateAllReplicableSyncsSuccess', payload: { action } },
Zack Cuddy's avatar
Zack Cuddy committed
425
          ],
426 427 428 429 430 431
          () => {
            expect(Api.initiateAllGeoReplicableSyncs).toHaveBeenCalledWith(
              MOCK_REPLICABLE_TYPE,
              action,
            );
          },
Zack Cuddy's avatar
Zack Cuddy committed
432 433 434 435 436 437 438
        );
      });
    });

    describe('on error', () => {
      beforeEach(() => {
        action = ACTION_TYPES.RESYNC;
439
        jest.spyOn(Api, 'initiateAllGeoReplicableSyncs').mockRejectedValue(new Error(500));
Zack Cuddy's avatar
Zack Cuddy committed
440 441 442 443
      });

      it('should dispatch the request and error actions', done => {
        testAction(
444
          actions.initiateAllReplicableSyncs,
Zack Cuddy's avatar
Zack Cuddy committed
445 446 447 448
          action,
          state,
          [],
          [
449 450
            { type: 'requestInitiateAllReplicableSyncs' },
            { type: 'receiveInitiateAllReplicableSyncsError' },
Zack Cuddy's avatar
Zack Cuddy committed
451 452 453 454 455 456 457
          ],
          done,
        );
      });
    });
  });

458 459
  describe('requestInitiateReplicableSync', () => {
    it('should commit mutation REQUEST_INITIATE_REPLICABLE_SYNC', done => {
Zack Cuddy's avatar
Zack Cuddy committed
460
      testAction(
461
        actions.requestInitiateReplicableSync,
Zack Cuddy's avatar
Zack Cuddy committed
462 463
        null,
        state,
464
        [{ type: types.REQUEST_INITIATE_REPLICABLE_SYNC }],
Zack Cuddy's avatar
Zack Cuddy committed
465 466 467 468 469 470
        [],
        done,
      );
    });
  });

471
  describe('receiveInitiateReplicableSyncSuccess', () => {
472
    it('should commit mutation RECEIVE_INITIATE_REPLICABLE_SYNC_SUCCESS and call fetchReplicableItems and toast', () => {
Zack Cuddy's avatar
Zack Cuddy committed
473
      testAction(
474
        actions.receiveInitiateReplicableSyncSuccess,
475
        { action: ACTION_TYPES.RESYNC, projectName: 'test' },
Zack Cuddy's avatar
Zack Cuddy committed
476
        state,
477
        [{ type: types.RECEIVE_INITIATE_REPLICABLE_SYNC_SUCCESS }],
478
        [{ type: 'fetchReplicableItems' }],
479 480 481 482
        () => {
          expect(toast).toHaveBeenCalledTimes(1);
          toast.mockClear();
        },
Zack Cuddy's avatar
Zack Cuddy committed
483 484 485 486
      );
    });
  });

487 488
  describe('receiveInitiateReplicableSyncError', () => {
    it('should commit mutation RECEIVE_INITIATE_REPLICABLE_SYNC_ERROR', () => {
Zack Cuddy's avatar
Zack Cuddy committed
489
      testAction(
490
        actions.receiveInitiateReplicableSyncError,
Zack Cuddy's avatar
Zack Cuddy committed
491 492
        { action: ACTION_TYPES.RESYNC, projectId: 1, projectName: 'test' },
        state,
493
        [{ type: types.RECEIVE_INITIATE_REPLICABLE_SYNC_ERROR }],
Zack Cuddy's avatar
Zack Cuddy committed
494 495 496 497 498 499 500 501
        [],
        () => {
          expect(flash).toHaveBeenCalledTimes(1);
        },
      );
    });
  });

502
  describe('initiateReplicableSync', () => {
Zack Cuddy's avatar
Zack Cuddy committed
503 504 505 506 507 508 509 510 511
    let action;
    let projectId;
    let name;

    describe('on success', () => {
      beforeEach(() => {
        action = ACTION_TYPES.RESYNC;
        projectId = 1;
        name = 'test';
512
        jest.spyOn(Api, 'initiateGeoReplicableSync').mockResolvedValue(MOCK_BASIC_POST_RESPONSE);
Zack Cuddy's avatar
Zack Cuddy committed
513 514
      });

515
      it('should dispatch the request with correct replicable param and success actions', () => {
Zack Cuddy's avatar
Zack Cuddy committed
516
        testAction(
517
          actions.initiateReplicableSync,
Zack Cuddy's avatar
Zack Cuddy committed
518 519 520
          { projectId, name, action },
          state,
          [],
521
          [
522 523
            { type: 'requestInitiateReplicableSync' },
            { type: 'receiveInitiateReplicableSyncSuccess', payload: { name, action } },
524
          ],
525 526 527 528 529 530
          () => {
            expect(Api.initiateGeoReplicableSync).toHaveBeenCalledWith(MOCK_REPLICABLE_TYPE, {
              projectId,
              action,
            });
          },
Zack Cuddy's avatar
Zack Cuddy committed
531 532 533 534 535 536 537 538 539
        );
      });
    });

    describe('on error', () => {
      beforeEach(() => {
        action = ACTION_TYPES.RESYNC;
        projectId = 1;
        name = 'test';
540
        jest.spyOn(Api, 'initiateGeoReplicableSync').mockRejectedValue(new Error(500));
Zack Cuddy's avatar
Zack Cuddy committed
541 542 543 544
      });

      it('should dispatch the request and error actions', done => {
        testAction(
545
          actions.initiateReplicableSync,
Zack Cuddy's avatar
Zack Cuddy committed
546 547 548 549
          { projectId, name, action },
          state,
          [],
          [
550
            { type: 'requestInitiateReplicableSync' },
Zack Cuddy's avatar
Zack Cuddy committed
551
            {
552
              type: 'receiveInitiateReplicableSyncError',
Zack Cuddy's avatar
Zack Cuddy committed
553 554 555 556 557 558 559 560 561
              payload: { name: 'test' },
            },
          ],
          done,
        );
      });
    });
  });

Zack Cuddy's avatar
Zack Cuddy committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  describe('setFilter', () => {
    it('should commit mutation SET_FILTER', done => {
      const testValue = 1;

      testAction(
        actions.setFilter,
        testValue,
        state,
        [{ type: types.SET_FILTER, payload: testValue }],
        [],
        done,
      );
    });
  });

  describe('setSearch', () => {
    it('should commit mutation SET_SEARCH', done => {
      const testValue = 'Test Search';

      testAction(
        actions.setSearch,
        testValue,
        state,
        [{ type: types.SET_SEARCH, payload: testValue }],
        [],
        done,
      );
    });
  });

592 593
  describe('setPage', () => {
    it('should commit mutation SET_PAGE', done => {
594
      state.paginationData.page = 1;
Zack Cuddy's avatar
Zack Cuddy committed
595 596 597 598 599 600 601 602 603 604 605

      const testValue = 2;

      testAction(
        actions.setPage,
        testValue,
        state,
        [{ type: types.SET_PAGE, payload: testValue }],
        [],
        done,
      );
606 607 608
    });
  });
});