Welcome to mirror list, hosted at ThFree Co, Russian Federation.

protected_branch_create_spec.js « protected_branches « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b3de2d5e031ac7b58af48b9be5c142557e8af860 (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
import ProtectedBranchCreate from '~/protected_branches/protected_branch_create';

const FORCE_PUSH_TOGGLE_TESTID = 'force-push-toggle';
const CODE_OWNER_TOGGLE_TESTID = 'code-owner-toggle';
const IS_CHECKED_CLASS = 'is-checked';
const IS_DISABLED_CLASS = 'is-disabled';
const IS_LOADING_CLASS = 'toggle-loading';

describe('ProtectedBranchCreate', () => {
  beforeEach(() => {
    jest.spyOn(ProtectedBranchCreate.prototype, 'buildDropdowns').mockImplementation();
  });

  const findForcePushToggle = () =>
    document.querySelector(`div[data-testid="${FORCE_PUSH_TOGGLE_TESTID}"] button`);
  const findCodeOwnerToggle = () =>
    document.querySelector(`div[data-testid="${CODE_OWNER_TOGGLE_TESTID}"] button`);

  const create = ({
    forcePushToggleChecked = false,
    codeOwnerToggleChecked = false,
    hasLicense = true,
  } = {}) => {
    setFixtures(`
      <form class="js-new-protected-branch">
        <span
          class="js-force-push-toggle"
          data-label="Toggle allowed to force push"
          data-is-checked="${forcePushToggleChecked}"
          data-testid="${FORCE_PUSH_TOGGLE_TESTID}"></span>
        <span
          class="js-code-owner-toggle"
          data-label="Toggle code owner approval"
          data-is-checked="${codeOwnerToggleChecked}"
          data-testid="${CODE_OWNER_TOGGLE_TESTID}"></span>
        <input type="submit" />
      </form>
    `);

    return new ProtectedBranchCreate({ hasLicense });
  };

  describe('when license supports code owner approvals', () => {
    it('instantiates the code owner toggle', () => {
      create();

      expect(findCodeOwnerToggle()).not.toBe(null);
    });
  });

  describe('when license does not support code owner approvals', () => {
    it('does not instantiate the code owner toggle', () => {
      create({ hasLicense: false });

      expect(findCodeOwnerToggle()).toBe(null);
    });
  });

  describe.each`
    description     | checkedOption               | finder
    ${'force push'} | ${'forcePushToggleChecked'} | ${findForcePushToggle}
    ${'code owner'} | ${'codeOwnerToggleChecked'} | ${findCodeOwnerToggle}
  `('when unchecked $description toggle button', ({ checkedOption, finder }) => {
    it('is not changed', () => {
      create({ [checkedOption]: false });

      const toggle = finder();

      expect(toggle).not.toHaveClass(IS_CHECKED_CLASS);
      expect(toggle.querySelector(`.${IS_LOADING_CLASS}`)).toBe(null);
      expect(toggle).not.toHaveClass(IS_DISABLED_CLASS);
    });
  });

  describe('form data', () => {
    let protectedBranchCreate;

    beforeEach(() => {
      protectedBranchCreate = create({
        forcePushToggleChecked: false,
        codeOwnerToggleChecked: true,
      });

      // Mock access levels. This should probably be improved in future iterations.
      protectedBranchCreate.merge_access_levels_dropdown = {
        getSelectedItems: () => [],
      };
      protectedBranchCreate.push_access_levels_dropdown = {
        getSelectedItems: () => [],
      };
    });

    afterEach(() => {
      protectedBranchCreate = null;
    });

    it('returns the default form data if toggles are untouched', () => {
      expect(protectedBranchCreate.getFormData().protected_branch).toMatchObject({
        allow_force_push: false,
        code_owner_approval_required: true,
      });
    });

    it('reflects toggles changes if any', () => {
      findForcePushToggle().click();
      findCodeOwnerToggle().click();

      expect(protectedBranchCreate.getFormData().protected_branch).toMatchObject({
        allow_force_push: true,
        code_owner_approval_required: false,
      });
    });
  });
});