summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_shared/components/user_avatar/user_avatar_list_spec.js
blob: 64aa7e297184c256e4002bd41ede56e1ad9b440f (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
import { shallowMount, createLocalVue } from '@vue/test-utils';
import { GlButton } from '@gitlab/ui';
import { TEST_HOST } from 'spec/test_constants';
import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';

const TEST_IMAGE_SIZE = 7;
const TEST_BREAKPOINT = 5;

const createUser = id => ({
  id,
  name: 'Lorem',
  web_url: `${TEST_HOST}/${id}`,
  avatar_url: `${TEST_HOST}/${id}/avatar`,
});
const createList = n =>
  Array(n)
    .fill(1)
    .map((x, id) => createUser(id));

const localVue = createLocalVue();

describe('UserAvatarList', () => {
  let propsData;
  let wrapper;

  const factory = options => {
    wrapper = shallowMount(localVue.extend(UserAvatarList), {
      localVue,
      propsData,
      ...options,
    });
  };

  const clickButton = () => {
    const button = wrapper.find(GlButton);
    button.vm.$emit('click');
  };

  beforeEach(() => {
    propsData = { imgSize: TEST_IMAGE_SIZE };
  });

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

  describe('with no breakpoint', () => {
    beforeEach(() => {
      propsData.breakpoint = 0;
    });

    it('renders avatars', () => {
      const items = createList(20);
      propsData.items = items;
      factory();

      const links = wrapper.findAll(UserAvatarLink);
      const linkProps = links.wrappers.map(x => x.props());

      expect(linkProps).toEqual(
        propsData.items.map(x =>
          jasmine.objectContaining({
            linkHref: x.web_url,
            imgSrc: x.avatar_url,
            imgAlt: x.name,
            tooltipText: x.name,
            imgSize: TEST_IMAGE_SIZE,
          }),
        ),
      );
    });
  });

  describe('with breakpoint and length equal to breakpoint', () => {
    beforeEach(() => {
      propsData.breakpoint = TEST_BREAKPOINT;
      propsData.items = createList(TEST_BREAKPOINT);
    });

    it('renders all avatars if length is <= breakpoint', () => {
      factory();

      const links = wrapper.findAll(UserAvatarLink);

      expect(links.length).toEqual(propsData.items.length);
    });

    it('does not show button', () => {
      factory();

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

  describe('with breakpoint and length greater than breakpoint', () => {
    beforeEach(() => {
      propsData.breakpoint = TEST_BREAKPOINT;
      propsData.items = createList(TEST_BREAKPOINT + 1);
    });

    it('renders avatars up to breakpoint', () => {
      factory();

      const links = wrapper.findAll(UserAvatarLink);

      expect(links.length).toEqual(TEST_BREAKPOINT);
    });

    describe('with expand clicked', () => {
      beforeEach(() => {
        factory();
        clickButton();
      });

      it('renders all avatars', () => {
        const links = wrapper.findAll(UserAvatarLink);

        expect(links.length).toEqual(propsData.items.length);
      });

      it('with collapse clicked, it renders avatars up to breakpoint', () => {
        clickButton();
        const links = wrapper.findAll(UserAvatarLink);

        expect(links.length).toEqual(TEST_BREAKPOINT);
      });
    });
  });
});