summaryrefslogtreecommitdiff
path: root/spec/frontend/members/components/members_tabs_spec.js
blob: 28614b52706771b0e7fed06c1713438507a97f81 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import Vue, { nextTick } from 'vue';
import Vuex from 'vuex';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import MembersApp from '~/members/components/app.vue';
import MembersTabs from '~/members/components/members_tabs.vue';
import { MEMBER_TYPES } from '~/members/constants';
import { pagination } from '../mock_data';

describe('MembersApp', () => {
  Vue.use(Vuex);

  let wrapper;

  const createComponent = ({ totalItems = 10, options = {} } = {}) => {
    const store = new Vuex.Store({
      modules: {
        [MEMBER_TYPES.user]: {
          namespaced: true,
          state: {
            pagination: {
              ...pagination,
              totalItems,
            },
            filteredSearchBar: {
              searchParam: 'search',
            },
          },
        },
        [MEMBER_TYPES.group]: {
          namespaced: true,
          state: {
            pagination: {
              ...pagination,
              totalItems,
              paramName: 'groups_page',
            },
            filteredSearchBar: {
              searchParam: 'search_groups',
            },
          },
        },
        [MEMBER_TYPES.invite]: {
          namespaced: true,
          state: {
            pagination: {
              ...pagination,
              totalItems,
              paramName: 'invited_page',
            },
            filteredSearchBar: {
              searchParam: 'search_invited',
            },
          },
        },
        [MEMBER_TYPES.accessRequest]: {
          namespaced: true,
          state: {
            pagination: {
              ...pagination,
              totalItems,
              paramName: 'access_requests_page',
            },
            filteredSearchBar: {
              searchParam: 'search_access_requests',
            },
          },
        },
      },
    });

    wrapper = mountExtended(MembersTabs, {
      store,
      stubs: ['members-app'],
      provide: {
        canManageMembers: true,
      },
      ...options,
    });

    return nextTick();
  };

  const findTabs = () => wrapper.findAllByRole('tab').wrappers;
  const findTabByText = (text) => findTabs().find((tab) => tab.text().includes(text));
  const findActiveTab = () => wrapper.findByRole('tab', { selected: true });

  beforeEach(() => {
    delete window.location;
    window.location = new URL('https://localhost');
  });

  afterEach(() => {
    wrapper.destroy();
  });

  describe('when tabs have a count', () => {
    it('renders tabs with count', async () => {
      await createComponent();

      const tabs = findTabs();

      expect(tabs[0].text()).toBe('Members  10');
      expect(tabs[1].text()).toBe('Groups  10');
      expect(tabs[2].text()).toBe('Invited  10');
      expect(tabs[3].text()).toBe('Access requests  10');
      expect(findActiveTab().text()).toContain('Members');
    });

    it('renders `MembersApp` and passes `namespace` prop', async () => {
      await createComponent();

      const membersApps = wrapper.findAllComponents(MembersApp).wrappers;

      expect(membersApps[0].attributes('namespace')).toBe(MEMBER_TYPES.user);
      expect(membersApps[1].attributes('namespace')).toBe(MEMBER_TYPES.group);
      expect(membersApps[2].attributes('namespace')).toBe(MEMBER_TYPES.invite);
      expect(membersApps[3].attributes('namespace')).toBe(MEMBER_TYPES.accessRequest);
    });
  });

  describe('when tabs do not have a count', () => {
    it('only renders `Members` tab', async () => {
      await createComponent({ totalItems: 0 });

      expect(findTabByText('Members')).not.toBeUndefined();
      expect(findTabByText('Groups')).toBeUndefined();
      expect(findTabByText('Invited')).toBeUndefined();
      expect(findTabByText('Access requests')).toBeUndefined();
    });
  });

  describe('when url param matches `filteredSearchBar.searchParam`', () => {
    beforeEach(() => {
      window.location.search = '?search_groups=foo+bar';
    });

    const expectGroupsTabActive = () => {
      expect(findActiveTab().text()).toContain('Groups');
    };

    describe('when tab has a count', () => {
      it('sets tab that corresponds to search param as active tab', async () => {
        await createComponent();

        expectGroupsTabActive();
      });
    });

    describe('when tab does not have a count', () => {
      it('sets tab that corresponds to search param as active tab', async () => {
        await createComponent({ totalItems: 0 });

        expectGroupsTabActive();
      });
    });
  });

  describe('when url param matches `pagination.paramName`', () => {
    beforeEach(() => {
      window.location.search = '?invited_page=2';
    });

    const expectInvitedTabActive = () => {
      expect(findActiveTab().text()).toContain('Invited');
    };

    describe('when tab has a count', () => {
      it('sets tab that corresponds to pagination param as active tab', async () => {
        await createComponent();

        expectInvitedTabActive();
      });
    });

    describe('when tab does not have a count', () => {
      it('sets tab that corresponds to pagination param as active tab', async () => {
        await createComponent({ totalItems: 0 });

        expectInvitedTabActive();
      });
    });
  });

  describe('when `canManageMembers` is `false`', () => {
    it('shows all tabs except `Invited` and `Access requests`', async () => {
      await createComponent({ options: { provide: { canManageMembers: false } } });

      expect(findTabByText('Members')).not.toBeUndefined();
      expect(findTabByText('Groups')).not.toBeUndefined();
      expect(findTabByText('Invited')).toBeUndefined();
      expect(findTabByText('Access requests')).toBeUndefined();
    });
  });
});