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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
import { GlAlert } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
import { updateGroup } from '~/api/groups_api';
import SharedRunnersForm from '~/group_settings/components/shared_runners_form.vue';
import { I18N_CONFIRM_MESSAGE } from '~/group_settings/constants';
jest.mock('~/api/groups_api');
jest.mock('~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal');
const GROUP_ID = '99';
const GROUP_NAME = 'My group';
const RUNNER_ENABLED_VALUE = 'enabled';
const RUNNER_DISABLED_VALUE = 'disabled_and_unoverridable';
const RUNNER_ALLOW_OVERRIDE_VALUE = 'disabled_and_overridable';
describe('group_settings/components/shared_runners_form', () => {
let wrapper;
const createComponent = (provide = {}) => {
wrapper = shallowMountExtended(SharedRunnersForm, {
provide: {
groupId: GROUP_ID,
groupName: GROUP_NAME,
groupIsEmpty: false,
sharedRunnersSetting: RUNNER_ENABLED_VALUE,
parentSharedRunnersSetting: null,
runnerEnabledValue: RUNNER_ENABLED_VALUE,
runnerDisabledValue: RUNNER_DISABLED_VALUE,
runnerAllowOverrideValue: RUNNER_ALLOW_OVERRIDE_VALUE,
...provide,
},
});
};
const findAlert = (variant) =>
wrapper
.findAllComponents(GlAlert)
.filter((w) => w.props('variant') === variant)
.at(0);
const findSharedRunnersToggle = () => wrapper.findByTestId('shared-runners-toggle');
const findOverrideToggle = () => wrapper.findByTestId('override-runners-toggle');
const getSharedRunnersSetting = () => {
return updateGroup.mock.calls[0][1].shared_runners_setting;
};
beforeEach(() => {
confirmAction.mockResolvedValue(true);
updateGroup.mockResolvedValue({});
});
afterEach(() => {
confirmAction.mockReset();
updateGroup.mockReset();
});
describe('default state', () => {
beforeEach(() => {
createComponent();
});
it('"Enable shared runners" toggle is enabled', () => {
expect(findSharedRunnersToggle().props()).toMatchObject({
isLoading: false,
disabled: false,
});
});
it('"Override the group setting" is disabled', () => {
expect(findOverrideToggle().props()).toMatchObject({
isLoading: false,
disabled: true,
});
});
});
describe('When group disabled shared runners', () => {
it(`toggles are not disabled with setting ${RUNNER_DISABLED_VALUE}`, () => {
createComponent({ sharedRunnersSetting: RUNNER_DISABLED_VALUE });
expect(findSharedRunnersToggle().props('disabled')).toBe(false);
expect(findOverrideToggle().props('disabled')).toBe(false);
});
});
describe('When parent group disabled shared runners', () => {
it('toggles are disabled', () => {
createComponent({
sharedRunnersSetting: RUNNER_DISABLED_VALUE,
parentSharedRunnersSetting: RUNNER_DISABLED_VALUE,
});
expect(findSharedRunnersToggle().props('disabled')).toBe(true);
expect(findOverrideToggle().props('disabled')).toBe(true);
expect(findAlert('warning').exists()).toBe(true);
});
});
describe('loading state', () => {
beforeEach(() => {
createComponent();
});
it('is not loading by default', () => {
expect(findSharedRunnersToggle().props('isLoading')).toBe(false);
expect(findOverrideToggle().props('isLoading')).toBe(false);
});
it('is loading immediately after request', async () => {
findSharedRunnersToggle().vm.$emit('change', true);
await nextTick();
expect(findSharedRunnersToggle().props('isLoading')).toBe(true);
expect(findOverrideToggle().props('isLoading')).toBe(true);
});
it('does not update settings while loading', async () => {
findSharedRunnersToggle().vm.$emit('change', true);
await nextTick();
findSharedRunnersToggle().vm.$emit('change', false);
await nextTick();
expect(updateGroup).toHaveBeenCalledTimes(1);
});
it('is not loading state after completed request', async () => {
findSharedRunnersToggle().vm.$emit('change', true);
await waitForPromises();
expect(findSharedRunnersToggle().props('isLoading')).toBe(false);
expect(findOverrideToggle().props('isLoading')).toBe(false);
});
});
describe('"Enable shared runners" toggle', () => {
beforeEach(() => {
createComponent();
});
it('sends correct payload when turned on', async () => {
findSharedRunnersToggle().vm.$emit('change', true);
await waitForPromises();
expect(confirmAction).not.toHaveBeenCalled();
expect(getSharedRunnersSetting()).toEqual(RUNNER_ENABLED_VALUE);
expect(findOverrideToggle().props('disabled')).toBe(true);
});
it('sends correct payload when turned off', async () => {
findSharedRunnersToggle().vm.$emit('change', false);
await waitForPromises();
expect(confirmAction).toHaveBeenCalledTimes(1);
expect(confirmAction).toHaveBeenCalledWith(
I18N_CONFIRM_MESSAGE,
expect.objectContaining({
title: expect.stringContaining(GROUP_NAME),
}),
);
expect(getSharedRunnersSetting()).toEqual(RUNNER_DISABLED_VALUE);
expect(findOverrideToggle().props('disabled')).toBe(false);
});
describe('when user cancels operation', () => {
beforeEach(() => {
confirmAction.mockResolvedValue(false);
});
it('sends no payload when turned off', async () => {
findSharedRunnersToggle().vm.$emit('change', false);
await waitForPromises();
expect(confirmAction).toHaveBeenCalledTimes(1);
expect(confirmAction).toHaveBeenCalledWith(
I18N_CONFIRM_MESSAGE,
expect.objectContaining({
title: expect.stringContaining(GROUP_NAME),
}),
);
expect(updateGroup).not.toHaveBeenCalled();
expect(findOverrideToggle().props('disabled')).toBe(true);
});
});
describe('when group is empty', () => {
beforeEach(() => {
createComponent({ groupIsEmpty: true });
});
it('confirmation is not shown when turned off', async () => {
findSharedRunnersToggle().vm.$emit('change', false);
await waitForPromises();
expect(confirmAction).not.toHaveBeenCalled();
expect(getSharedRunnersSetting()).toEqual(RUNNER_DISABLED_VALUE);
});
});
});
describe('"Override the group setting" toggle', () => {
it('enabling the override toggle sends correct payload', async () => {
createComponent({ sharedRunnersSetting: RUNNER_ALLOW_OVERRIDE_VALUE });
findOverrideToggle().vm.$emit('change', true);
await waitForPromises();
expect(getSharedRunnersSetting()).toEqual(RUNNER_ALLOW_OVERRIDE_VALUE);
});
it('disabling the override toggle sends correct payload', async () => {
createComponent({ sharedRunnersSetting: RUNNER_ALLOW_OVERRIDE_VALUE });
findOverrideToggle().vm.$emit('change', false);
await waitForPromises();
expect(getSharedRunnersSetting()).toEqual(RUNNER_DISABLED_VALUE);
});
});
describe.each`
errorData | message
${{}} | ${'An error occurred while updating configuration. Refresh the page and try again.'}
${{ error: 'Undefined error' }} | ${'Undefined error Refresh the page and try again.'}
`(`with error $errorObj`, ({ errorData, message }) => {
beforeEach(async () => {
updateGroup.mockRejectedValue({
response: { data: errorData },
});
createComponent();
findSharedRunnersToggle().vm.$emit('change', false);
await waitForPromises();
});
it('error should be shown', () => {
expect(findAlert('danger').text()).toBe(message);
});
});
});
|