summaryrefslogtreecommitdiff
path: root/spec/frontend/packages/list/stores/mutations_spec.js
blob: 743de595eb5306941d3fa329b6044c9696eb9823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import mutations from '~/packages/list/stores/mutations';
import * as types from '~/packages/list/stores/mutation_types';
import createState from '~/packages/list/stores/state';
import * as commonUtils from '~/lib/utils/common_utils';
import { npmPackage, mavenPackage } from '../../mock_data';

describe('Mutations Registry Store', () => {
  let mockState;
  beforeEach(() => {
    mockState = createState();
  });

  describe('SET_INITIAL_STATE', () => {
    it('should set the initial state', () => {
      const config = {
        resourceId: '1',
        pageType: 'groups',
        userCanDelete: '',
        emptyListIllustration: 'foo',
        emptyListHelpUrl: 'baz',
      };

      const expectedState = {
        ...mockState,
        config: {
          ...config,
          isGroupPage: true,
          canDestroyPackage: true,
        },
      };
      mutations[types.SET_INITIAL_STATE](mockState, config);

      expect(mockState.projectId).toEqual(expectedState.projectId);
    });
  });

  describe('SET_PACKAGE_LIST_SUCCESS', () => {
    it('should set a packages list', () => {
      const payload = [npmPackage, mavenPackage];
      const expectedState = { ...mockState, packages: payload };
      mutations[types.SET_PACKAGE_LIST_SUCCESS](mockState, payload);

      expect(mockState.packages).toEqual(expectedState.packages);
    });
  });

  describe('SET_MAIN_LOADING', () => {
    it('should set main loading', () => {
      mutations[types.SET_MAIN_LOADING](mockState, true);

      expect(mockState.isLoading).toEqual(true);
    });
  });

  describe('SET_PAGINATION', () => {
    const mockPagination = { perPage: 10, page: 1 };
    beforeEach(() => {
      commonUtils.normalizeHeaders = jest.fn().mockReturnValue('baz');
      commonUtils.parseIntPagination = jest.fn().mockReturnValue(mockPagination);
    });
    it('should set a parsed pagination', () => {
      mutations[types.SET_PAGINATION](mockState, 'foo');
      expect(commonUtils.normalizeHeaders).toHaveBeenCalledWith('foo');
      expect(commonUtils.parseIntPagination).toHaveBeenCalledWith('baz');
      expect(mockState.pagination).toEqual(mockPagination);
    });
  });

  describe('SET_SORTING', () => {
    it('should merge the sorting object with sort value', () => {
      mutations[types.SET_SORTING](mockState, { sort: 'desc' });
      expect(mockState.sorting).toEqual({ ...mockState.sorting, sort: 'desc' });
    });

    it('should merge the sorting object with order_by value', () => {
      mutations[types.SET_SORTING](mockState, { orderBy: 'foo' });
      expect(mockState.sorting).toEqual({ ...mockState.sorting, orderBy: 'foo' });
    });
  });

  describe('SET_FILTER', () => {
    it('should set the filter query', () => {
      mutations[types.SET_FILTER](mockState, 'foo');
      expect(mockState.filter).toEqual('foo');
    });
  });
});