Commit 39418022 authored by Denys Mishunov's avatar Denys Mishunov Committed by Illya Klymov

Prevent redirecting to snippet view on error

When getting seemingly successful response from the create/update
snippet mutation, we still should not redirect to the snippet view in
case 'errors' array is not empty
parent a0316ac9
......@@ -155,8 +155,9 @@ export default {
const errors = baseObj?.errors;
if (errors.length) {
this.flashAPIFailure(errors[0]);
} else {
redirectTo(baseObj.snippet.webUrl);
}
redirectTo(baseObj.snippet.webUrl);
})
.catch(e => {
this.flashAPIFailure(e);
......@@ -215,7 +216,7 @@ export default {
variant="success"
:disabled="updatePrevented"
data-qa-selector="submit_button"
@click="handleFormSubmit"
@click.prevent="handleFormSubmit"
>{{ saveButtonLabel }}</gl-button
>
</template>
......
......@@ -38,6 +38,7 @@ const rawPathMock = '/foo/bar';
const rawProjectPathMock = '/project/path';
const newlyEditedSnippetUrl = 'http://foo.bar';
const apiError = { message: 'Ufff' };
const mutationError = 'Bummer';
const defaultProps = {
snippetGid: 'gid://gitlab/PersonalSnippet/42',
......@@ -60,10 +61,26 @@ describe('Snippet Edit app', () => {
},
});
const resolveMutateWithErrors = jest.fn().mockResolvedValue({
data: {
updateSnippet: {
errors: [mutationError],
snippet: {
webUrl: newlyEditedSnippetUrl,
},
},
createSnippet: {
errors: [mutationError],
snippet: null,
},
},
});
const rejectMutation = jest.fn().mockRejectedValue(apiError);
const mutationTypes = {
RESOLVE: resolveMutate,
RESOLVE_WITH_ERRORS: resolveMutateWithErrors,
REJECT: rejectMutation,
};
......@@ -284,6 +301,35 @@ describe('Snippet Edit app', () => {
});
});
it.each`
newSnippet | projectPath | mutationName
${true} | ${rawProjectPathMock} | ${'CreateSnippetMutation with projectPath'}
${true} | ${''} | ${'CreateSnippetMutation without projectPath'}
${false} | ${rawProjectPathMock} | ${'UpdateSnippetMutation with projectPath'}
${false} | ${''} | ${'UpdateSnippetMutation without projectPath'}
`(
'does not redirect to snippet view if the seemingly successful' +
' $mutationName response contains errors',
({ newSnippet, projectPath }) => {
createComponent({
data: {
newSnippet,
},
props: {
...defaultProps,
projectPath,
},
mutationRes: mutationTypes.RESOLVE_WITH_ERRORS,
});
wrapper.vm.handleFormSubmit();
return waitForPromises().then(() => {
expect(redirectTo).not.toHaveBeenCalled();
expect(flashSpy).toHaveBeenCalledWith(mutationError);
});
},
);
it('flashes an error if mutation failed', () => {
createComponent({
mutationRes: mutationTypes.REJECT,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment