summaryrefslogtreecommitdiff
path: root/spec/frontend/sidebar/participants_spec.js
blob: 1210f7c95313093e1f750a117582495175c74755 (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
195
196
197
198
199
200
201
202
203
204
205
206
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Participants from '~/sidebar/components/participants/participants.vue';

const PARTICIPANT = {
  id: 1,
  state: 'active',
  username: 'marcene',
  name: 'Allie Will',
  web_url: 'foo.com',
  avatar_url: 'gravatar.com/avatar/xxx',
};

const PARTICIPANT_LIST = [PARTICIPANT, { ...PARTICIPANT, id: 2 }, { ...PARTICIPANT, id: 3 }];

describe('Participants', () => {
  let wrapper;

  const getMoreParticipantsButton = () => wrapper.find('button');

  const getCollapsedParticipantsCount = () => wrapper.find('[data-testid="collapsed-count"]');

  const mountComponent = (propsData) =>
    shallowMount(Participants, {
      propsData,
    });

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

  describe('collapsed sidebar state', () => {
    it('shows loading spinner when loading', () => {
      wrapper = mountComponent({
        loading: true,
      });

      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });

    it('does not show loading spinner not loading', () => {
      wrapper = mountComponent({
        loading: false,
      });

      expect(wrapper.find(GlLoadingIcon).exists()).toBe(false);
    });

    it('shows participant count when given', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
      });

      expect(getCollapsedParticipantsCount().text()).toBe(`${PARTICIPANT_LIST.length}`);
    });

    it('shows full participant count when there are hidden participants', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants: 1,
      });

      expect(getCollapsedParticipantsCount().text()).toBe(`${PARTICIPANT_LIST.length}`);
    });
  });

  describe('expanded sidebar state', () => {
    it('shows loading spinner when loading', () => {
      wrapper = mountComponent({
        loading: true,
      });

      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
    });

    it('when only showing visible participants, shows an avatar only for each participant under the limit', () => {
      const numberOfLessParticipants = 2;
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants,
      });

      wrapper.setData({
        isShowingMoreParticipants: false,
      });

      return Vue.nextTick().then(() => {
        expect(wrapper.findAll('.participants-author')).toHaveLength(numberOfLessParticipants);
      });
    });

    it('when only showing all participants, each has an avatar', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants: 2,
      });

      wrapper.setData({
        isShowingMoreParticipants: true,
      });

      return Vue.nextTick().then(() => {
        expect(wrapper.findAll('.participants-author')).toHaveLength(PARTICIPANT_LIST.length);
      });
    });

    it('does not have more participants link when they can all be shown', () => {
      const numberOfLessParticipants = 100;
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants,
      });

      expect(PARTICIPANT_LIST.length).toBeLessThan(numberOfLessParticipants);
      expect(getMoreParticipantsButton().exists()).toBe(false);
    });

    it('when too many participants, has more participants link to show more', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants: 2,
      });

      wrapper.setData({
        isShowingMoreParticipants: false,
      });

      return Vue.nextTick().then(() => {
        expect(getMoreParticipantsButton().text()).toBe('+ 1 more');
      });
    });

    it('when too many participants and already showing them, has more participants link to show less', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants: 2,
      });

      wrapper.setData({
        isShowingMoreParticipants: true,
      });

      return Vue.nextTick().then(() => {
        expect(getMoreParticipantsButton().text()).toBe('- show less');
      });
    });

    it('clicking more participants link emits event', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants: 2,
      });

      expect(wrapper.vm.isShowingMoreParticipants).toBe(false);

      getMoreParticipantsButton().trigger('click');

      expect(wrapper.vm.isShowingMoreParticipants).toBe(true);
    });

    it('clicking on participants icon emits `toggleSidebar` event', () => {
      wrapper = mountComponent({
        loading: false,
        participants: PARTICIPANT_LIST,
        numberOfLessParticipants: 2,
      });

      const spy = jest.spyOn(wrapper.vm, '$emit');

      wrapper.find('.sidebar-collapsed-icon').trigger('click');

      return Vue.nextTick(() => {
        expect(spy).toHaveBeenCalledWith('toggleSidebar');

        spy.mockRestore();
      });
    });
  });

  describe('when not showing participants label', () => {
    beforeEach(() => {
      wrapper = mountComponent({
        participants: PARTICIPANT_LIST,
        showParticipantLabel: false,
      });
    });

    it('does not show sidebar collapsed icon', () => {
      expect(wrapper.find('.sidebar-collapsed-icon').exists()).toBe(false);
    });

    it('does not show participants label title', () => {
      expect(wrapper.find('.title').exists()).toBe(false);
    });
  });
});