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
|
import { shallowMount } from '@vue/test-utils';
import { GlIcon } from '@gitlab/ui';
import ImageDiffOverlay from '~/diffs/components/image_diff_overlay.vue';
import { createStore } from '~/mr_notes/stores';
import { imageDiffDiscussions } from '../mock_data/diff_discussions';
describe('Diffs image diff overlay component', () => {
const dimensions = {
width: 100,
height: 200,
};
let wrapper;
let dispatch;
const getAllImageBadges = () => wrapper.findAll('.js-image-badge');
function createComponent(props = {}, extendStore = () => {}) {
const store = createStore();
extendStore(store);
dispatch = jest.spyOn(store, 'dispatch').mockImplementation();
wrapper = shallowMount(ImageDiffOverlay, {
store,
propsData: {
discussions: [...imageDiffDiscussions],
fileHash: 'ABC',
...props,
},
methods: {
getImageDimensions: jest.fn().mockReturnValue(dimensions),
},
});
}
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
it('renders comment badges', () => {
createComponent();
expect(getAllImageBadges()).toHaveLength(2);
});
it('renders index of discussion in badge', () => {
createComponent();
const imageBadges = getAllImageBadges();
expect(
imageBadges
.at(0)
.text()
.trim(),
).toBe('1');
expect(
imageBadges
.at(1)
.text()
.trim(),
).toBe('2');
});
it('renders icon when showCommentIcon is true', () => {
createComponent({ showCommentIcon: true });
expect(wrapper.find(GlIcon).exists()).toBe(true);
});
it('sets badge comment positions', () => {
createComponent();
const imageBadges = getAllImageBadges();
expect(imageBadges.at(0).attributes('style')).toBe('left: 10px; top: 10px;');
expect(imageBadges.at(1).attributes('style')).toBe('left: 5px; top: 5px;');
});
it('renders single badge for discussion object', () => {
createComponent({
discussions: {
...imageDiffDiscussions[0],
},
});
expect(getAllImageBadges()).toHaveLength(1);
});
it('dispatches openDiffFileCommentForm when clicking overlay', () => {
createComponent({ canComment: true });
wrapper.find('.js-add-image-diff-note-button').trigger('click', { offsetX: 0, offsetY: 0 });
expect(dispatch).toHaveBeenCalledWith('diffs/openDiffFileCommentForm', {
fileHash: 'ABC',
x: 0,
y: 0,
width: 100,
height: 200,
});
});
describe('toggle discussion', () => {
const getImageBadge = () => wrapper.find('.js-image-badge');
it('disables buttons when shouldToggleDiscussion is false', () => {
createComponent({ shouldToggleDiscussion: false });
expect(getImageBadge().attributes('disabled')).toEqual('disabled');
});
it('dispatches toggleDiscussion when clicking image badge', () => {
createComponent();
getImageBadge().trigger('click');
expect(dispatch).toHaveBeenCalledWith('toggleDiscussion', {
discussionId: '1',
});
});
});
describe('comment form', () => {
const getCommentIndicator = () => wrapper.find('.comment-indicator');
beforeEach(() => {
createComponent({}, store => {
store.state.diffs.commentForms.push({
fileHash: 'ABC',
x: 20,
y: 10,
});
});
});
it('renders comment form badge', () => {
expect(getCommentIndicator().exists()).toBe(true);
});
it('sets comment form badge position', () => {
expect(getCommentIndicator().attributes('style')).toBe('left: 20px; top: 10px;');
});
});
});
|