summaryrefslogtreecommitdiff
path: root/spec/frontend/user_lists/store/index/mutations_spec.js
blob: 18d6a9b8f389ee34415a2148a814a78899f0b10b (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { parseIntPagination, normalizeHeaders } from '~/lib/utils/common_utils';
import * as types from '~/user_lists/store/index/mutation_types';
import mutations from '~/user_lists/store/index/mutations';
import createState from '~/user_lists/store/index/state';
import { userList } from 'jest/feature_flags/mock_data';

describe('~/user_lists/store/index/mutations', () => {
  let state;

  beforeEach(() => {
    state = createState({ projectId: '1' });
  });

  describe('SET_USER_LISTS_OPTIONS', () => {
    it('should set provided options', () => {
      mutations[types.SET_USER_LISTS_OPTIONS](state, { page: '1', scope: 'all' });

      expect(state.options).toEqual({ page: '1', scope: 'all' });
    });
  });

  describe('REQUEST_USER_LISTS', () => {
    it('sets isLoading to true', () => {
      mutations[types.REQUEST_USER_LISTS](state);
      expect(state.isLoading).toBe(true);
    });
  });

  describe('RECEIVE_USER_LISTS_SUCCESS', () => {
    const headers = {
      'x-next-page': '2',
      'x-page': '1',
      'X-Per-Page': '2',
      'X-Prev-Page': '',
      'X-TOTAL': '37',
      'X-Total-Pages': '5',
    };

    beforeEach(() => {
      mutations[types.RECEIVE_USER_LISTS_SUCCESS](state, { data: [userList], headers });
    });

    it('sets isLoading to false', () => {
      expect(state.isLoading).toBe(false);
    });

    it('sets userLists to the received userLists', () => {
      expect(state.userLists).toEqual([userList]);
    });

    it('sets pagination info for user lits', () => {
      expect(state.pageInfo).toEqual(parseIntPagination(normalizeHeaders(headers)));
    });

    it('sets the count for user lists', () => {
      expect(state.count).toBe(parseInt(headers['X-TOTAL'], 10));
    });
  });

  describe('RECEIVE_USER_LISTS_ERROR', () => {
    beforeEach(() => {
      mutations[types.RECEIVE_USER_LISTS_ERROR](state);
    });

    it('should set isLoading to false', () => {
      expect(state.isLoading).toEqual(false);
    });

    it('should set hasError to true', () => {
      expect(state.hasError).toEqual(true);
    });
  });

  describe('REQUEST_DELETE_USER_LIST', () => {
    beforeEach(() => {
      state.userLists = [userList];
      mutations[types.REQUEST_DELETE_USER_LIST](state, userList);
    });

    it('should remove the deleted list', () => {
      expect(state.userLists).not.toContain(userList);
    });
  });

  describe('RECEIVE_DELETE_USER_LIST_ERROR', () => {
    beforeEach(() => {
      state.userLists = [];
      mutations[types.RECEIVE_DELETE_USER_LIST_ERROR](state, {
        list: userList,
        error: 'some error',
      });
    });

    it('should set isLoading to false and hasError to false', () => {
      expect(state.isLoading).toBe(false);
      expect(state.hasError).toBe(false);
    });

    it('should add the user list back to the list of user lists', () => {
      expect(state.userLists).toContain(userList);
    });
  });

  describe('RECEIVE_CLEAR_ALERT', () => {
    it('clears the alert', () => {
      state.alerts = ['a server error'];

      mutations[types.RECEIVE_CLEAR_ALERT](state, 0);

      expect(state.alerts).toEqual([]);
    });

    it('clears the alert at the specified index', () => {
      state.alerts = ['a server error', 'another error', 'final error'];

      mutations[types.RECEIVE_CLEAR_ALERT](state, 1);

      expect(state.alerts).toEqual(['a server error', 'final error']);
    });
  });
});