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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
import Vue from 'vue';
import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import { projectData, branches } from 'jest/ide/mock_data';
import NewMergeRequestOption from '~/ide/components/commit_sidebar/new_merge_request_option.vue';
import { createStore } from '~/ide/stores';
import { PERMISSION_CREATE_MR } from '~/ide/constants';
import consts from '~/ide/stores/modules/commit/constants';
describe('create new MR checkbox', () => {
let store;
let vm;
const setMR = () => {
vm.$store.state.currentMergeRequestId = '1';
vm.$store.state.projects[store.state.currentProjectId].mergeRequests[
store.state.currentMergeRequestId
] = { foo: 'bar' };
};
const setPermissions = permissions => {
store.state.projects[store.state.currentProjectId].userPermissions = permissions;
};
const createComponent = ({ currentBranchId = 'master', createNewBranch = false } = {}) => {
const Component = Vue.extend(NewMergeRequestOption);
vm = createComponentWithStore(Component, store);
vm.$store.state.commit.commitAction = createNewBranch
? consts.COMMIT_TO_NEW_BRANCH
: consts.COMMIT_TO_CURRENT_BRANCH;
vm.$store.state.currentBranchId = currentBranchId;
store.state.projects.abcproject.branches[currentBranchId] = branches.find(
branch => branch.name === currentBranchId,
);
return vm.$mount();
};
const findInput = () => vm.$el.querySelector('input[type="checkbox"]');
const findLabel = () => vm.$el.querySelector('.js-ide-commit-new-mr');
beforeEach(() => {
store = createStore();
store.state.currentProjectId = 'abcproject';
const proj = JSON.parse(JSON.stringify(projectData));
proj.userPermissions[PERMISSION_CREATE_MR] = true;
Vue.set(store.state.projects, 'abcproject', proj);
});
afterEach(() => {
vm.$destroy();
});
describe('for default branch', () => {
describe('is rendered when pushing to a new branch', () => {
beforeEach(() => {
createComponent({
currentBranchId: 'master',
createNewBranch: true,
});
});
it('has NO new MR', () => {
expect(vm.$el.textContent).not.toBe('');
});
it('has new MR', done => {
setMR();
vm.$nextTick()
.then(() => {
expect(vm.$el.textContent).not.toBe('');
})
.then(done)
.catch(done.fail);
});
});
describe('is NOT rendered when pushing to the same branch', () => {
beforeEach(() => {
createComponent({
currentBranchId: 'master',
createNewBranch: false,
});
});
it('has NO new MR', () => {
expect(vm.$el.textContent).toBe('');
});
it('has new MR', done => {
setMR();
vm.$nextTick()
.then(() => {
expect(vm.$el.textContent).toBe('');
})
.then(done)
.catch(done.fail);
});
});
});
describe('for protected branch', () => {
describe('when user does not have the write access', () => {
beforeEach(() => {
createComponent({
currentBranchId: 'protected/no-access',
});
});
it('is rendered if MR does not exists', () => {
expect(vm.$el.textContent).not.toBe('');
});
it('is rendered if MR exists', done => {
setMR();
vm.$nextTick()
.then(() => {
expect(vm.$el.textContent).not.toBe('');
})
.then(done)
.catch(done.fail);
});
});
describe('when user has the write access', () => {
beforeEach(() => {
createComponent({
currentBranchId: 'protected/access',
});
});
it('is rendered if MR does not exist', () => {
expect(vm.$el.textContent).not.toBe('');
});
it('is hidden if MR exists', done => {
setMR();
vm.$nextTick()
.then(() => {
expect(vm.$el.textContent).toBe('');
})
.then(done)
.catch(done.fail);
});
});
});
describe('for regular branch', () => {
beforeEach(() => {
createComponent({
currentBranchId: 'regular',
});
});
it('is rendered if no MR exists', () => {
expect(vm.$el.textContent).not.toBe('');
});
it('is hidden if MR exists', done => {
setMR();
vm.$nextTick()
.then(() => {
expect(vm.$el.textContent).toBe('');
})
.then(done)
.catch(done.fail);
});
it('shows enablded checkbox', () => {
expect(findLabel().classList.contains('is-disabled')).toBe(false);
expect(findInput().disabled).toBe(false);
});
});
describe('when user cannot create MR', () => {
beforeEach(() => {
setPermissions({ [PERMISSION_CREATE_MR]: false });
createComponent({ currentBranchId: 'regular' });
});
it('disabled checkbox', () => {
expect(findLabel().classList.contains('is-disabled')).toBe(true);
expect(findInput().disabled).toBe(true);
});
});
it('dispatches toggleShouldCreateMR when clicking checkbox', () => {
createComponent({
currentBranchId: 'regular',
});
const el = vm.$el.querySelector('input[type="checkbox"]');
jest.spyOn(vm.$store, 'dispatch').mockImplementation(() => {});
el.dispatchEvent(new Event('change'));
expect(vm.$store.dispatch.mock.calls).toEqual(
expect.arrayContaining([['commit/toggleShouldCreateMR', expect.any(Object)]]),
);
});
});
|