summaryrefslogtreecommitdiff
path: root/spec/javascripts/diffs/components/inline_diff_view_spec.js
blob: 0d5a357620435aec3dec178a7d0a11d167cbebdf (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
import Vue from 'vue';
import InlineDiffView from '~/diffs/components/inline_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('InlineDiffView', () => {
  let component;
  const getDiffFileMock = () => Object.assign({}, diffFileMockData);
  const getDiscussionsMockData = () => [Object.assign({}, discussionsMockData)];

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

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

  describe('methods', () => {
    describe('handleMouse', () => {
      it('should set hoveredLineCode', () => {
        expect(component.hoveredLineCode).toEqual(null);

        component.handleMouse('lineCode1', true);
        expect(component.hoveredLineCode).toEqual('lineCode1');

        component.handleMouse('lineCode1', false);
        expect(component.hoveredLineCode).toEqual(null);
      });
    });

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

        expect(component.getLineClass(component.diffLines[0])).toEqual({
          [NEW_LINE_TYPE]: NEW_LINE_TYPE,
          [LINE_UNFOLD_CLASS_NAME]: false,
          [LINE_HOVER_CLASS_NAME]: false,
        });

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

        expect(component.getLineClass(component.diffLines[0])).toEqual({
          [NEW_LINE_TYPE]: NEW_LINE_TYPE,
          [LINE_UNFOLD_CLASS_NAME]: false,
          [LINE_HOVER_CLASS_NAME]: true,
        });

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

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

      expect(el.querySelectorAll('tr.line_holder').length).toEqual(6);
      expect(el.querySelectorAll('tr.line_holder.new').length).toEqual(2);
      expect(el.querySelectorAll('tr.line_holder.match').length).toEqual(1);
      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().highlightedDiffLines;

      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();
      });
    });
  });
});