summaryrefslogtreecommitdiff
path: root/spec/javascripts/issue_show/components/edit_actions_spec.js
blob: f6625b748b645a4a4fcb71f1c2270ebebeb4d435 (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
import Vue from 'vue';
import editActions from '~/issue_show/components/edit_actions.vue';
import eventHub from '~/issue_show/event_hub';
import Store from '~/issue_show/stores';

describe('Edit Actions components', () => {
  let vm;

  beforeEach((done) => {
    const Component = Vue.extend(editActions);
    const store = new Store({
      titleHtml: '',
      descriptionHtml: '',
      issuableRef: '',
    });
    store.formState.title = 'test';

    spyOn(eventHub, '$emit');

    vm = new Component({
      propsData: {
        canDestroy: true,
        formState: store.formState,
      },
    }).$mount();

    Vue.nextTick(done);
  });

  it('renders all buttons as enabled', () => {
    expect(
      vm.$el.querySelectorAll('.disabled').length,
    ).toBe(0);

    expect(
      vm.$el.querySelectorAll('[disabled]').length,
    ).toBe(0);
  });

  it('does not render delete button if canUpdate is false', (done) => {
    vm.canDestroy = false;

    Vue.nextTick(() => {
      expect(
        vm.$el.querySelector('.btn-danger'),
      ).toBeNull();

      done();
    });
  });

  it('disables submit button when title is blank', (done) => {
    vm.formState.title = '';

    Vue.nextTick(() => {
      expect(
        vm.$el.querySelector('.btn-save').getAttribute('disabled'),
      ).toBe('disabled');

      done();
    });
  });

  describe('updateIssuable', () => {
    it('sends update.issauble event when clicking save button', () => {
      vm.$el.querySelector('.btn-save').click();

      expect(
        eventHub.$emit,
      ).toHaveBeenCalledWith('update.issuable');
    });

    it('shows loading icon after clicking save button', (done) => {
      vm.$el.querySelector('.btn-save').click();

      Vue.nextTick(() => {
        expect(
          vm.$el.querySelector('.btn-save .fa'),
        ).not.toBeNull();

        done();
      });
    });

    it('disabled button after clicking save button', (done) => {
      vm.$el.querySelector('.btn-save').click();

      Vue.nextTick(() => {
        expect(
          vm.$el.querySelector('.btn-save').getAttribute('disabled'),
        ).toBe('disabled');

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

  describe('closeForm', () => {
    it('emits close.form when clicking cancel', () => {
      vm.$el.querySelector('.btn-default').click();

      expect(
        eventHub.$emit,
      ).toHaveBeenCalledWith('close.form');
    });
  });

  describe('deleteIssuable', () => {
    it('sends delete.issuable event when clicking save button', () => {
      spyOn(window, 'confirm').and.returnValue(true);
      vm.$el.querySelector('.btn-danger').click();

      expect(
        eventHub.$emit,
      ).toHaveBeenCalledWith('delete.issuable');
    });

    it('shows loading icon after clicking delete button', (done) => {
      spyOn(window, 'confirm').and.returnValue(true);
      vm.$el.querySelector('.btn-danger').click();

      Vue.nextTick(() => {
        expect(
          vm.$el.querySelector('.btn-danger .fa'),
        ).not.toBeNull();

        done();
      });
    });

    it('does no actions when confirm is false', (done) => {
      spyOn(window, 'confirm').and.returnValue(false);
      vm.$el.querySelector('.btn-danger').click();

      Vue.nextTick(() => {
        expect(
          eventHub.$emit,
        ).not.toHaveBeenCalledWith('delete.issuable');
        expect(
          vm.$el.querySelector('.btn-danger .fa'),
        ).toBeNull();

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