Welcome to mirror list, hosted at ThFree Co, Russian Federation.

note_awards_list_spec.js « components « notes « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0107b27f98059abf9d759dc444ca6f3cf11692a1 (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
import AxiosMockAdapter from 'axios-mock-adapter';
import Vue from 'vue';
import Vuex from 'vuex';
import { TEST_HOST } from 'helpers/test_constants';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import { userDataMock } from 'jest/notes/mock_data';
import EmojiPicker from '~/emoji/components/picker.vue';
import axios from '~/lib/utils/axios_utils';
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
import awardsNote from '~/notes/components/note_awards_list.vue';
import createStore from '~/notes/stores';

Vue.use(Vuex);

describe('Note Awards List', () => {
  let wrapper;
  let mock;

  const awardsMock = [
    {
      name: 'flag_tz',
      user: { id: 1, name: 'Administrator', username: 'root' },
    },
    {
      name: 'cartwheel_tone3',
      user: { id: 12, name: 'Bobbie Stehr', username: 'erin' },
    },
  ];
  const toggleAwardPathMock = `${TEST_HOST}/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji`;

  const defaultProps = {
    awards: awardsMock,
    noteAuthorId: 2,
    noteId: '545',
    canAwardEmoji: false,
    toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
  };

  const findAddAward = () => wrapper.find('.js-add-award');
  const findAwardButton = () => wrapper.findByTestId('award-button');
  const findAllEmojiAwards = () => wrapper.findAll('gl-emoji');
  const findEmojiPicker = () => wrapper.findComponent(EmojiPicker);

  const createComponent = (props = defaultProps, store = createStore()) => {
    wrapper = mountExtended(awardsNote, {
      store,
      propsData: {
        ...props,
      },
    });
  };

  describe('Note Awards functionality', () => {
    const toggleAwardRequestSpy = jest.fn();
    const fakeStore = () => {
      return new Vuex.Store({
        getters: {
          getUserData: () => userDataMock,
        },
        actions: {
          toggleAwardRequest: toggleAwardRequestSpy,
        },
      });
    };

    beforeEach(() => {
      mock = new AxiosMockAdapter(axios);
      mock.onPost(toggleAwardPathMock).reply(HTTP_STATUS_OK, '');

      createComponent(
        {
          awards: awardsMock,
          noteAuthorId: 2,
          noteId: '545',
          canAwardEmoji: true,
          toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
        },
        fakeStore(),
      );
    });

    afterEach(() => {
      mock.restore();
    });

    it('should render awarded emojis', () => {
      const emojiAwards = findAllEmojiAwards();

      expect(emojiAwards).toHaveLength(awardsMock.length);
      expect(emojiAwards.at(0).attributes('data-name')).toBe('flag_tz');
      expect(emojiAwards.at(1).attributes('data-name')).toBe('cartwheel_tone3');
    });

    it('should be possible to add new emoji', () => {
      expect(findEmojiPicker().exists()).toBe(true);
    });

    it('should be possible to remove awarded emoji', async () => {
      await findAwardButton().vm.$emit('click');

      const { toggleAwardPath, noteId } = defaultProps;
      expect(toggleAwardRequestSpy).toHaveBeenCalledWith(expect.anything(), {
        awardName: awardsMock[0].name,
        endpoint: toggleAwardPath,
        noteId,
      });
    });
  });

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

    const customProps = {
      awards: awardsMock,
      noteAuthorId: 0,
      noteId: '545',
      canAwardEmoji: true,
      toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
    };

    it('should not escape special HTML characters twice when only 1 person awarded', () => {
      const awardsCopy = [...new Array(1)].map(createAwardEmoji);
      createComponent({
        ...customProps,
        awards: awardsCopy,
      });

      awardsCopy.forEach((award) => {
        expect(findAwardButton().attributes('title')).toContain(award.user.name);
      });
    });

    it('should not escape special HTML characters twice when 2 people awarded', () => {
      const awardsCopy = [...new Array(2)].map(createAwardEmoji);
      createComponent({
        ...customProps,
        awards: awardsCopy,
      });

      awardsCopy.forEach((award) => {
        expect(findAwardButton().attributes('title')).toContain(award.user.name);
      });
    });

    it('should not escape special HTML characters twice when more than 10 people awarded', () => {
      const awardsCopy = [...new Array(11)].map(createAwardEmoji);
      createComponent({
        ...customProps,
        awards: awardsCopy,
      });

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

  describe('when the user cannot award an emoji', () => {
    beforeEach(() => {
      createComponent({
        awards: awardsMock,
        noteAuthorId: 2,
        noteId: '545',
        canAwardEmoji: false,
        toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
      });
    });

    it('should display an award emoji button with a disabled class', () => {
      expect(findAwardButton().classes()).toContain('disabled');
    });

    it('should not be possible to add new emoji', () => {
      expect(findAddAward().exists()).toBe(false);
    });
  });
});