repo_commit_section_spec.js 4.5 KB
Newer Older
1
import Vue from 'vue';
2 3 4
import repoCommitSection from '~/repo/components/repo_commit_section.vue';
import RepoStore from '~/repo/stores/repo_store';
import RepoHelper from '~/repo/helpers/repo_helper';
5 6
import Api from '~/api';

7
describe('RepoCommitSection', () => {
8
  const branch = 'master';
9
  const projectUrl = 'projectUrl';
10 11 12
  const openedFiles = [{
    id: 0,
    changed: true,
13
    url: `/namespace/${projectUrl}/blob/${branch}/dir/file0.ext`,
14 15 16 17
    newContent: 'a',
  }, {
    id: 1,
    changed: true,
18
    url: `/namespace/${projectUrl}/blob/${branch}/dir/file1.ext`,
19 20 21
    newContent: 'b',
  }, {
    id: 2,
22
    url: `/namespace/${projectUrl}/blob/${branch}/dir/file2.ext`,
23 24 25
    changed: false,
  }];

26 27
  RepoStore.projectUrl = projectUrl;

28 29 30 31 32 33 34 35
  function createComponent() {
    const RepoCommitSection = Vue.extend(repoCommitSection);

    return new RepoCommitSection().$mount();
  }

  it('renders a commit section', () => {
    RepoStore.isCommitable = true;
36
    RepoStore.targetBranch = branch;
37
    RepoStore.openedFiles = openedFiles;
38

39
    spyOn(RepoHelper, 'getBranch').and.returnValue(branch);
40 41 42 43 44

    const vm = createComponent();
    const changedFiles = [...vm.$el.querySelectorAll('.changed-files > li')];
    const commitMessage = vm.$el.querySelector('#commit-message');
    const submitCommit = vm.$el.querySelector('.submit-commit');
45
    const targetBranch = vm.$el.querySelector('.target-branch');
46 47 48 49 50 51

    expect(vm.$el.querySelector(':scope > form')).toBeTruthy();
    expect(vm.$el.querySelector('.staged-files').textContent).toEqual('Staged files (2)');
    expect(changedFiles.length).toEqual(2);

    changedFiles.forEach((changedFile, i) => {
L
Luke "Jared" Bennett 已提交
52 53 54
      const filePath = RepoHelper.getFilePathFromFullPath(openedFiles[i].url, branch);

      expect(changedFile.textContent).toEqual(filePath);
55 56 57 58 59 60
    });

    expect(commitMessage.tagName).toEqual('TEXTAREA');
    expect(commitMessage.name).toEqual('commit-message');
    expect(submitCommit.type).toEqual('submit');
    expect(submitCommit.disabled).toBeTruthy();
61
    expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeFalsy();
62
    expect(vm.$el.querySelector('.commit-summary').textContent).toEqual('Commit 2 files');
63 64
    expect(targetBranch.querySelector(':scope > label').textContent).toEqual('Target branch');
    expect(targetBranch.querySelector('.help-block').textContent).toEqual(branch);
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
  });

  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();
  });

  it('shows commit submit and summary if commitMessage and spinner if submitCommitsLoading', (done) => {
    const projectId = 'projectId';
    const commitMessage = 'commitMessage';
    RepoStore.isCommitable = true;
    RepoStore.openedFiles = openedFiles;
    RepoStore.projectId = projectId;

95 96
    spyOn(RepoHelper, 'getBranch').and.returnValue(branch);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    const vm = createComponent();
    const commitMessageEl = vm.$el.querySelector('#commit-message');
    const submitCommit = vm.$el.querySelector('.submit-commit');

    vm.commitMessage = commitMessage;

    Vue.nextTick(() => {
      expect(commitMessageEl.value).toBe(commitMessage);
      expect(submitCommit.disabled).toBeFalsy();

      spyOn(vm, 'makeCommit').and.callThrough();
      spyOn(Api, 'commitMultiple');

      submitCommit.click();

      Vue.nextTick(() => {
        expect(vm.makeCommit).toHaveBeenCalled();
        expect(submitCommit.querySelector('.fa-spinner.fa-spin')).toBeTruthy();

        const args = Api.commitMultiple.calls.allArgs()[0];
117
        const { commit_message, actions, branch: payloadBranch } = args[1];
118 119 120 121

        expect(args[0]).toBe(projectId);
        expect(commit_message).toBe(commitMessage);
        expect(actions.length).toEqual(2);
122
        expect(payloadBranch).toEqual(branch);
123 124
        expect(actions[0].action).toEqual('update');
        expect(actions[1].action).toEqual('update');
125 126
        expect(actions[0].content).toEqual(openedFiles[0].newContent);
        expect(actions[1].content).toEqual(openedFiles[1].newContent);
L
Luke "Jared" Bennett 已提交
127 128 129 130
        expect(actions[0].file_path)
          .toEqual(RepoHelper.getFilePathFromFullPath(openedFiles[0].url, branch));
        expect(actions[1].file_path)
          .toEqual(RepoHelper.getFilePathFromFullPath(openedFiles[1].url, branch));
131 132 133 134 135 136

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