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
|
import Vue from 'vue';
import { GlForm } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import RunnerCreateForm from '~/ci/runner/components/runner_create_form.vue';
import RunnerFormFields from '~/ci/runner/components/runner_form_fields.vue';
import {
DEFAULT_ACCESS_LEVEL,
INSTANCE_TYPE,
GROUP_TYPE,
PROJECT_TYPE,
} from '~/ci/runner/constants';
import runnerCreateMutation from '~/ci/runner/graphql/new/runner_create.mutation.graphql';
import { captureException } from '~/ci/runner/sentry_utils';
import { runnerCreateResult } from '../mock_data';
jest.mock('~/ci/runner/sentry_utils');
const mockCreatedRunner = runnerCreateResult.data.runnerCreate.runner;
const defaultRunnerModel = {
description: '',
accessLevel: DEFAULT_ACCESS_LEVEL,
paused: false,
maintenanceNote: '',
maximumTimeout: '',
runUntagged: false,
tagList: '',
};
Vue.use(VueApollo);
describe('RunnerCreateForm', () => {
let wrapper;
let runnerCreateHandler;
const findForm = () => wrapper.findComponent(GlForm);
const findRunnerFormFields = () => wrapper.findComponent(RunnerFormFields);
const findSubmitBtn = () => wrapper.find('[type="submit"]');
const createComponent = ({ props } = {}) => {
wrapper = shallowMountExtended(RunnerCreateForm, {
propsData: {
runnerType: INSTANCE_TYPE,
...props,
},
apolloProvider: createMockApollo([[runnerCreateMutation, runnerCreateHandler]]),
});
};
beforeEach(() => {
runnerCreateHandler = jest.fn().mockResolvedValue(runnerCreateResult);
});
it('shows default runner values', () => {
createComponent();
expect(findRunnerFormFields().props('value')).toEqual(defaultRunnerModel);
});
it('shows a submit button', () => {
createComponent();
expect(findSubmitBtn().exists()).toBe(true);
});
describe.each`
typeName | props | scopeData
${'an instance runner'} | ${{ runnerType: INSTANCE_TYPE }} | ${{ runnerType: INSTANCE_TYPE }}
${'a group runner'} | ${{ runnerType: GROUP_TYPE, groupId: 'gid://gitlab/Group/72' }} | ${{ runnerType: GROUP_TYPE, groupId: 'gid://gitlab/Group/72' }}
${'a project runner'} | ${{ runnerType: PROJECT_TYPE, projectId: 'gid://gitlab/Project/42' }} | ${{ runnerType: PROJECT_TYPE, projectId: 'gid://gitlab/Project/42' }}
`('when user submits $typeName', ({ props, scopeData }) => {
let preventDefault;
beforeEach(() => {
createComponent({ props });
preventDefault = jest.fn();
findRunnerFormFields().vm.$emit('input', {
...defaultRunnerModel,
description: 'My runner',
maximumTimeout: 0,
tagList: 'tag1, tag2',
});
});
describe('immediately after submit', () => {
beforeEach(() => {
findForm().vm.$emit('submit', { preventDefault });
});
it('prevents default form submission', () => {
expect(preventDefault).toHaveBeenCalledTimes(1);
});
it('shows a saving state', () => {
expect(findSubmitBtn().props('loading')).toBe(true);
});
it('saves runner', () => {
expect(runnerCreateHandler).toHaveBeenCalledWith({
input: {
...defaultRunnerModel,
...scopeData,
description: 'My runner',
maximumTimeout: 0,
tagList: ['tag1', 'tag2'],
},
});
});
});
describe('when saved successfully', () => {
beforeEach(async () => {
findForm().vm.$emit('submit', { preventDefault });
await waitForPromises();
});
it('emits "saved" result', () => {
expect(wrapper.emitted('saved')[0]).toEqual([mockCreatedRunner]);
});
it('does not show a saving state', () => {
expect(findSubmitBtn().props('loading')).toBe(false);
});
});
describe('when a server error occurs', () => {
const error = new Error('Error!');
beforeEach(async () => {
runnerCreateHandler.mockRejectedValue(error);
findForm().vm.$emit('submit', { preventDefault });
await waitForPromises();
});
it('emits "error" result', () => {
expect(wrapper.emitted('error')[0]).toEqual([error]);
});
it('does not show a saving state', () => {
expect(findSubmitBtn().props('loading')).toBe(false);
});
it('reports error', () => {
expect(captureException).toHaveBeenCalledTimes(1);
expect(captureException).toHaveBeenCalledWith({
component: 'RunnerCreateForm',
error,
});
});
});
describe('when a validation error occurs', () => {
const errorMsg1 = 'Issue1!';
const errorMsg2 = 'Issue2!';
beforeEach(async () => {
runnerCreateHandler.mockResolvedValue({
data: {
runnerCreate: {
errors: [errorMsg1, errorMsg2],
runner: null,
},
},
});
findForm().vm.$emit('submit', { preventDefault });
await waitForPromises();
});
it('emits "error" results', () => {
expect(wrapper.emitted('error')[0]).toEqual([new Error(`${errorMsg1} ${errorMsg2}`)]);
});
it('does not show a saving state', () => {
expect(findSubmitBtn().props('loading')).toBe(false);
});
it('does not report error', () => {
expect(captureException).not.toHaveBeenCalled();
});
});
});
});
|