summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/markdown/suggestion_diff_spec.js
blob: 5bd6bda2d2c414c09194f840af57bd034bc50fb1 (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
import { shallowMount } from '@vue/test-utils';
import SuggestionDiffComponent from '~/vue_shared/components/markdown/suggestion_diff.vue';
import SuggestionDiffHeader from '~/vue_shared/components/markdown/suggestion_diff_header.vue';
import SuggestionDiffRow from '~/vue_shared/components/markdown/suggestion_diff_row.vue';

const suggestionId = 1;
const MOCK_DATA = {
  suggestion: {
    id: suggestionId,
    diff_lines: [
      {
        can_receive_suggestion: false,
        line_code: null,
        meta_data: null,
        new_line: null,
        old_line: 5,
        rich_text: '-test',
        text: '-test',
        type: 'old',
      },
      {
        can_receive_suggestion: true,
        line_code: null,
        meta_data: null,
        new_line: 5,
        old_line: null,
        rich_text: '+new test',
        text: '+new test',
        type: 'new',
      },
      {
        can_receive_suggestion: true,
        line_code: null,
        meta_data: null,
        new_line: 5,
        old_line: null,
        rich_text: '+new test2',
        text: '+new test2',
        type: 'new',
      },
    ],
    is_applying_batch: true,
  },
  helpPagePath: 'path_to_docs',
  defaultCommitMessage: 'Apply suggestion',
  batchSuggestionsInfo: [{ suggestionId }],
};

describe('Suggestion Diff component', () => {
  let wrapper;

  const createComponent = () => {
    wrapper = shallowMount(SuggestionDiffComponent, {
      propsData: {
        ...MOCK_DATA,
      },
    });
  };

  beforeEach(() => {
    createComponent();
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  it('matches snapshot', () => {
    expect(wrapper.element).toMatchSnapshot();
  });

  it('renders a correct amount of suggestion diff rows', () => {
    expect(wrapper.findAll(SuggestionDiffRow)).toHaveLength(3);
  });

  it.each`
    event                | childArgs         | args
    ${'apply'}           | ${['test-event']} | ${[{ callback: 'test-event', suggestionId }]}
    ${'applyBatch'}      | ${[]}             | ${[]}
    ${'addToBatch'}      | ${[]}             | ${[suggestionId]}
    ${'removeFromBatch'} | ${[]}             | ${[suggestionId]}
  `('emits $event event on sugestion diff header $event', ({ event, childArgs, args }) => {
    wrapper.find(SuggestionDiffHeader).vm.$emit(event, ...childArgs);

    expect(wrapper.emitted(event)).toBeDefined();
    expect(wrapper.emitted(event)).toEqual([args]);
  });

  it('passes suggestion batch props to suggestion diff header', () => {
    expect(wrapper.find(SuggestionDiffHeader).props()).toMatchObject({
      batchSuggestionsCount: 1,
      isBatched: true,
      isApplyingBatch: MOCK_DATA.suggestion.is_applying_batch,
    });
  });
});