summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/repo_commit_section_spec.js
blob: 113ade269e9113252c104d3750caff412cce5416 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Vue from 'vue';
import store from '~/ide/stores';
import service from '~/ide/services';
import repoCommitSection from '~/ide/components/repo_commit_section.vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import getSetTimeoutPromise from 'spec/helpers/set_timeout_promise_helper';
import { file, resetStore } from '../helpers';

describe('RepoCommitSection', () => {
  let vm;

  function createComponent() {
    const Component = Vue.extend(repoCommitSection);

    vm = createComponentWithStore(Component, store, {
      noChangesStateSvgPath: 'svg',
      committedStateSvgPath: 'commitsvg',
    });

    vm.$store.state.currentProjectId = 'abcproject';
    vm.$store.state.currentBranchId = 'master';
    vm.$store.state.projects.abcproject = {
      web_url: '',
      branches: {
        master: {
          workingReference: '1',
        },
      },
    };

    vm.$store.state.rightPanelCollapsed = false;
    vm.$store.state.currentBranch = 'master';
    vm.$store.state.changedFiles = [file('file1'), file('file2')];
    vm.$store.state.changedFiles.forEach(f =>
      Object.assign(f, {
        changed: true,
        content: 'testing',
      }),
    );

    return vm.$mount();
  }

  beforeEach(done => {
    vm = createComponent();

    spyOn(service, 'getTreeData').and.returnValue(
      Promise.resolve({
        headers: {
          'page-title': 'test',
        },
        json: () =>
          Promise.resolve({
            last_commit_path: 'last_commit_path',
            parent_tree_url: 'parent_tree_url',
            path: '/',
            trees: [{ name: 'tree' }],
            blobs: [{ name: 'blob' }],
            submodules: [{ name: 'submodule' }],
          }),
      }),
    );

    Vue.nextTick(done);
  });

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

    resetStore(vm.$store);
  });

  describe('empty Stage', () => {
    it('renders no changes text', () => {
      resetStore(vm.$store);
      const Component = Vue.extend(repoCommitSection);

      vm = createComponentWithStore(Component, store, {
        noChangesStateSvgPath: 'nochangessvg',
        committedStateSvgPath: 'svg',
      }).$mount();

      expect(
        vm.$el.querySelector('.js-empty-state').textContent.trim(),
      ).toContain('No changes');
      expect(
        vm.$el.querySelector('.js-empty-state img').getAttribute('src'),
      ).toBe('nochangessvg');
    });
  });

  it('renders a commit section', () => {
    const changedFileElements = [
      ...vm.$el.querySelectorAll('.multi-file-commit-list li'),
    ];
    const submitCommit = vm.$el.querySelector('form .btn');

    expect(vm.$el.querySelector('.multi-file-commit-form')).not.toBeNull();
    expect(changedFileElements.length).toEqual(2);

    changedFileElements.forEach((changedFile, i) => {
      expect(changedFile.textContent.trim()).toContain(
        vm.$store.state.changedFiles[i].path,
      );
    });

    expect(submitCommit.disabled).toBeTruthy();
    expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeNull();
  });

  it('updates commitMessage in store on input', done => {
    const textarea = vm.$el.querySelector('textarea');

    textarea.value = 'testing commit message';

    textarea.dispatchEvent(new Event('input'));

    getSetTimeoutPromise()
      .then(() => {
        expect(vm.$store.state.commit.commitMessage).toBe(
          'testing commit message',
        );
      })
      .then(done)
      .catch(done.fail);
  });

  describe('discard draft button', () => {
    it('hidden when commitMessage is empty', () => {
      expect(
        vm.$el.querySelector('.multi-file-commit-form .btn-default'),
      ).toBeNull();
    });

    it('resets commitMessage when clicking discard button', done => {
      vm.$store.state.commit.commitMessage = 'testing commit message';

      getSetTimeoutPromise()
        .then(() => {
          vm.$el.querySelector('.multi-file-commit-form .btn-default').click();
        })
        .then(Vue.nextTick)
        .then(() => {
          expect(vm.$store.state.commit.commitMessage).not.toBe(
            'testing commit message',
          );
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('when submitting', () => {
    beforeEach(() => {
      spyOn(vm, 'commitChanges');
    });

    it('calls commitChanges', done => {
      vm.$store.state.commit.commitMessage = 'testing commit message';

      getSetTimeoutPromise()
        .then(() => {
          vm.$el.querySelector('.multi-file-commit-form .btn-success').click();
        })
        .then(Vue.nextTick)
        .then(() => {
          expect(vm.commitChanges).toHaveBeenCalled();
        })
        .then(done)
        .catch(done.fail);
    });
  });
});