summaryrefslogtreecommitdiff
path: root/spec/javascripts/diffs/components/parallel_diff_view_spec.js
blob: cab533217c0d1a222b344cc8cdf35de85271b4c2 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import Vue from 'vue';
import ParallelDiffView from '~/diffs/components/parallel_diff_view.vue';
import store from '~/mr_notes/stores';
import * as constants from '~/diffs/constants';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import diffFileMockData from '../mock_data/diff_file';
import discussionsMockData from '../mock_data/diff_discussions';

describe('ParallelDiffView', () => {
  let component;
  const getDiffFileMock = () => Object.assign({}, diffFileMockData);
  const getDiscussionsMockData = () => [Object.assign({}, discussionsMockData)];

  beforeEach(() => {
    const diffFile = getDiffFileMock();

    component = createComponentWithStore(Vue.extend(ParallelDiffView), store, {
      diffFile,
      diffLines: diffFile.parallelDiffLines,
    }).$mount();
  });

  describe('computed', () => {
    describe('parallelDiffLines', () => {
      it('should normalize lines for empty cells', () => {
        expect(component.parallelDiffLines[0].left.type).toEqual(constants.EMPTY_CELL_TYPE);
        expect(component.parallelDiffLines[1].left.type).toEqual(constants.EMPTY_CELL_TYPE);
      });
    });
  });

  describe('methods', () => {
    describe('hasDiscussion', () => {
      it('it should return true if there is a discussion either for left or right section', () => {
        Object.defineProperty(component, 'discussionsByLineCode', {
          get() {
            return { line_42: true };
          },
        });

        expect(component.hasDiscussion({ left: {}, right: {} })).toEqual(undefined);
        expect(component.hasDiscussion({ left: { lineCode: 'line_42' }, right: {} })).toEqual(true);
        expect(component.hasDiscussion({ left: {}, right: { lineCode: 'line_42' } })).toEqual(true);
      });
    });

    describe('getClassName', () => {
      it('should return line class object', () => {
        const { LINE_HOVER_CLASS_NAME, LINE_UNFOLD_CLASS_NAME } = constants;
        const { MATCH_LINE_TYPE, NEW_LINE_TYPE, LINE_POSITION_RIGHT } = constants;

        expect(component.getClassName(component.diffLines[1], LINE_POSITION_RIGHT)).toEqual({
          [NEW_LINE_TYPE]: NEW_LINE_TYPE,
          [LINE_UNFOLD_CLASS_NAME]: false,
          [LINE_HOVER_CLASS_NAME]: false,
        });

        const eventMock = {
          target: component.$refs.rightLines[1],
        };

        component.handleMouse(eventMock, component.diffLines[1], true);
        Object.defineProperty(component, 'isLoggedIn', {
          get() {
            return true;
          },
        });

        expect(component.getClassName(component.diffLines[1], LINE_POSITION_RIGHT)).toEqual({
          [NEW_LINE_TYPE]: NEW_LINE_TYPE,
          [LINE_UNFOLD_CLASS_NAME]: false,
          [LINE_HOVER_CLASS_NAME]: true,
        });

        expect(component.getClassName(component.diffLines[5], LINE_POSITION_RIGHT)).toEqual({
          [MATCH_LINE_TYPE]: MATCH_LINE_TYPE,
          [LINE_UNFOLD_CLASS_NAME]: true,
          [LINE_HOVER_CLASS_NAME]: false,
        });
      });
    });

    describe('handleMouse', () => {
      it('should set hovered line code and line section to null when isHover is false', () => {
        const rightLineEventMock = { target: component.$refs.rightLines[1] };
        expect(component.hoveredLineCode).toEqual(null);
        expect(component.hoveredSection).toEqual(null);

        component.handleMouse(rightLineEventMock, null, false);
        expect(component.hoveredLineCode).toEqual(null);
        expect(component.hoveredSection).toEqual(null);
      });

      it('should set hovered line code and line section for right section', () => {
        const rightLineEventMock = { target: component.$refs.rightLines[1] };
        component.handleMouse(rightLineEventMock, component.diffLines[1], true);
        expect(component.hoveredLineCode).toEqual(component.diffLines[1].right.lineCode);
        expect(component.hoveredSection).toEqual(constants.LINE_POSITION_RIGHT);
      });

      it('should set hovered line code and line section for left section', () => {
        const leftLineEventMock = { target: component.$refs.leftLines[2] };
        component.handleMouse(leftLineEventMock, component.diffLines[2], true);
        expect(component.hoveredLineCode).toEqual(component.diffLines[2].left.lineCode);
        expect(component.hoveredSection).toEqual(constants.LINE_POSITION_LEFT);
      });
    });

    describe('shouldRenderDiscussions', () => {
      it('should return true if there is a discussion on left side and it is expanded', () => {
        const line = { left: { lineCode: 'lineCode1' } };
        spyOn(component, 'isDiscussionExpanded').and.returnValue(true);
        Object.defineProperty(component, 'discussionsByLineCode', {
          get() {
            return {
              [line.left.lineCode]: true,
            };
          },
        });

        expect(component.shouldRenderDiscussions(line, constants.LINE_POSITION_LEFT)).toEqual(true);
        expect(component.isDiscussionExpanded).toHaveBeenCalledWith(line.left.lineCode);
      });

      it('should return false if there is a discussion on left side but it is collapsed', () => {
        const line = { left: { lineCode: 'lineCode1' } };
        spyOn(component, 'isDiscussionExpanded').and.returnValue(false);
        Object.defineProperty(component, 'discussionsByLineCode', {
          get() {
            return {
              [line.left.lineCode]: true,
            };
          },
        });

        expect(component.shouldRenderDiscussions(line, constants.LINE_POSITION_LEFT)).toEqual(
          false,
        );
      });

      it('should return false for discussions on the right side if there is no line type', () => {
        const CUSTOM_RIGHT_LINE_TYPE = 'CUSTOM_RIGHT_LINE_TYPE';
        const line = { right: { lineCode: 'lineCode1', type: CUSTOM_RIGHT_LINE_TYPE } };
        spyOn(component, 'isDiscussionExpanded').and.returnValue(true);
        Object.defineProperty(component, 'discussionsByLineCode', {
          get() {
            return {
              [line.right.lineCode]: true,
            };
          },
        });

        expect(component.shouldRenderDiscussions(line, constants.LINE_POSITION_RIGHT)).toEqual(
          CUSTOM_RIGHT_LINE_TYPE,
        );
      });
    });

    describe('hasAnyExpandedDiscussion', () => {
      const LINE_CODE_LEFT = 'LINE_CODE_LEFT';
      const LINE_CODE_RIGHT = 'LINE_CODE_RIGHT';

      it('should return true if there is a discussion either on the left or the right side', () => {
        const mockLineOne = {
          right: { lineCode: LINE_CODE_RIGHT },
          left: {},
        };
        const mockLineTwo = {
          left: { lineCode: LINE_CODE_LEFT },
          right: {},
        };

        spyOn(component, 'isDiscussionExpanded').and.callFake(lc => lc === LINE_CODE_RIGHT);
        expect(component.hasAnyExpandedDiscussion(mockLineOne)).toEqual(true);
        expect(component.hasAnyExpandedDiscussion(mockLineTwo)).toEqual(false);
      });
    });
  });

  describe('template', () => {
    it('should have rendered diff lines', () => {
      const el = component.$el;

      expect(el.querySelectorAll('tr.line_holder.parallel').length).toEqual(6);
      expect(el.querySelectorAll('td.empty-cell').length).toEqual(4);
      expect(el.querySelectorAll('td.line_content.parallel.right-side').length).toEqual(6);
      expect(el.querySelectorAll('td.line_content.parallel.left-side').length).toEqual(6);
      expect(el.querySelectorAll('td.match').length).toEqual(4);
      expect(el.textContent.indexOf('Bad dates') > -1).toEqual(true);
    });

    it('should render discussions', done => {
      const el = component.$el;
      component.$store.dispatch('setInitialNotes', getDiscussionsMockData());

      Vue.nextTick(() => {
        expect(el.querySelectorAll('.notes_holder').length).toEqual(1);
        expect(el.querySelectorAll('.notes_holder .note-discussion li').length).toEqual(5);
        expect(el.innerText.indexOf('comment 5') > -1).toEqual(true);
        component.$store.dispatch('setInitialNotes', []);

        done();
      });
    });

    it('should render new discussion forms', done => {
      const el = component.$el;
      const lines = getDiffFileMock().parallelDiffLines;

      component.handleShowCommentForm({ lineCode: lines[0].lineCode });
      component.handleShowCommentForm({ lineCode: lines[1].lineCode });

      Vue.nextTick(() => {
        expect(el.querySelectorAll('.js-vue-markdown-field').length).toEqual(2);
        expect(el.querySelectorAll('tr')[1].classList.contains('notes_holder')).toEqual(true);
        expect(el.querySelectorAll('tr')[3].classList.contains('notes_holder')).toEqual(true);

        store.state.diffs.diffLineCommentForms = {};

        done();
      });
    });
  });
});