summaryrefslogtreecommitdiff
path: root/spec/javascripts/groups/store/groups_store_spec.js
blob: d74f38f476e9c58f580838f7168a9ab06362ceb6 (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
import GroupsStore from '~/groups/store/groups_store';
import {
  mockGroups, mockSearchedGroups,
  mockParentGroupItem, mockRawChildren,
  mockRawPageInfo,
} from '../mock_data';

describe('ProjectsStore', () => {
  describe('constructor', () => {
    it('should initialize default state', () => {
      let store;

      store = new GroupsStore();
      expect(Object.keys(store.state).length).toBe(2);
      expect(Array.isArray(store.state.groups)).toBeTruthy();
      expect(Object.keys(store.state.pageInfo).length).toBe(0);
      expect(store.hideProjects).not.toBeDefined();

      store = new GroupsStore(true);
      expect(store.hideProjects).toBeTruthy();
    });
  });

  describe('setGroups', () => {
    it('should set groups to state', () => {
      const store = new GroupsStore();
      spyOn(store, 'formatGroupItem').and.callThrough();

      store.setGroups(mockGroups);
      expect(store.state.groups.length).toBe(mockGroups.length);
      expect(store.formatGroupItem).toHaveBeenCalledWith(jasmine.any(Object));
      expect(Object.keys(store.state.groups[0]).indexOf('fullName') > -1).toBeTruthy();
    });
  });

  describe('setSearchedGroups', () => {
    it('should set searched groups to state', () => {
      const store = new GroupsStore();
      spyOn(store, 'formatGroupItem').and.callThrough();

      store.setSearchedGroups(mockSearchedGroups);
      expect(store.state.groups.length).toBe(mockSearchedGroups.length);
      expect(store.formatGroupItem).toHaveBeenCalledWith(jasmine.any(Object));
      expect(Object.keys(store.state.groups[0]).indexOf('fullName') > -1).toBeTruthy();
      expect(Object.keys(store.state.groups[0].children[0]).indexOf('fullName') > -1).toBeTruthy();
    });
  });

  describe('setGroupChildren', () => {
    it('should set children to group item in state', () => {
      const store = new GroupsStore();
      spyOn(store, 'formatGroupItem').and.callThrough();

      store.setGroupChildren(mockParentGroupItem, mockRawChildren);
      expect(store.formatGroupItem).toHaveBeenCalledWith(jasmine.any(Object));
      expect(mockParentGroupItem.children.length).toBe(1);
      expect(Object.keys(mockParentGroupItem.children[0]).indexOf('fullName') > -1).toBeTruthy();
      expect(mockParentGroupItem.isOpen).toBeTruthy();
      expect(mockParentGroupItem.isChildrenLoading).toBeFalsy();
    });
  });

  describe('setPaginationInfo', () => {
    it('should parse and set pagination info in state', () => {
      const store = new GroupsStore();

      store.setPaginationInfo(mockRawPageInfo);
      expect(store.state.pageInfo.perPage).toBe(10);
      expect(store.state.pageInfo.page).toBe(10);
      expect(store.state.pageInfo.total).toBe(10);
      expect(store.state.pageInfo.totalPages).toBe(10);
      expect(store.state.pageInfo.nextPage).toBe(10);
      expect(store.state.pageInfo.previousPage).toBe(10);
    });
  });

  describe('formatGroupItem', () => {
    it('should parse group item object and return updated object', () => {
      let store;
      let updatedGroupItem;

      store = new GroupsStore();
      updatedGroupItem = store.formatGroupItem(mockRawChildren[0]);
      expect(Object.keys(updatedGroupItem).indexOf('fullName') > -1).toBeTruthy();
      expect(updatedGroupItem.childrenCount).toBe(mockRawChildren[0].children_count);
      expect(updatedGroupItem.isChildrenLoading).toBe(false);
      expect(updatedGroupItem.isBeingRemoved).toBe(false);

      store = new GroupsStore(true);
      updatedGroupItem = store.formatGroupItem(mockRawChildren[0]);
      expect(Object.keys(updatedGroupItem).indexOf('fullName') > -1).toBeTruthy();
      expect(updatedGroupItem.childrenCount).toBe(mockRawChildren[0].subgroup_count);
    });
  });

  describe('removeGroup', () => {
    it('should remove children from group item in state', () => {
      const store = new GroupsStore();
      const rawParentGroup = Object.assign({}, mockGroups[0]);
      const rawChildGroup = Object.assign({}, mockGroups[1]);

      store.setGroups([rawParentGroup]);
      store.setGroupChildren(store.state.groups[0], [rawChildGroup]);
      const childItem = store.state.groups[0].children[0];

      store.removeGroup(childItem, store.state.groups[0]);
      expect(store.state.groups[0].children.length).toBe(0);
    });
  });
});