Commit 37dc2817 authored by Denys Mishunov's avatar Denys Mishunov

Do not fetch snippet on snippet creation

Cleaned up the component as well to get rid of redundant props
parent d1d09af7
...@@ -46,7 +46,7 @@ export default { ...@@ -46,7 +46,7 @@ export default {
query: defaultVisibilityQuery, query: defaultVisibilityQuery,
manual: true, manual: true,
result({ data: { selectedLevel } }) { result({ data: { selectedLevel } }) {
this.selectedLevelDefault = selectedLevel; this.snippet.visibilityLevel = selectedLevel;
}, },
}, },
}, },
...@@ -73,9 +73,12 @@ export default { ...@@ -73,9 +73,12 @@ export default {
data() { data() {
return { return {
isUpdating: false, isUpdating: false,
newSnippet: false,
actions: [], actions: [],
selectedLevelDefault: SNIPPET_VISIBILITY_PRIVATE, snippet: {
title: '',
description: '',
visibilityLevel: SNIPPET_VISIBILITY_PRIVATE,
},
}; };
}, },
computed: { computed: {
...@@ -112,13 +115,6 @@ export default { ...@@ -112,13 +115,6 @@ export default {
} }
return this.snippet.webUrl; return this.snippet.webUrl;
}, },
newSnippetSchema() {
return {
title: '',
description: '',
visibilityLevel: this.selectedLevelDefault,
};
},
}, },
beforeCreate() { beforeCreate() {
performanceMarkAndMeasure({ mark: SNIPPET_MARK_EDIT_APP_START }); performanceMarkAndMeasure({ mark: SNIPPET_MARK_EDIT_APP_START });
...@@ -145,20 +141,6 @@ export default { ...@@ -145,20 +141,6 @@ export default {
Flash(sprintf(defaultErrorMsg, { err })); Flash(sprintf(defaultErrorMsg, { err }));
this.isUpdating = false; this.isUpdating = false;
}, },
onNewSnippetFetched() {
this.newSnippet = true;
this.snippet = this.newSnippetSchema;
},
onExistingSnippetFetched() {
this.newSnippet = false;
},
onSnippetFetch(snippetRes) {
if (snippetRes.data.snippets.nodes.length === 0) {
this.onNewSnippetFetched();
} else {
this.onExistingSnippetFetched();
}
},
getAttachedFiles() { getAttachedFiles() {
const fileInputs = Array.from(this.$el.querySelectorAll('[name="files[]"]')); const fileInputs = Array.from(this.$el.querySelectorAll('[name="files[]"]'));
return fileInputs.map(node => node.value); return fileInputs.map(node => node.value);
...@@ -209,7 +191,7 @@ export default { ...@@ -209,7 +191,7 @@ export default {
</script> </script>
<template> <template>
<form <form
class="snippet-form js-requires-input js-quick-submit common-note-form" class="snippet-form js-quick-submit common-note-form"
:data-snippet-type="isProjectSnippet ? 'project' : 'personal'" :data-snippet-type="isProjectSnippet ? 'project' : 'personal'"
data-testid="snippet-edit-form" data-testid="snippet-edit-form"
@submit.prevent="handleFormSubmit" @submit.prevent="handleFormSubmit"
......
...@@ -21,9 +21,9 @@ export const getSnippetMixin = { ...@@ -21,9 +21,9 @@ export const getSnippetMixin = {
}, },
result(res) { result(res) {
this.blobs = res.data.snippets.nodes[0]?.blobs || blobsDefault; this.blobs = res.data.snippets.nodes[0]?.blobs || blobsDefault;
if (this.onSnippetFetch) { },
this.onSnippetFetch(res); skip() {
} return this.newSnippet;
}, },
}, },
}, },
...@@ -36,7 +36,7 @@ export const getSnippetMixin = { ...@@ -36,7 +36,7 @@ export const getSnippetMixin = {
data() { data() {
return { return {
snippet: {}, snippet: {},
newSnippet: false, newSnippet: !this.snippetGid,
blobs: blobsDefault, blobs: blobsDefault,
}; };
}, },
......
...@@ -12,10 +12,13 @@ import SnippetVisibilityEdit from '~/snippets/components/snippet_visibility_edit ...@@ -12,10 +12,13 @@ import SnippetVisibilityEdit from '~/snippets/components/snippet_visibility_edit
import SnippetBlobActionsEdit from '~/snippets/components/snippet_blob_actions_edit.vue'; import SnippetBlobActionsEdit from '~/snippets/components/snippet_blob_actions_edit.vue';
import TitleField from '~/vue_shared/components/form/title.vue'; import TitleField from '~/vue_shared/components/form/title.vue';
import FormFooterActions from '~/vue_shared/components/form/form_footer_actions.vue'; import FormFooterActions from '~/vue_shared/components/form/form_footer_actions.vue';
import { SNIPPET_VISIBILITY_PRIVATE } from '~/snippets/constants'; import {
SNIPPET_VISIBILITY_PRIVATE,
SNIPPET_VISIBILITY_INTERNAL,
SNIPPET_VISIBILITY_PUBLIC,
} from '~/snippets/constants';
import UpdateSnippetMutation from '~/snippets/mutations/updateSnippet.mutation.graphql'; import UpdateSnippetMutation from '~/snippets/mutations/updateSnippet.mutation.graphql';
import CreateSnippetMutation from '~/snippets/mutations/createSnippet.mutation.graphql'; import CreateSnippetMutation from '~/snippets/mutations/createSnippet.mutation.graphql';
import defaultVisibilityQuery from '~/snippets/queries/snippet_visibility.query.graphql';
import { testEntries } from '../test_utils'; import { testEntries } from '../test_utils';
jest.mock('~/flash'); jest.mock('~/flash');
...@@ -53,7 +56,9 @@ describe('Snippet Edit app', () => { ...@@ -53,7 +56,9 @@ describe('Snippet Edit app', () => {
let fakeApollo; let fakeApollo;
const relativeUrlRoot = '/foo/'; const relativeUrlRoot = '/foo/';
const originalRelativeUrlRoot = gon.relative_url_root; const originalRelativeUrlRoot = gon.relative_url_root;
const GetSnippetQuerySpy = jest.fn().mockResolvedValue(createTestSnippet()); const GetSnippetQuerySpy = jest.fn().mockResolvedValue({
data: { snippets: { nodes: [createTestSnippet()] } },
});
const mutationTypes = { const mutationTypes = {
RESOLVE: jest.fn().mockResolvedValue({ RESOLVE: jest.fn().mockResolvedValue({
...@@ -84,6 +89,7 @@ describe('Snippet Edit app', () => { ...@@ -84,6 +89,7 @@ describe('Snippet Edit app', () => {
loading = false, loading = false,
mutationRes = mutationTypes.RESOLVE, mutationRes = mutationTypes.RESOLVE,
withApollo = false, withApollo = false,
apolloData = { selectedLevel: SNIPPET_VISIBILITY_PRIVATE },
} = {}) { } = {}) {
let componentData = { let componentData = {
mocks: { mocks: {
...@@ -94,6 +100,13 @@ describe('Snippet Edit app', () => { ...@@ -94,6 +100,13 @@ describe('Snippet Edit app', () => {
mutate: mutationRes, mutate: mutationRes,
}, },
}, },
data() {
return {
snippet: {
visibilityLevel: SNIPPET_VISIBILITY_PRIVATE,
},
};
},
}; };
if (wrapper) { if (wrapper) {
...@@ -104,18 +117,13 @@ describe('Snippet Edit app', () => { ...@@ -104,18 +117,13 @@ describe('Snippet Edit app', () => {
const localVue = createLocalVue(); const localVue = createLocalVue();
localVue.use(VueApollo); localVue.use(VueApollo);
const requestHandlers = [ const requestHandlers = [[GetSnippetQuery, GetSnippetQuerySpy]];
[GetSnippetQuery, GetSnippetQuerySpy],
[
defaultVisibilityQuery,
jest.fn().mockResolvedValue({
visibilityLevels: expect.anything(),
selectedLevel: SNIPPET_VISIBILITY_PRIVATE,
multipleLevelsRestricted: expect.anything(),
}),
],
];
fakeApollo = createMockApollo(requestHandlers); fakeApollo = createMockApollo(requestHandlers);
fakeApollo.clients.defaultClient.cache.writeData({
data: {
...apolloData,
},
});
componentData = { componentData = {
localVue, localVue,
apolloProvider: fakeApollo, apolloProvider: fakeApollo,
...@@ -134,13 +142,6 @@ describe('Snippet Edit app', () => { ...@@ -134,13 +142,6 @@ describe('Snippet Edit app', () => {
markdownDocsPath: 'http://docs.foo.bar', markdownDocsPath: 'http://docs.foo.bar',
...props, ...props,
}, },
data() {
return {
snippet: {
visibilityLevel: SNIPPET_VISIBILITY_PRIVATE,
},
};
},
}); });
} }
...@@ -212,15 +213,6 @@ describe('Snippet Edit app', () => { ...@@ -212,15 +213,6 @@ describe('Snippet Edit app', () => {
}, },
); );
it('does not make the query to fetch snippet on create', async () => {
createComponent({ props: { snippetGid: '' }, withApollo: true });
jest.runOnlyPendingTimers();
await wrapper.vm.$nextTick();
expect(GetSnippetQuerySpy).not.toHaveBeenCalled();
});
it.each` it.each`
title | actions | shouldDisable title | actions | shouldDisable
${''} | ${[]} | ${true} ${''} | ${[]} | ${true}
...@@ -266,6 +258,34 @@ describe('Snippet Edit app', () => { ...@@ -266,6 +258,34 @@ describe('Snippet Edit app', () => {
}); });
describe('functionality', () => { describe('functionality', () => {
it('does not fetch snippet when create a new snippet', async () => {
createComponent({ props: { snippetGid: '' }, withApollo: true });
jest.runOnlyPendingTimers();
await wrapper.vm.$nextTick();
expect(GetSnippetQuerySpy).not.toHaveBeenCalled();
});
describe('default visibility', () => {
it.each([SNIPPET_VISIBILITY_PRIVATE, SNIPPET_VISIBILITY_INTERNAL, SNIPPET_VISIBILITY_PUBLIC])(
'marks %s visibility by default',
async visibility => {
createComponent({
props: { snippetGid: '' },
withApollo: true,
apolloData: {
selectedLevel: visibility,
},
});
jest.runOnlyPendingTimers();
await wrapper.vm.$nextTick();
expect(wrapper.vm.snippet.visibilityLevel).toEqual(visibility);
},
);
});
describe('form submission handling', () => { describe('form submission handling', () => {
it.each` it.each`
snippetArg | projectPath | uploadedFiles | input | mutation snippetArg | projectPath | uploadedFiles | input | mutation
......
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