getters_spec.js 3.59 KB
Newer Older
1 2 3
import commitState from '~/ide/stores/modules/commit/state';
import * as consts from '~/ide/stores/modules/commit/constants';
import * as getters from '~/ide/stores/modules/commit/getters';
Phil Hughes's avatar
Phil Hughes committed
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

describe('IDE commit module getters', () => {
  let state;

  beforeEach(() => {
    state = commitState();
  });

  describe('discardDraftButtonDisabled', () => {
    it('returns true when commitMessage is empty', () => {
      expect(getters.discardDraftButtonDisabled(state)).toBeTruthy();
    });

    it('returns false when commitMessage is not empty & loading is false', () => {
      state.commitMessage = 'test';
      state.submitCommitLoading = false;

      expect(getters.discardDraftButtonDisabled(state)).toBeFalsy();
    });

    it('returns true when commitMessage is not empty & loading is true', () => {
      state.commitMessage = 'test';
      state.submitCommitLoading = true;

      expect(getters.discardDraftButtonDisabled(state)).toBeTruthy();
    });
  });

  describe('commitButtonDisabled', () => {
    const localGetters = {
      discardDraftButtonDisabled: false,
    };
    const rootState = {
      changedFiles: ['a'],
    };

    it('returns false when discardDraftButtonDisabled is false & changedFiles is not empty', () => {
41 42 43
      expect(
        getters.commitButtonDisabled(state, localGetters, rootState),
      ).toBeFalsy();
Phil Hughes's avatar
Phil Hughes committed
44 45 46 47 48
    });

    it('returns true when discardDraftButtonDisabled is false & changedFiles is empty', () => {
      rootState.changedFiles.length = 0;

49 50 51
      expect(
        getters.commitButtonDisabled(state, localGetters, rootState),
      ).toBeTruthy();
Phil Hughes's avatar
Phil Hughes committed
52 53 54 55 56
    });

    it('returns true when discardDraftButtonDisabled is true', () => {
      localGetters.discardDraftButtonDisabled = true;

57 58 59
      expect(
        getters.commitButtonDisabled(state, localGetters, rootState),
      ).toBeTruthy();
Phil Hughes's avatar
Phil Hughes committed
60 61 62 63 64 65
    });

    it('returns true when discardDraftButtonDisabled is false & changedFiles is not empty', () => {
      localGetters.discardDraftButtonDisabled = false;
      rootState.changedFiles.length = 0;

66 67 68
      expect(
        getters.commitButtonDisabled(state, localGetters, rootState),
      ).toBeTruthy();
Phil Hughes's avatar
Phil Hughes committed
69 70 71 72 73 74 75
    });
  });

  describe('newBranchName', () => {
    it('includes username, currentBranchId, patch & random number', () => {
      gon.current_username = 'username';

76 77 78
      const branch = getters.newBranchName(state, null, {
        currentBranchId: 'testing',
      });
Phil Hughes's avatar
Phil Hughes committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

      expect(branch).toMatch(/username-testing-patch-\d{5}$/);
    });
  });

  describe('branchName', () => {
    const rootState = {
      currentBranchId: 'master',
    };
    const localGetters = {
      newBranchName: 'newBranchName',
    };

    beforeEach(() => {
      Object.assign(state, {
        newBranchName: 'state-newBranchName',
      });
    });

    it('defualts to currentBranchId', () => {
      expect(getters.branchName(state, null, rootState)).toBe('master');
    });

102
    ['COMMIT_TO_NEW_BRANCH', 'COMMIT_TO_NEW_BRANCH_MR'].forEach(type => {
Phil Hughes's avatar
Phil Hughes committed
103 104 105 106 107 108 109 110
      describe(type, () => {
        beforeEach(() => {
          Object.assign(state, {
            commitAction: consts[type],
          });
        });

        it('uses newBranchName when not empty', () => {
111 112 113
          expect(getters.branchName(state, localGetters, rootState)).toBe(
            'state-newBranchName',
          );
Phil Hughes's avatar
Phil Hughes committed
114 115 116 117 118 119 120
        });

        it('uses getters newBranchName when state newBranchName is empty', () => {
          Object.assign(state, {
            newBranchName: '',
          });

121 122 123
          expect(getters.branchName(state, localGetters, rootState)).toBe(
            'newBranchName',
          );
Phil Hughes's avatar
Phil Hughes committed
124 125 126 127 128
        });
      });
    });
  });
});