summaryrefslogtreecommitdiff
path: root/spec/frontend/groups/store/groups_store_spec.js
blob: 8ac5d7099f15d3afd96d101d1857e0274008bac8 (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
import GroupsStore from '~/groups/store/groups_store';
import { getGroupItemMicrodata } from '~/groups/store/utils';
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).toBeFalsy();

      store = new GroupsStore({ hideProjects: true });

      expect(store.hideProjects).toBeTruthy();
    });
  });

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

      store.setGroups(mockGroups);

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

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

      store.setSearchedGroups(mockSearchedGroups);

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

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

      store.setGroupChildren(mockParentGroupItem, mockRawChildren);

      expect(store.formatGroupItem).toHaveBeenCalledWith(expect.any(Object));
      expect(mockParentGroupItem.children.length).toBe(1);
      expect(Object.keys(mockParentGroupItem.children[0]).indexOf('fullName')).toBeGreaterThan(-1);
      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', () => {
      const store = new GroupsStore();
      const updatedGroupItem = store.formatGroupItem(mockRawChildren[0]);

      expect(Object.keys(updatedGroupItem).indexOf('fullName')).toBeGreaterThan(-1);
      expect(updatedGroupItem.childrenCount).toBe(mockRawChildren[0].children_count);
      expect(updatedGroupItem.isChildrenLoading).toBe(false);
      expect(updatedGroupItem.isBeingRemoved).toBe(false);
      expect(updatedGroupItem.microdata).toEqual({});
    });

    it('with hideProjects', () => {
      const store = new GroupsStore({ hideProjects: true });
      const updatedGroupItem = store.formatGroupItem(mockRawChildren[0]);

      expect(Object.keys(updatedGroupItem).indexOf('fullName')).toBeGreaterThan(-1);
      expect(updatedGroupItem.childrenCount).toBe(mockRawChildren[0].subgroup_count);
      expect(updatedGroupItem.microdata).toEqual({});
    });

    it('with showSchemaMarkup', () => {
      const store = new GroupsStore({ showSchemaMarkup: true });
      const updatedGroupItem = store.formatGroupItem(mockRawChildren[0]);

      expect(updatedGroupItem.microdata).toEqual(getGroupItemMicrodata(mockRawChildren[0]));
    });
  });

  describe('removeGroup', () => {
    it('should remove children from group item in state', () => {
      const store = new GroupsStore();
      const rawParentGroup = { ...mockGroups[0] };
      const rawChildGroup = { ...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);
    });
  });
});