debian-mirror-gitlab/spec/frontend/notes/components/note_awards_list_spec.js

163 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-04-22 19:07:51 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
2021-03-11 19:13:27 +05:30
import Vue from 'vue';
2021-03-08 18:12:59 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2020-04-22 19:07:51 +05:30
import axios from '~/lib/utils/axios_utils';
2018-03-17 18:26:18 +05:30
import awardsNote from '~/notes/components/note_awards_list.vue';
2021-03-11 19:13:27 +05:30
import createStore from '~/notes/stores';
2018-03-17 18:26:18 +05:30
import { noteableDataMock, notesDataMock } from '../mock_data';
describe('note_awards_list component', () => {
2018-11-08 19:23:39 +05:30
let store;
2018-03-17 18:26:18 +05:30
let vm;
let awardsMock;
2020-04-22 19:07:51 +05:30
let mock;
const toggleAwardPath = `${TEST_HOST}/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji`;
2018-03-17 18:26:18 +05:30
beforeEach(() => {
2020-04-22 19:07:51 +05:30
mock = new AxiosMockAdapter(axios);
mock.onPost(toggleAwardPath).reply(200, '');
2018-03-17 18:26:18 +05:30
const Component = Vue.extend(awardsNote);
2018-11-08 19:23:39 +05:30
store = createStore();
2018-03-17 18:26:18 +05:30
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,
2018-11-20 20:47:30 +05:30
noteId: '545',
2018-05-09 12:01:36 +05:30
canAwardEmoji: true,
2020-04-22 19:07:51 +05:30
toggleAwardPath,
2018-03-17 18:26:18 +05:30
},
}).$mount();
});
afterEach(() => {
2020-04-22 19:07:51 +05:30
mock.restore();
2018-03-17 18:26:18 +05:30
vm.$destroy();
});
it('should render awarded emojis', () => {
expect(vm.$el.querySelector('.js-awards-block button [data-name="flag_tz"]')).toBeDefined();
2018-11-08 19:23:39 +05:30
expect(
vm.$el.querySelector('.js-awards-block button [data-name="cartwheel_tone3"]'),
).toBeDefined();
2018-03-17 18:26:18 +05:30
});
2018-05-09 12:01:36 +05:30
it('should be possible to remove awarded emoji', () => {
2020-04-22 19:07:51 +05:30
jest.spyOn(vm, 'handleAward');
jest.spyOn(vm, 'toggleAwardRequest');
2018-03-17 18:26:18 +05:30
vm.$el.querySelector('.js-awards-block button').click();
expect(vm.handleAward).toHaveBeenCalledWith('flag_tz');
2018-05-09 12:01:36 +05:30
expect(vm.toggleAwardRequest).toHaveBeenCalled();
2018-03-17 18:26:18 +05:30
});
it('should be possible to add new emoji', () => {
expect(vm.$el.querySelector('.js-add-award')).toBeDefined();
});
2018-05-09 12:01:36 +05:30
2020-01-01 13:55:28 +05:30
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();
};
2021-01-29 00:20:46 +05:30
const findTooltip = () => vm.$el.querySelector('[title]').getAttribute('title');
2020-01-01 13:55:28 +05:30
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;');
2021-01-29 00:20:46 +05:30
expect(vm.$el.querySelector('[title]').outerHTML).toContain(escapedName);
2020-01-01 13:55:28 +05:30
});
it('should not escape special HTML characters twice when only 1 person awarded', () => {
awardsMock = [...new Array(1)].map(createAwardEmoji);
mountComponent();
2021-03-08 18:12:59 +05:30
awardsMock.forEach((award) => {
2020-01-01 13:55:28 +05:30
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();
2021-03-08 18:12:59 +05:30
awardsMock.forEach((award) => {
2020-01-01 13:55:28 +05:30
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.
2021-03-08 18:12:59 +05:30
awardsMock.slice(0, 10).forEach((award) => {
2020-01-01 13:55:28 +05:30
expect(findTooltip()).toContain(award.user.name);
});
});
});
2018-05-09 12:01:36 +05:30
describe('when the user cannot award emoji', () => {
beforeEach(() => {
const Component = Vue.extend(awardsNote);
vm = new Component({
store,
propsData: {
awards: awardsMock,
noteAuthorId: 2,
2018-11-20 20:47:30 +05:30
noteId: '545',
2018-05-09 12:01:36 +05:30
canAwardEmoji: false,
2019-12-04 20:38:33 +05:30
toggleAwardPath: '/gitlab-org/gitlab-foss/notes/545/toggle_award_emoji',
2018-05-09 12:01:36 +05:30
},
}).$mount();
});
it('should not be possible to remove awarded emoji', () => {
2020-04-22 19:07:51 +05:30
jest.spyOn(vm, 'toggleAwardRequest');
2018-05-09 12:01:36 +05:30
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();
});
});
2018-03-17 18:26:18 +05:30
});