repo_commit_section_spec.js 7.0 KB
Newer Older
1
import Vue from 'vue';
2 3
import repoCommitSection from '~/repo/components/repo_commit_section.vue';
import RepoStore from '~/repo/stores/repo_store';
4
import RepoService from '~/repo/services/repo_service';
J
Jacob Schatz 已提交
5
import getSetTimeoutPromise from '../../helpers/set_timeout_promise_helper';
6

7
describe('RepoCommitSection', () => {
8
  const branch = 'master';
9
  const projectUrl = 'projectUrl';
J
Jacob Schatz 已提交
10 11
  let changedFiles;
  let openedFiles;
12

13 14
  RepoStore.projectUrl = projectUrl;

J
Jacob Schatz 已提交
15
  function createComponent(el) {
16 17
    const RepoCommitSection = Vue.extend(repoCommitSection);

J
Jacob Schatz 已提交
18
    return new RepoCommitSection().$mount(el);
19 20
  }

J
Jacob Schatz 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  beforeEach(() => {
    // Create a copy for each test because these can get modified directly
    changedFiles = [{
      id: 0,
      changed: true,
      url: `/namespace/${projectUrl}/blob/${branch}/dir/file0.ext`,
      path: 'dir/file0.ext',
      newContent: 'a',
    }, {
      id: 1,
      changed: true,
      url: `/namespace/${projectUrl}/blob/${branch}/dir/file1.ext`,
      path: 'dir/file1.ext',
      newContent: 'b',
    }];
    openedFiles = changedFiles.concat([{
      id: 2,
      url: `/namespace/${projectUrl}/blob/${branch}/dir/file2.ext`,
      path: 'dir/file2.ext',
      changed: false,
    }]);
  });

44 45
  it('renders a commit section', () => {
    RepoStore.isCommitable = true;
J
Jacob Schatz 已提交
46
    RepoStore.currentBranch = branch;
47
    RepoStore.targetBranch = branch;
48
    RepoStore.openedFiles = openedFiles;
49

50
    const vm = createComponent();
J
Jacob Schatz 已提交
51
    const changedFileElements = [...vm.$el.querySelectorAll('.changed-files > li')];
52
    const commitMessage = vm.$el.querySelector('#commit-message');
J
Jacob Schatz 已提交
53
    const submitCommit = vm.$refs.submitCommit;
54
    const targetBranch = vm.$el.querySelector('.target-branch');
55 56

    expect(vm.$el.querySelector(':scope > form')).toBeTruthy();
J
Jacob Schatz 已提交
57 58
    expect(vm.$el.querySelector('.staged-files').textContent.trim()).toEqual('Staged files (2)');
    expect(changedFileElements.length).toEqual(2);
59

J
Jacob Schatz 已提交
60 61
    changedFileElements.forEach((changedFile, i) => {
      expect(changedFile.textContent.trim()).toEqual(changedFiles[i].path);
62 63 64 65 66 67
    });

    expect(commitMessage.tagName).toEqual('TEXTAREA');
    expect(commitMessage.name).toEqual('commit-message');
    expect(submitCommit.type).toEqual('submit');
    expect(submitCommit.disabled).toBeTruthy();
68
    expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeFalsy();
J
Jacob Schatz 已提交
69 70 71
    expect(vm.$el.querySelector('.commit-summary').textContent.trim()).toEqual('Commit 2 files');
    expect(targetBranch.querySelector(':scope > label').textContent.trim()).toEqual('Target branch');
    expect(targetBranch.querySelector('.help-block').textContent.trim()).toEqual(branch);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  });

  it('does not render if not isCommitable', () => {
    RepoStore.isCommitable = false;
    RepoStore.openedFiles = [{
      id: 0,
      changed: true,
    }];

    const vm = createComponent();

    expect(vm.$el.innerHTML).toBeFalsy();
  });

  it('does not render if no changedFiles', () => {
    RepoStore.isCommitable = true;
    RepoStore.openedFiles = [];

    const vm = createComponent();

    expect(vm.$el.innerHTML).toBeFalsy();
  });

J
Jacob Schatz 已提交
95 96 97
  describe('when submitting', () => {
    let el;
    let vm;
98 99 100
    const projectId = 'projectId';
    const commitMessage = 'commitMessage';

J
Jacob Schatz 已提交
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
    beforeEach((done) => {
      RepoStore.isCommitable = true;
      RepoStore.currentBranch = branch;
      RepoStore.targetBranch = branch;
      RepoStore.openedFiles = openedFiles;
      RepoStore.projectId = projectId;

      // We need to append to body to get form `submit` events working
      // Otherwise we run into, "Form submission canceled because the form is not connected"
      // See https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm
      el = document.createElement('div');
      document.body.appendChild(el);

      vm = createComponent(el);
      vm.commitMessage = commitMessage;

      spyOn(vm, 'tryCommit').and.callThrough();
      spyOn(vm, 'redirectToNewMr').and.stub();
      spyOn(vm, 'redirectToBranch').and.stub();
      spyOn(RepoService, 'commitFiles').and.returnValue(Promise.resolve());
      spyOn(RepoService, 'getBranch').and.returnValue(Promise.resolve({
        commit: {
          id: 1,
          short_id: 1,
        },
      }));

      // Wait for the vm data to be in place
      Vue.nextTick(() => {
        done();
      });
    });
133

J
Jacob Schatz 已提交
134 135 136 137
    afterEach(() => {
      vm.$destroy();
      el.remove();
    });
138

J
Jacob Schatz 已提交
139 140
    it('shows commit message', () => {
      const commitMessageEl = vm.$el.querySelector('#commit-message');
141
      expect(commitMessageEl.value).toBe(commitMessage);
J
Jacob Schatz 已提交
142
    });
143

J
Jacob Schatz 已提交
144 145 146 147
    it('allows you to submit', () => {
      const submitCommit = vm.$refs.submitCommit;
      expect(submitCommit.disabled).toBeFalsy();
    });
148

J
Jacob Schatz 已提交
149 150
    it('shows commit submit and summary if commitMessage and spinner if submitCommitsLoading', (done) => {
      const submitCommit = vm.$refs.submitCommit;
151 152
      submitCommit.click();

J
Jacob Schatz 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
      // Wait for the branch check to finish
      getSetTimeoutPromise()
        .then(() => Vue.nextTick())
        .then(() => {
          expect(vm.tryCommit).toHaveBeenCalled();
          expect(submitCommit.querySelector('.js-commit-loading-icon')).toBeTruthy();
          expect(vm.redirectToBranch).toHaveBeenCalled();

          const args = RepoService.commitFiles.calls.allArgs()[0];
          const { commit_message, actions, branch: payloadBranch } = args[0];

          expect(commit_message).toBe(commitMessage);
          expect(actions.length).toEqual(2);
          expect(payloadBranch).toEqual(branch);
          expect(actions[0].action).toEqual('update');
          expect(actions[1].action).toEqual('update');
          expect(actions[0].content).toEqual(openedFiles[0].newContent);
          expect(actions[1].content).toEqual(openedFiles[1].newContent);
          expect(actions[0].file_path).toEqual(openedFiles[0].path);
          expect(actions[1].file_path).toEqual(openedFiles[1].path);
        })
        .then(done)
        .catch(done.fail);
    });
177

J
Jacob Schatz 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    it('redirects to MR creation page if start new MR checkbox checked', (done) => {
      vm.startNewMR = true;

      Vue.nextTick()
        .then(() => {
          const submitCommit = vm.$refs.submitCommit;
          submitCommit.click();
        })
        // Wait for the branch check to finish
        .then(() => getSetTimeoutPromise())
        .then(() => {
          expect(vm.redirectToNewMr).toHaveBeenCalled();
        })
        .then(done)
        .catch(done.fail);
193 194
    });
  });
195

L
Luke "Jared" Bennett 已提交
196
  describe('methods', () => {
197 198 199 200 201
    describe('resetCommitState', () => {
      it('should reset store vars and scroll to top', () => {
        const vm = {
          submitCommitsLoading: true,
          changedFiles: new Array(10),
J
Jacob Schatz 已提交
202
          openedFiles: new Array(3),
203 204 205 206 207 208 209 210 211 212 213 214 215
          commitMessage: 'commitMessage',
          editMode: true,
        };

        repoCommitSection.methods.resetCommitState.call(vm);

        expect(vm.submitCommitsLoading).toEqual(false);
        expect(vm.changedFiles).toEqual([]);
        expect(vm.commitMessage).toEqual('');
        expect(vm.editMode).toEqual(false);
      });
    });
  });
216
});