summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/markdown/apply_suggestion_spec.js
blob: ecb2b37c3a563180cab18545e1418a8f4eccbfc3 (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
import { GlDropdown, GlFormTextarea, GlButton, GlAlert } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import ApplySuggestionComponent from '~/vue_shared/components/markdown/apply_suggestion.vue';

describe('Apply Suggestion component', () => {
  const propsData = { defaultCommitMessage: 'Apply suggestion', disabled: false };
  let wrapper;

  const createWrapper = (props) => {
    wrapper = shallowMount(ApplySuggestionComponent, { propsData: { ...propsData, ...props } });
  };

  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findTextArea = () => wrapper.findComponent(GlFormTextarea);
  const findApplyButton = () => wrapper.findComponent(GlButton);
  const findAlert = () => wrapper.findComponent(GlAlert);

  beforeEach(() => createWrapper());

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

  describe('initial template', () => {
    it('renders a dropdown with the correct props', () => {
      const dropdown = findDropdown();

      expect(dropdown.exists()).toBe(true);
      expect(dropdown.props('text')).toBe('Apply suggestion');
      expect(dropdown.props('disabled')).toBe(false);
    });

    it('renders a textarea with the correct props', () => {
      const textArea = findTextArea();

      expect(textArea.exists()).toBe(true);
      expect(textArea.attributes('placeholder')).toBe('Apply suggestion');
    });

    it('renders an apply button', () => {
      const applyButton = findApplyButton();

      expect(applyButton.exists()).toBe(true);
      expect(applyButton.text()).toBe('Apply');
    });
  });

  describe('disabled', () => {
    it('disables the dropdown', () => {
      createWrapper({ disabled: true });

      expect(findDropdown().props('disabled')).toBe(true);
    });
  });

  describe('error', () => {
    it('displays an error message', () => {
      const errorMessage = 'Error message';
      createWrapper({ errorMessage });

      const alert = findAlert();

      expect(alert.exists()).toBe(true);
      expect(alert.props('variant')).toBe('danger');
      expect(alert.props('dismissible')).toBe(false);
      expect(alert.text()).toBe(errorMessage);
    });
  });

  describe('apply suggestion', () => {
    it('emits an apply event with no message if no message was added', () => {
      findTextArea().vm.$emit('input', null);
      findApplyButton().vm.$emit('click');

      expect(wrapper.emitted('apply')).toEqual([[null]]);
    });

    it('emits an apply event with a user-defined message', () => {
      findTextArea().vm.$emit('input', 'some text');
      findApplyButton().vm.$emit('click');

      expect(wrapper.emitted('apply')).toEqual([['some text']]);
    });
  });
});