summaryrefslogtreecommitdiff
path: root/spec/javascripts/image_diff/helpers/comment_indicator_helper_spec.js
blob: a284b981d2a90355d378382462f5ff9c1ddd4a63 (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
import * as commentIndicatorHelper from '~/image_diff/helpers/comment_indicator_helper';
import * as mockData from '../mock_data';

describe('commentIndicatorHelper', () => {
  const { coordinate } = mockData;
  let containerEl;

  beforeEach(() => {
    containerEl = document.createElement('div');
  });

  describe('addCommentIndicator', () => {
    let buttonEl;

    beforeEach(() => {
      commentIndicatorHelper.addCommentIndicator(containerEl, coordinate);
      buttonEl = containerEl.querySelector('button');
    });

    it('should append button to container', () => {
      expect(buttonEl).toBeDefined();
    });

    describe('button', () => {
      it('should set coordinate', () => {
        expect(buttonEl.style.left).toEqual(`${coordinate.x}px`);
        expect(buttonEl.style.top).toEqual(`${coordinate.y}px`);
      });

      it('should contain image-comment-dark svg', () => {
        const svgEl = buttonEl.querySelector('svg');
        expect(svgEl).toBeDefined();

        const svgLink = svgEl.querySelector('use').getAttribute('xlink:href');
        expect(svgLink.indexOf('image-comment-dark') !== -1).toEqual(true);
      });
    });
  });

  describe('removeCommentIndicator', () => {
    it('should return removed false if there is no comment-indicator', () => {
      const result = commentIndicatorHelper.removeCommentIndicator(containerEl);
      expect(result.removed).toEqual(false);
    });

    describe('has comment indicator', () => {
      let result;

      beforeEach(() => {
        containerEl.innerHTML = `
          <div class="comment-indicator" style="left:${coordinate.x}px; top: ${coordinate.y}px;">
            <img src="${gl.TEST_HOST}/image.png">
          </div>
        `;
        result = commentIndicatorHelper.removeCommentIndicator(containerEl);
      });

      it('should remove comment indicator', () => {
        expect(containerEl.querySelector('.comment-indicator')).toBeNull();
      });

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

      it('should return indicator meta', () => {
        expect(result.x).toEqual(coordinate.x);
        expect(result.y).toEqual(coordinate.y);
        expect(result.image).toBeDefined();
        expect(result.image.width).toBeDefined();
        expect(result.image.height).toBeDefined();
      });
    });
  });

  describe('showCommentIndicator', () => {
    describe('commentIndicator exists', () => {
      beforeEach(() => {
        containerEl.innerHTML = `
          <button class="comment-indicator"></button>
        `;
        commentIndicatorHelper.showCommentIndicator(containerEl, coordinate);
      });

      it('should set commentIndicator coordinates', () => {
        const commentIndicatorEl = containerEl.querySelector('.comment-indicator');
        expect(commentIndicatorEl.style.left).toEqual(`${coordinate.x}px`);
        expect(commentIndicatorEl.style.top).toEqual(`${coordinate.y}px`);
      });
    });

    describe('commentIndicator does not exist', () => {
      beforeEach(() => {
        commentIndicatorHelper.showCommentIndicator(containerEl, coordinate);
      });

      it('should addCommentIndicator', () => {
        const buttonEl = containerEl.querySelector('.comment-indicator');
        expect(buttonEl).toBeDefined();
        expect(buttonEl.style.left).toEqual(`${coordinate.x}px`);
        expect(buttonEl.style.top).toEqual(`${coordinate.y}px`);
      });
    });
  });

  describe('commentIndicatorOnClick', () => {
    let event;
    let textAreaEl;

    beforeEach(() => {
      containerEl.innerHTML = `
        <div class="diff-viewer">
          <button></button>
          <div class="note-container">
            <textarea class="note-textarea"></textarea>
          </div>
        </div>
      `;
      textAreaEl = containerEl.querySelector('textarea');

      event = {
        stopPropagation: () => {},
        currentTarget: containerEl.querySelector('button'),
      };

      spyOn(event, 'stopPropagation');
      spyOn(textAreaEl, 'focus');
      commentIndicatorHelper.commentIndicatorOnClick(event);
    });

    it('should stopPropagation', () => {
      expect(event.stopPropagation).toHaveBeenCalled();
    });

    it('should focus textAreaEl', () => {
      expect(textAreaEl.focus).toHaveBeenCalled();
    });
  });
});