summaryrefslogtreecommitdiff
path: root/spec/frontend/frequent_items/store/mutations_spec.js
blob: 1e1878c3377252dea4b2103e5107b865b2ba44ca (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
122
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
import * as types from '~/frequent_items/store/mutation_types';
import mutations from '~/frequent_items/store/mutations';
import state from '~/frequent_items/store/state';
import {
  mockNamespace,
  mockStorageKey,
  mockFrequentProjects,
  mockSearchedProjects,
  mockProcessedSearchedProjects,
  mockSearchedGroups,
  mockProcessedSearchedGroups,
} from '../mock_data';

describe('Frequent Items dropdown mutations', () => {
  let stateCopy;

  beforeEach(() => {
    stateCopy = state();
  });

  describe('SET_NAMESPACE', () => {
    it('should set namespace', () => {
      mutations[types.SET_NAMESPACE](stateCopy, mockNamespace);

      expect(stateCopy.namespace).toEqual(mockNamespace);
    });
  });

  describe('SET_STORAGE_KEY', () => {
    it('should set storage key', () => {
      mutations[types.SET_STORAGE_KEY](stateCopy, mockStorageKey);

      expect(stateCopy.storageKey).toEqual(mockStorageKey);
    });
  });

  describe('SET_SEARCH_QUERY', () => {
    it('should set search query', () => {
      const searchQuery = 'gitlab-ce';

      mutations[types.SET_SEARCH_QUERY](stateCopy, searchQuery);

      expect(stateCopy.searchQuery).toEqual(searchQuery);
    });
  });

  describe('TOGGLE_ITEMS_LIST_EDITABILITY', () => {
    it('should toggle items list editablity', () => {
      mutations[types.TOGGLE_ITEMS_LIST_EDITABILITY](stateCopy);

      expect(stateCopy.isItemsListEditable).toEqual(true);

      mutations[types.TOGGLE_ITEMS_LIST_EDITABILITY](stateCopy);

      expect(stateCopy.isItemsListEditable).toEqual(false);
    });
  });

  describe('REQUEST_FREQUENT_ITEMS', () => {
    it('should set view states when requesting frequent items', () => {
      mutations[types.REQUEST_FREQUENT_ITEMS](stateCopy);

      expect(stateCopy.isLoadingItems).toEqual(true);
      expect(stateCopy.hasSearchQuery).toEqual(false);
    });
  });

  describe('RECEIVE_FREQUENT_ITEMS_SUCCESS', () => {
    it('should set view states when receiving frequent items', () => {
      mutations[types.RECEIVE_FREQUENT_ITEMS_SUCCESS](stateCopy, mockFrequentProjects);

      expect(stateCopy.items).toEqual(mockFrequentProjects);
      expect(stateCopy.isLoadingItems).toEqual(false);
      expect(stateCopy.hasSearchQuery).toEqual(false);
      expect(stateCopy.isFetchFailed).toEqual(false);
    });
  });

  describe('RECEIVE_FREQUENT_ITEMS_ERROR', () => {
    it('should set items and view states when error occurs retrieving frequent items', () => {
      mutations[types.RECEIVE_FREQUENT_ITEMS_ERROR](stateCopy);

      expect(stateCopy.items).toEqual([]);
      expect(stateCopy.isLoadingItems).toEqual(false);
      expect(stateCopy.hasSearchQuery).toEqual(false);
      expect(stateCopy.isFetchFailed).toEqual(true);
    });
  });

  describe('REQUEST_SEARCHED_ITEMS', () => {
    it('should set view states when requesting searched items', () => {
      mutations[types.REQUEST_SEARCHED_ITEMS](stateCopy);

      expect(stateCopy.isLoadingItems).toEqual(true);
      expect(stateCopy.hasSearchQuery).toEqual(true);
    });
  });

  describe('RECEIVE_SEARCHED_ITEMS_SUCCESS', () => {
    it('should set items and view states when receiving searched items', () => {
      mutations[types.RECEIVE_SEARCHED_ITEMS_SUCCESS](stateCopy, mockSearchedProjects);

      expect(stateCopy.items).toEqual(mockProcessedSearchedProjects);
      expect(stateCopy.isLoadingItems).toEqual(false);
      expect(stateCopy.hasSearchQuery).toEqual(true);
      expect(stateCopy.isFetchFailed).toEqual(false);
    });

    it('should also handle the different `full_name` key for namespace in groups payload', () => {
      mutations[types.RECEIVE_SEARCHED_ITEMS_SUCCESS](stateCopy, mockSearchedGroups);

      expect(stateCopy.items).toEqual(mockProcessedSearchedGroups);
      expect(stateCopy.isLoadingItems).toEqual(false);
      expect(stateCopy.hasSearchQuery).toEqual(true);
      expect(stateCopy.isFetchFailed).toEqual(false);
    });
  });

  describe('RECEIVE_SEARCHED_ITEMS_ERROR', () => {
    it('should set view states when error occurs retrieving searched items', () => {
      mutations[types.RECEIVE_SEARCHED_ITEMS_ERROR](stateCopy);

      expect(stateCopy.items).toEqual([]);
      expect(stateCopy.isLoadingItems).toEqual(false);
      expect(stateCopy.hasSearchQuery).toEqual(true);
      expect(stateCopy.isFetchFailed).toEqual(true);
    });
  });

  describe('RECEIVE_REMOVE_FREQUENT_ITEM_SUCCESS', () => {
    it('should remove item with provided itemId from the items', () => {
      stateCopy.isItemRemovalFailed = true;
      stateCopy.items = mockFrequentProjects;

      mutations[types.RECEIVE_REMOVE_FREQUENT_ITEM_SUCCESS](stateCopy, mockFrequentProjects[0].id);

      expect(stateCopy.items).toHaveLength(mockFrequentProjects.length - 1);
      expect(stateCopy.items).toEqual([...mockFrequentProjects.slice(1)]);
      expect(stateCopy.isItemRemovalFailed).toBe(false);
    });
  });

  describe('RECEIVE_REMOVE_FREQUENT_ITEM_ERROR', () => {
    it('should remove item with provided itemId from the items', () => {
      stateCopy.isItemRemovalFailed = false;

      mutations[types.RECEIVE_REMOVE_FREQUENT_ITEM_ERROR](stateCopy);

      expect(stateCopy.isItemRemovalFailed).toBe(true);
    });
  });
});