summaryrefslogtreecommitdiff
path: root/spec/frontend/notes/components/note_awards_list_spec.js
blob: 5ab183e5452eb462e53c9be98b07be902b8c484a (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
import Vue from 'vue';
import AxiosMockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'jest/helpers/test_constants';
import axios from '~/lib/utils/axios_utils';
import createStore from '~/notes/stores';
import awardsNote from '~/notes/components/note_awards_list.vue';
import { noteableDataMock, notesDataMock } from '../mock_data';

describe('note_awards_list component', () => {
  let store;
  let vm;
  let awardsMock;
  let mock;

  const toggleAwardPath = `${TEST_HOST}/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji`;

  beforeEach(() => {
    mock = new AxiosMockAdapter(axios);

    mock.onPost(toggleAwardPath).reply(200, '');

    const Component = Vue.extend(awardsNote);

    store = createStore();
    store.dispatch('setNoteableData', noteableDataMock);
    store.dispatch('setNotesData', notesDataMock);
    awardsMock = [
      {
        name: 'flag_tz',
        user: { id: 1, name: 'Administrator', username: 'root' },
      },
      {
        name: 'cartwheel_tone3',
        user: { id: 12, name: 'Bobbie Stehr', username: 'erin' },
      },
    ];

    vm = new Component({
      store,
      propsData: {
        awards: awardsMock,
        noteAuthorId: 2,
        noteId: '545',
        canAwardEmoji: true,
        toggleAwardPath,
      },
    }).$mount();
  });

  afterEach(() => {
    mock.restore();
    vm.$destroy();
  });

  it('should render awarded emojis', () => {
    expect(vm.$el.querySelector('.js-awards-block button [data-name="flag_tz"]')).toBeDefined();
    expect(
      vm.$el.querySelector('.js-awards-block button [data-name="cartwheel_tone3"]'),
    ).toBeDefined();
  });

  it('should be possible to remove awarded emoji', () => {
    jest.spyOn(vm, 'handleAward');
    jest.spyOn(vm, 'toggleAwardRequest');
    vm.$el.querySelector('.js-awards-block button').click();

    expect(vm.handleAward).toHaveBeenCalledWith('flag_tz');
    expect(vm.toggleAwardRequest).toHaveBeenCalled();
  });

  it('should be possible to add new emoji', () => {
    expect(vm.$el.querySelector('.js-add-award')).toBeDefined();
  });

  describe('when the user name contains special HTML characters', () => {
    const createAwardEmoji = (_, index) => ({
      name: 'art',
      user: { id: index, name: `&<>"\`'-${index}`, username: `user-${index}` },
    });

    const mountComponent = () => {
      const Component = Vue.extend(awardsNote);
      vm = new Component({
        store,
        propsData: {
          awards: awardsMock,
          noteAuthorId: 0,
          noteId: '545',
          canAwardEmoji: true,
          toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
        },
      }).$mount();
    };

    const findTooltip = () => vm.$el.querySelector('[title]').getAttribute('title');

    it('should only escape & and " characters', () => {
      awardsMock = [...new Array(1)].map(createAwardEmoji);
      mountComponent();
      const escapedName = awardsMock[0].user.name.replace(/&/g, '&amp;').replace(/"/g, '&quot;');

      expect(vm.$el.querySelector('[title]').outerHTML).toContain(escapedName);
    });

    it('should not escape special HTML characters twice when only 1 person awarded', () => {
      awardsMock = [...new Array(1)].map(createAwardEmoji);
      mountComponent();

      awardsMock.forEach(award => {
        expect(findTooltip()).toContain(award.user.name);
      });
    });

    it('should not escape special HTML characters twice when 2 people awarded', () => {
      awardsMock = [...new Array(2)].map(createAwardEmoji);
      mountComponent();

      awardsMock.forEach(award => {
        expect(findTooltip()).toContain(award.user.name);
      });
    });

    it('should not escape special HTML characters twice when more than 10 people awarded', () => {
      awardsMock = [...new Array(11)].map(createAwardEmoji);
      mountComponent();

      // Testing only the first 10 awards since 11 onward will not be displayed.
      awardsMock.slice(0, 10).forEach(award => {
        expect(findTooltip()).toContain(award.user.name);
      });
    });
  });

  describe('when the user cannot award emoji', () => {
    beforeEach(() => {
      const Component = Vue.extend(awardsNote);

      vm = new Component({
        store,
        propsData: {
          awards: awardsMock,
          noteAuthorId: 2,
          noteId: '545',
          canAwardEmoji: false,
          toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
        },
      }).$mount();
    });

    it('should not be possible to remove awarded emoji', () => {
      jest.spyOn(vm, 'toggleAwardRequest');

      vm.$el.querySelector('.js-awards-block button').click();

      expect(vm.toggleAwardRequest).not.toHaveBeenCalled();
    });

    it('should not be possible to add new emoji', () => {
      expect(vm.$el.querySelector('.js-add-award')).toBeNull();
    });
  });
});