summaryrefslogtreecommitdiff
path: root/spec/javascripts/image_diff/helpers/utils_helper_spec.js
blob: 56d77a05c4cbef2490070eebbd746871a07ae419 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import * as utilsHelper from '~/image_diff/helpers/utils_helper';
import ImageDiff from '~/image_diff/image_diff';
import ReplacedImageDiff from '~/image_diff/replaced_image_diff';
import ImageBadge from '~/image_diff/image_badge';
import * as mockData from '../mock_data';

describe('utilsHelper', () => {
  const {
    noteId,
    discussionId,
    image,
    imageProperties,
    imageMeta,
  } = mockData;

  describe('resizeCoordinatesToImageElement', () => {
    let result;

    beforeEach(() => {
      result = utilsHelper.resizeCoordinatesToImageElement(image, imageMeta);
    });

    it('should return x based on widthRatio', () => {
      expect(result.x).toEqual(imageMeta.x * 0.5);
    });

    it('should return y based on heightRatio', () => {
      expect(result.y).toEqual(imageMeta.y * 0.5);
    });

    it('should return image width', () => {
      expect(result.width).toEqual(image.width);
    });

    it('should return image height', () => {
      expect(result.height).toEqual(image.height);
    });
  });

  describe('generateBadgeFromDiscussionDOM', () => {
    let discussionEl;
    let result;

    beforeEach(() => {
      const imageFrameEl = document.createElement('div');
      imageFrameEl.innerHTML = `
        <img src="${gl.TEST_HOST}/image.png">
      `;
      discussionEl = document.createElement('div');
      discussionEl.dataset.discussionId = discussionId;
      discussionEl.innerHTML = `
        <div class="note" id="${noteId}"></div>
      `;
      discussionEl.dataset.position = JSON.stringify(imageMeta);
      result = utilsHelper.generateBadgeFromDiscussionDOM(imageFrameEl, discussionEl);
    });

    it('should return actual image properties', () => {
      const { actual } = result;
      expect(actual.x).toEqual(imageMeta.x);
      expect(actual.y).toEqual(imageMeta.y);
      expect(actual.width).toEqual(imageMeta.width);
      expect(actual.height).toEqual(imageMeta.height);
    });

    it('should return browser image properties', () => {
      const { browser } = result;
      expect(browser.x).toBeDefined();
      expect(browser.y).toBeDefined();
      expect(browser.width).toBeDefined();
      expect(browser.height).toBeDefined();
    });

    it('should return instance of ImageBadge', () => {
      expect(result instanceof ImageBadge).toEqual(true);
    });

    it('should return noteId', () => {
      expect(result.noteId).toEqual(noteId);
    });

    it('should return discussionId', () => {
      expect(result.discussionId).toEqual(discussionId);
    });
  });

  describe('getTargetSelection', () => {
    let containerEl;

    beforeEach(() => {
      containerEl = {
        querySelector: () => imageProperties,
      };
    });

    function generateEvent(offsetX, offsetY) {
      return {
        currentTarget: containerEl,
        offsetX,
        offsetY,
      };
    }

    it('should return browser properties', () => {
      const event = generateEvent(25, 25);
      const result = utilsHelper.getTargetSelection(event);

      const { browser } = result;
      expect(browser.x).toEqual(event.offsetX);
      expect(browser.y).toEqual(event.offsetY);
      expect(browser.width).toEqual(imageProperties.width);
      expect(browser.height).toEqual(imageProperties.height);
    });

    it('should return resized actual image properties', () => {
      const event = generateEvent(50, 50);
      const result = utilsHelper.getTargetSelection(event);

      const { actual } = result;
      expect(actual.x).toEqual(100);
      expect(actual.y).toEqual(100);
      expect(actual.width).toEqual(imageProperties.naturalWidth);
      expect(actual.height).toEqual(imageProperties.naturalHeight);
    });

    describe('normalize coordinates', () => {
      it('should return x = 0 if x < 0', () => {
        const event = generateEvent(-5, 50);
        const result = utilsHelper.getTargetSelection(event);
        expect(result.browser.x).toEqual(0);
      });

      it('should return x = width if x > width', () => {
        const event = generateEvent(1000, 50);
        const result = utilsHelper.getTargetSelection(event);
        expect(result.browser.x).toEqual(imageProperties.width);
      });

      it('should return y = 0 if y < 0', () => {
        const event = generateEvent(50, -10);
        const result = utilsHelper.getTargetSelection(event);
        expect(result.browser.y).toEqual(0);
      });

      it('should return y = height if y > height', () => {
        const event = generateEvent(50, 1000);
        const result = utilsHelper.getTargetSelection(event);
        expect(result.browser.y).toEqual(imageProperties.height);
      });
    });
  });

  describe('initImageDiff', () => {
    let glCache;
    let fileEl;

    beforeEach(() => {
      window.gl = window.gl || (window.gl = {});
      glCache = window.gl;
      window.gl.ImageFile = () => {};
      fileEl = document.createElement('div');
      fileEl.innerHTML = `
        <div class="diff-file"></div>
      `;

      spyOn(ImageDiff.prototype, 'init').and.callFake(() => {});
      spyOn(ReplacedImageDiff.prototype, 'init').and.callFake(() => {});
    });

    afterEach(() => {
      window.gl = glCache;
    });

    it('should initialize gl.ImageFile', () => {
      spyOn(window.gl, 'ImageFile');

      utilsHelper.initImageDiff(fileEl, false, false);
      expect(gl.ImageFile).toHaveBeenCalled();
    });

    it('should initialize ImageDiff if js-single-image', () => {
      const diffFileEl = fileEl.querySelector('.diff-file');
      diffFileEl.innerHTML = `
        <div class="js-single-image">
        </div>
      `;

      const imageDiff = utilsHelper.initImageDiff(fileEl, true, false);
      expect(ImageDiff.prototype.init).toHaveBeenCalled();
      expect(imageDiff.canCreateNote).toEqual(true);
      expect(imageDiff.renderCommentBadge).toEqual(false);
    });

    it('should initialize ReplacedImageDiff if js-replaced-image', () => {
      const diffFileEl = fileEl.querySelector('.diff-file');
      diffFileEl.innerHTML = `
        <div class="js-replaced-image">
        </div>
      `;

      const replacedImageDiff = utilsHelper.initImageDiff(fileEl, false, true);
      expect(ReplacedImageDiff.prototype.init).toHaveBeenCalled();
      expect(replacedImageDiff.canCreateNote).toEqual(false);
      expect(replacedImageDiff.renderCommentBadge).toEqual(true);
    });
  });
});