summaryrefslogtreecommitdiff
path: root/spec/frontend/batch_comments/components/preview_item_spec.js
blob: 08167a940682f74f3028ccb58fcf96eb2890caf2 (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
import Vue from 'vue';
import { mountComponentWithStore } from 'helpers/vue_mount_component_helper';
import PreviewItem from '~/batch_comments/components/preview_item.vue';
import { createStore } from '~/batch_comments/stores';
import diffsModule from '~/diffs/store/modules';
import notesModule from '~/notes/stores/modules';
import '~/behaviors/markdown/render_gfm';
import { createDraft } from '../mock_data';

describe('Batch comments draft preview item component', () => {
  let vm;
  let Component;
  let draft;

  function createComponent(isLast = false, extra = {}, extendStore = () => {}) {
    const store = createStore();
    store.registerModule('diffs', diffsModule());
    store.registerModule('notes', notesModule());

    extendStore(store);

    draft = {
      ...createDraft(),
      ...extra,
    };

    vm = mountComponentWithStore(Component, { store, props: { draft, isLast } });
  }

  beforeAll(() => {
    Component = Vue.extend(PreviewItem);
  });

  afterEach(() => {
    vm.$destroy();
  });

  it('renders text content', () => {
    createComponent(false, { note_html: '<img src="" /><p>Hello world</p>' });

    expect(vm.$el.querySelector('.review-preview-item-content').innerHTML).toEqual(
      '<p>Hello world</p>',
    );
  });

  describe('for file', () => {
    it('renders file path', () => {
      createComponent(false, { file_path: 'index.js', file_hash: 'abc', position: {} });

      expect(vm.$el.querySelector('.review-preview-item-header-text').textContent).toContain(
        'index.js',
      );
    });

    it('renders new line position', () => {
      createComponent(false, {
        file_path: 'index.js',
        file_hash: 'abc',
        position: {
          line_range: {
            start: {
              new_line: 1,
              type: 'new',
            },
          },
        },
      });

      expect(vm.$el.querySelector('.bold').textContent).toContain(':+1');
    });

    it('renders old line position', () => {
      createComponent(false, {
        file_path: 'index.js',
        file_hash: 'abc',
        position: {
          line_range: {
            start: {
              old_line: 2,
            },
          },
        },
      });

      expect(vm.$el.querySelector('.bold').textContent).toContain(':2');
    });

    it('renders image position', () => {
      createComponent(false, {
        file_path: 'index.js',
        file_hash: 'abc',
        position: { position_type: 'image', x: 10, y: 20 },
      });

      expect(vm.$el.querySelector('.bold').textContent).toContain('10x 20y');
    });
  });

  describe('for thread', () => {
    beforeEach(() => {
      createComponent(false, { discussion_id: '1', resolve_discussion: true }, (store) => {
        store.state.notes.discussions.push({
          id: '1',
          notes: [
            {
              author: {
                name: 'Author Name',
              },
            },
          ],
        });
      });
    });

    it('renders title', () => {
      expect(vm.$el.querySelector('.review-preview-item-header-text').textContent).toContain(
        "Author Name's thread",
      );
    });

    it('it renders thread resolved text', () => {
      expect(vm.$el.querySelector('.draft-note-resolution').textContent).toContain(
        'Thread will be resolved',
      );
    });
  });
});