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
|
import { GlButton } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { featureToMutationMap } from 'ee_else_ce/security_configuration/components/constants';
import createMockApollo from 'helpers/mock_apollo_helper';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { humanize } from '~/lib/utils/text_utility';
import { redirectTo } from '~/lib/utils/url_utility'; // eslint-disable-line import/no-deprecated
import ManageViaMr, {
i18n,
} from '~/vue_shared/security_configuration/components/manage_via_mr.vue';
import { REPORT_TYPE_SAST } from '~/vue_shared/security_reports/constants';
import { buildConfigureSecurityFeatureMockFactory } from './apollo_mocks';
jest.mock('~/lib/utils/url_utility');
Vue.use(VueApollo);
const projectFullPath = 'namespace/project';
const ufErrorPrefix = 'Foo:';
describe('ManageViaMr component', () => {
let wrapper;
const findButton = () => wrapper.findComponent(GlButton);
function createMockApolloProvider(mutation, handler) {
const requestHandlers = [[mutation, handler]];
return createMockApollo(requestHandlers);
}
function createComponent({
featureName = 'SAST',
featureType = 'sast',
isFeatureConfigured = false,
variant = undefined,
category = undefined,
...options
} = {}) {
wrapper = extendedWrapper(
mount(ManageViaMr, {
provide: {
projectFullPath,
},
propsData: {
feature: {
name: featureName,
type: featureType,
configured: isFeatureConfigured,
},
variant,
category,
},
...options,
}),
);
}
beforeEach(() => {
gon.uf_error_prefix = ufErrorPrefix;
});
// This component supports different report types/mutations depending on
// whether it's in a CE or EE context. This makes sure we are only testing
// the ones available in the current test context.
const supportedReportTypes = Object.entries(featureToMutationMap).map(
([featureType, { getMutationPayload, mutationId }]) => {
const { mutation, variables: mutationVariables } = getMutationPayload(projectFullPath);
return [humanize(featureType), featureType, mutation, mutationId, mutationVariables];
},
);
describe.each(supportedReportTypes)(
'%s',
(featureName, featureType, mutation, mutationId, mutationVariables) => {
const buildConfigureSecurityFeatureMock = buildConfigureSecurityFeatureMockFactory(
mutationId,
);
const successHandler = jest.fn().mockResolvedValue(buildConfigureSecurityFeatureMock());
const noSuccessPathHandler = jest.fn().mockResolvedValue(
buildConfigureSecurityFeatureMock({
successPath: '',
}),
);
const errorHandler = (message = 'foo') => {
return Promise.resolve(
buildConfigureSecurityFeatureMock({
errors: [message],
}),
);
};
const pendingHandler = () => new Promise(() => {});
describe('when feature is configured', () => {
beforeEach(() => {
const apolloProvider = createMockApolloProvider(mutation, successHandler);
createComponent({ apolloProvider, featureName, featureType, isFeatureConfigured: true });
});
it('does not render a button', () => {
expect(findButton().exists()).toBe(false);
});
});
describe('when feature is not configured', () => {
beforeEach(() => {
const apolloProvider = createMockApolloProvider(mutation, successHandler);
createComponent({ apolloProvider, featureName, featureType, isFeatureConfigured: false });
});
it('does render a button', () => {
expect(findButton().exists()).toBe(true);
});
it('clicking on the button triggers the configure mutation', () => {
findButton().trigger('click');
expect(successHandler).toHaveBeenCalledTimes(1);
expect(successHandler).toHaveBeenCalledWith(mutationVariables);
});
});
describe('given a pending response', () => {
beforeEach(() => {
const apolloProvider = createMockApolloProvider(mutation, pendingHandler);
createComponent({ apolloProvider, featureName, featureType });
});
it('renders spinner correctly', async () => {
const button = findButton();
expect(button.props('loading')).toBe(false);
await button.trigger('click');
expect(button.props('loading')).toBe(true);
});
});
describe('given a successful response', () => {
beforeEach(() => {
const apolloProvider = createMockApolloProvider(mutation, successHandler);
createComponent({ apolloProvider, featureName, featureType });
});
it('should call redirect helper with correct value', async () => {
await wrapper.trigger('click');
await waitForPromises();
expect(redirectTo).toHaveBeenCalledTimes(1); // eslint-disable-line import/no-deprecated
expect(redirectTo).toHaveBeenCalledWith('testSuccessPath'); // eslint-disable-line import/no-deprecated
// This is done for UX reasons. If the loading prop is set to false
// on success, then there's a period where the button is clickable
// again. Instead, we want the button to display a loading indicator
// for the remainder of the lifetime of the page (i.e., until the
// browser can start painting the new page it's been redirected to).
expect(findButton().props().loading).toBe(true);
});
});
describe.each`
handler | message
${noSuccessPathHandler} | ${`${featureName} merge request creation mutation failed`}
${errorHandler.bind(null, `${ufErrorPrefix} message`)} | ${'message'}
${errorHandler.bind(null, 'Blah: message')} | ${i18n.genericErrorText}
${errorHandler.bind(null, 'message')} | ${i18n.genericErrorText}
${errorHandler} | ${i18n.genericErrorText}
`('given an error response', ({ handler, message }) => {
beforeEach(() => {
const apolloProvider = createMockApolloProvider(mutation, handler);
createComponent({ apolloProvider, featureName, featureType });
});
it('should catch and emit error', async () => {
await wrapper.trigger('click');
await waitForPromises();
expect(wrapper.emitted('error')).toEqual([[message]]);
expect(findButton().props('loading')).toBe(false);
});
});
},
);
describe('canRender static method', () => {
it.each`
context | type | available | configured | canEnableByMergeRequest | expectedValue
${'an unconfigured feature'} | ${REPORT_TYPE_SAST} | ${true} | ${false} | ${true} | ${true}
${'a configured feature'} | ${REPORT_TYPE_SAST} | ${true} | ${true} | ${true} | ${false}
${'an unavailable feature'} | ${REPORT_TYPE_SAST} | ${false} | ${false} | ${true} | ${false}
${'a feature which cannot be enabled via MR'} | ${REPORT_TYPE_SAST} | ${true} | ${false} | ${false} | ${false}
${'an unknown feature'} | ${'foo'} | ${true} | ${false} | ${true} | ${false}
`(
'given $context returns $expectedValue',
({ type, available, configured, canEnableByMergeRequest, expectedValue }) => {
expect(
ManageViaMr.canRender({
type,
available,
configured,
canEnableByMergeRequest,
}),
).toBe(expectedValue);
},
);
});
describe('button props', () => {
it('passes the variant and category props to the GlButton', () => {
const variant = 'danger';
const category = 'tertiary';
createComponent({ variant, category });
expect(wrapper.findComponent(GlButton).props()).toMatchObject({
variant,
category,
});
});
});
});
|