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

index_spec.js « view « components « branch_rules « settings « projects « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc373d9deb754979def00cfd8422ac1c2db62544 (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
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import * as util from '~/lib/utils/url_utility';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import RuleView from '~/projects/settings/branch_rules/components/view/index.vue';
import {
  I18N,
  ALL_BRANCHES_WILDCARD,
} from '~/projects/settings/branch_rules/components/view/constants';
import Protection from '~/projects/settings/branch_rules/components/view/protection.vue';
import branchRulesQuery from '~/projects/settings/branch_rules/queries/branch_rules_details.query.graphql';
import { sprintf } from '~/locale';
import {
  branchProtectionsMockResponse,
  approvalRulesMock,
  statusChecksRulesMock,
  matchingBranchesCount,
} from './mock_data';

jest.mock('~/lib/utils/url_utility', () => ({
  getParameterByName: jest.fn().mockReturnValue('main'),
  mergeUrlParams: jest.fn().mockReturnValue('/branches?state=all&search=main'),
  joinPaths: jest.fn(),
}));

Vue.use(VueApollo);

const protectionMockProps = {
  headerLinkHref: 'protected/branches',
  headerLinkTitle: 'Manage in Protected Branches',
  roles: [{ accessLevelDescription: 'Maintainers' }],
  users: [{ avatarUrl: 'test.com/user.png', name: 'peter', webUrl: 'test.com' }],
};

describe('View branch rules', () => {
  let wrapper;
  let fakeApollo;
  const projectPath = 'test/testing';
  const protectedBranchesPath = 'protected/branches';
  const approvalRulesPath = 'approval/rules';
  const statusChecksPath = 'status/checks';
  const branchProtectionsMockRequestHandler = jest
    .fn()
    .mockResolvedValue(branchProtectionsMockResponse);

  const createComponent = async () => {
    fakeApollo = createMockApollo([[branchRulesQuery, branchProtectionsMockRequestHandler]]);

    wrapper = shallowMountExtended(RuleView, {
      apolloProvider: fakeApollo,
      provide: { projectPath, protectedBranchesPath, approvalRulesPath, statusChecksPath },
    });

    await waitForPromises();
  };

  beforeEach(() => createComponent());

  afterEach(() => wrapper.destroy());

  const findBranchName = () => wrapper.findByTestId('branch');
  const findBranchTitle = () => wrapper.findByTestId('branch-title');
  const findBranchProtectionTitle = () => wrapper.findByText(I18N.protectBranchTitle);
  const findBranchProtections = () => wrapper.findAllComponents(Protection);
  const findForcePushTitle = () => wrapper.findByText(I18N.allowForcePushDescription);
  const findApprovalsTitle = () => wrapper.findByText(I18N.approvalsTitle);
  const findStatusChecksTitle = () => wrapper.findByText(I18N.statusChecksTitle);
  const findMatchingBranchesLink = () =>
    wrapper.findByText(
      sprintf(I18N.matchingBranchesLinkTitle, {
        total: matchingBranchesCount,
        subject: 'branches',
      }),
    );

  it('gets the branch param from url and renders it in the view', () => {
    expect(util.getParameterByName).toHaveBeenCalledWith('branch');
    expect(findBranchName().text()).toBe('main');
    expect(findBranchTitle().text()).toBe(I18N.branchNameOrPattern);
  });

  it('renders the correct label if all branches are targeted', async () => {
    jest.spyOn(util, 'getParameterByName').mockReturnValueOnce(ALL_BRANCHES_WILDCARD);
    await createComponent();

    expect(findBranchName().text()).toBe(I18N.allBranches);
    expect(findBranchTitle().text()).toBe(I18N.targetBranch);
    jest.restoreAllMocks();
  });

  it('renders the correct branch title', () => {
    expect(findBranchTitle().exists()).toBe(true);
  });

  it('renders matching branches link', () => {
    const matchingBranchesLink = findMatchingBranchesLink();
    expect(matchingBranchesLink.exists()).toBe(true);
    expect(matchingBranchesLink.attributes().href).toBe('/branches?state=all&search=main');
  });

  it('renders a branch protection title', () => {
    expect(findBranchProtectionTitle().exists()).toBe(true);
  });

  it('renders a branch protection component for push rules', () => {
    expect(findBranchProtections().at(0).props()).toMatchObject({
      header: sprintf(I18N.allowedToPushHeader, { total: 2 }),
      ...protectionMockProps,
    });
  });

  it('renders force push protection', () => {
    expect(findForcePushTitle().exists()).toBe(true);
  });

  it('renders a branch protection component for merge rules', () => {
    expect(findBranchProtections().at(1).props()).toMatchObject({
      header: sprintf(I18N.allowedToMergeHeader, { total: 2 }),
      ...protectionMockProps,
    });
  });

  it('renders a branch protection component for approvals', () => {
    expect(findApprovalsTitle().exists()).toBe(true);

    expect(findBranchProtections().at(2).props()).toMatchObject({
      header: sprintf(I18N.approvalsHeader, { total: 3 }),
      headerLinkHref: approvalRulesPath,
      headerLinkTitle: I18N.manageApprovalsLinkTitle,
      approvals: approvalRulesMock,
    });
  });

  it('renders a branch protection component for status checks', () => {
    expect(findStatusChecksTitle().exists()).toBe(true);

    expect(findBranchProtections().at(3).props()).toMatchObject({
      header: sprintf(I18N.statusChecksHeader, { total: 2 }),
      headerLinkHref: statusChecksPath,
      headerLinkTitle: I18N.statusChecksLinkTitle,
      statusChecks: statusChecksRulesMock,
    });
  });
});