summaryrefslogtreecommitdiff
path: root/spec/frontend/diffs/components/diff_gutter_avatars_spec.js
blob: 48ee5c63f35186cb3a59beda7efca73b2cd10495 (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
import { shallowMount, createLocalVue } from '@vue/test-utils';
import DiffGutterAvatars from '~/diffs/components/diff_gutter_avatars.vue';
import discussionsMockData from '../mock_data/diff_discussions';

const localVue = createLocalVue();
const getDiscussionsMockData = () => [Object.assign({}, discussionsMockData)];

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

  const findCollapseButton = () => wrapper.find('.diff-notes-collapse');
  const findMoreCount = () => wrapper.find('.diff-comments-more-count');
  const findUserAvatars = () => wrapper.findAll('.diff-comment-avatar');

  const createComponent = (props = {}) => {
    wrapper = shallowMount(DiffGutterAvatars, {
      localVue,
      sync: false,
      propsData: {
        ...props,
      },
    });
  };

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

  describe('when expanded', () => {
    beforeEach(() => {
      createComponent({
        discussions: getDiscussionsMockData(),
        discussionsExpanded: true,
      });
    });

    it('renders a collapse button when discussions are expanded', () => {
      expect(findCollapseButton().exists()).toBe(true);
    });

    it('should emit toggleDiscussions event on button click', () => {
      findCollapseButton().trigger('click');

      expect(wrapper.emitted().toggleLineDiscussions).toBeTruthy();
    });
  });

  describe('when collapsed', () => {
    beforeEach(() => {
      createComponent({
        discussions: getDiscussionsMockData(),
        discussionsExpanded: false,
      });
    });

    it('renders user avatars and moreCount text', () => {
      expect(findUserAvatars().exists()).toBe(true);
      expect(findMoreCount().exists()).toBe(true);
    });

    it('renders correct amount of user avatars', () => {
      expect(findUserAvatars().length).toBe(3);
    });

    it('renders correct moreCount number', () => {
      expect(findMoreCount().text()).toBe('+2');
    });

    it('should emit toggleDiscussions event on avatars click', () => {
      findUserAvatars()
        .at(0)
        .trigger('click');

      expect(wrapper.emitted().toggleLineDiscussions).toBeTruthy();
    });

    it('should emit toggleDiscussions event on more count text click', () => {
      findMoreCount().trigger('click');

      expect(wrapper.emitted().toggleLineDiscussions).toBeTruthy();
    });
  });

  it('renders an empty more count string if there are no discussions', () => {
    createComponent({
      discussions: [],
      discussionsExpanded: false,
    });

    expect(findMoreCount().exists()).toBe(false);
  });

  describe('tooltip text', () => {
    beforeEach(() => {
      createComponent({
        discussions: getDiscussionsMockData(),
        discussionsExpanded: false,
      });
    });

    it('returns original comment if it is shorter than max length', () => {
      const note = wrapper.vm.discussions[0].notes[0];

      expect(wrapper.vm.getTooltipText(note)).toEqual('Administrator: comment 1');
    });

    it('returns truncated version of comment if it is longer than max length', () => {
      const note = wrapper.vm.discussions[0].notes[1];

      expect(wrapper.vm.getTooltipText(note)).toEqual('Fatih Acet: comment 2 is r...');
    });
  });
});