Commit 856bb872 authored by Denys Mishunov's avatar Denys Mishunov

Switched to using provide/inject

Instead of setting up the local Apollo cache for storing
immutable data we benefit the simpler provide/inject
mechanism
parent 37dc2817
...@@ -16,12 +16,7 @@ import { performanceMarkAndMeasure } from '~/performance_utils'; ...@@ -16,12 +16,7 @@ import { performanceMarkAndMeasure } from '~/performance_utils';
import UpdateSnippetMutation from '../mutations/updateSnippet.mutation.graphql'; import UpdateSnippetMutation from '../mutations/updateSnippet.mutation.graphql';
import CreateSnippetMutation from '../mutations/createSnippet.mutation.graphql'; import CreateSnippetMutation from '../mutations/createSnippet.mutation.graphql';
import { getSnippetMixin } from '../mixins/snippets'; import { getSnippetMixin } from '../mixins/snippets';
import { import { SNIPPET_CREATE_MUTATION_ERROR, SNIPPET_UPDATE_MUTATION_ERROR } from '../constants';
SNIPPET_CREATE_MUTATION_ERROR,
SNIPPET_UPDATE_MUTATION_ERROR,
SNIPPET_VISIBILITY_PRIVATE,
} from '../constants';
import defaultVisibilityQuery from '../queries/snippet_visibility.query.graphql';
import { markBlobPerformance } from '../utils/blob'; import { markBlobPerformance } from '../utils/blob';
import SnippetBlobActionsEdit from './snippet_blob_actions_edit.vue'; import SnippetBlobActionsEdit from './snippet_blob_actions_edit.vue';
...@@ -41,15 +36,7 @@ export default { ...@@ -41,15 +36,7 @@ export default {
GlLoadingIcon, GlLoadingIcon,
}, },
mixins: [getSnippetMixin], mixins: [getSnippetMixin],
apollo: { inject: ['selectedLevel'],
defaultVisibility: {
query: defaultVisibilityQuery,
manual: true,
result({ data: { selectedLevel } }) {
this.snippet.visibilityLevel = selectedLevel;
},
},
},
props: { props: {
markdownPreviewPath: { markdownPreviewPath: {
type: String, type: String,
...@@ -77,7 +64,7 @@ export default { ...@@ -77,7 +64,7 @@ export default {
snippet: { snippet: {
title: '', title: '',
description: '', description: '',
visibilityLevel: SNIPPET_VISIBILITY_PRIVATE, visibilityLevel: this.selectedLevel,
}, },
}; };
}, },
......
<script> <script>
import { GlIcon, GlFormGroup, GlFormRadio, GlFormRadioGroup, GlLink } from '@gitlab/ui'; import { GlIcon, GlFormGroup, GlFormRadio, GlFormRadioGroup, GlLink } from '@gitlab/ui';
import defaultVisibilityQuery from '../queries/snippet_visibility.query.graphql';
import { defaultSnippetVisibilityLevels } from '../utils/blob'; import { defaultSnippetVisibilityLevels } from '../utils/blob';
import { SNIPPET_LEVELS_RESTRICTED, SNIPPET_LEVELS_DISABLED } from '~/snippets/constants'; import { SNIPPET_LEVELS_RESTRICTED, SNIPPET_LEVELS_DISABLED } from '~/snippets/constants';
...@@ -12,16 +11,7 @@ export default { ...@@ -12,16 +11,7 @@ export default {
GlFormRadioGroup, GlFormRadioGroup,
GlLink, GlLink,
}, },
apollo: { inject: ['visibilityLevels', 'multipleLevelsRestricted'],
defaultVisibility: {
query: defaultVisibilityQuery,
manual: true,
result({ data: { visibilityLevels, multipleLevelsRestricted } }) {
this.visibilityLevels = defaultSnippetVisibilityLevels(visibilityLevels);
this.multipleLevelsRestricted = multipleLevelsRestricted;
},
},
},
props: { props: {
helpLink: { helpLink: {
type: String, type: String,
...@@ -38,11 +28,10 @@ export default { ...@@ -38,11 +28,10 @@ export default {
required: true, required: true,
}, },
}, },
data() { computed: {
return { defaultVisibilityLevels() {
visibilityLevels: [], return defaultSnippetVisibilityLevels(this.visibilityLevels) || [];
multipleLevelsRestricted: false, },
};
}, },
SNIPPET_LEVELS_DISABLED, SNIPPET_LEVELS_DISABLED,
SNIPPET_LEVELS_RESTRICTED, SNIPPET_LEVELS_RESTRICTED,
...@@ -59,7 +48,7 @@ export default { ...@@ -59,7 +48,7 @@ export default {
<gl-form-group id="visibility-level-setting" class="gl-mb-0"> <gl-form-group id="visibility-level-setting" class="gl-mb-0">
<gl-form-radio-group :checked="value" stacked v-bind="$attrs" v-on="$listeners"> <gl-form-radio-group :checked="value" stacked v-bind="$attrs" v-on="$listeners">
<gl-form-radio <gl-form-radio
v-for="option in visibilityLevels" v-for="option in defaultVisibilityLevels"
:key="option.value" :key="option.value"
:value="option.value" :value="option.value"
class="mb-3" class="mb-3"
...@@ -78,7 +67,9 @@ export default { ...@@ -78,7 +67,9 @@ export default {
</gl-form-group> </gl-form-group>
<div class="text-muted" data-testid="restricted-levels-info"> <div class="text-muted" data-testid="restricted-levels-info">
<template v-if="!visibilityLevels.length">{{ $options.SNIPPET_LEVELS_DISABLED }}</template> <template v-if="!defaultVisibilityLevels.length">{{
$options.SNIPPET_LEVELS_DISABLED
}}</template>
<template v-else-if="multipleLevelsRestricted">{{ <template v-else-if="multipleLevelsRestricted">{{
$options.SNIPPET_LEVELS_RESTRICTED $options.SNIPPET_LEVELS_RESTRICTED
}}</template> }}</template>
......
...@@ -24,17 +24,14 @@ export default function appFactory(el, Component) { ...@@ -24,17 +24,14 @@ export default function appFactory(el, Component) {
...restDataset ...restDataset
} = el.dataset; } = el.dataset;
apolloProvider.clients.defaultClient.cache.writeData({ return new Vue({
data: { el,
apolloProvider,
provide: {
visibilityLevels: JSON.parse(visibilityLevels), visibilityLevels: JSON.parse(visibilityLevels),
selectedLevel: SNIPPET_LEVELS_MAP[selectedLevel] ?? SNIPPET_VISIBILITY_PRIVATE, selectedLevel: SNIPPET_LEVELS_MAP[selectedLevel] ?? SNIPPET_VISIBILITY_PRIVATE,
multipleLevelsRestricted: 'multipleLevelsRestricted' in el.dataset, multipleLevelsRestricted: 'multipleLevelsRestricted' in el.dataset,
}, },
});
return new Vue({
el,
apolloProvider,
render(createElement) { render(createElement) {
return createElement(Component, { return createElement(Component, {
props: { props: {
......
...@@ -88,8 +88,8 @@ describe('Snippet Edit app', () => { ...@@ -88,8 +88,8 @@ describe('Snippet Edit app', () => {
props = {}, props = {},
loading = false, loading = false,
mutationRes = mutationTypes.RESOLVE, mutationRes = mutationTypes.RESOLVE,
selectedLevel = SNIPPET_VISIBILITY_PRIVATE,
withApollo = false, withApollo = false,
apolloData = { selectedLevel: SNIPPET_VISIBILITY_PRIVATE },
} = {}) { } = {}) {
let componentData = { let componentData = {
mocks: { mocks: {
...@@ -100,13 +100,6 @@ describe('Snippet Edit app', () => { ...@@ -100,13 +100,6 @@ describe('Snippet Edit app', () => {
mutate: mutationRes, mutate: mutationRes,
}, },
}, },
data() {
return {
snippet: {
visibilityLevel: SNIPPET_VISIBILITY_PRIVATE,
},
};
},
}; };
if (wrapper) { if (wrapper) {
...@@ -119,11 +112,6 @@ describe('Snippet Edit app', () => { ...@@ -119,11 +112,6 @@ describe('Snippet Edit app', () => {
const requestHandlers = [[GetSnippetQuery, GetSnippetQuerySpy]]; const requestHandlers = [[GetSnippetQuery, GetSnippetQuerySpy]];
fakeApollo = createMockApollo(requestHandlers); fakeApollo = createMockApollo(requestHandlers);
fakeApollo.clients.defaultClient.cache.writeData({
data: {
...apolloData,
},
});
componentData = { componentData = {
localVue, localVue,
apolloProvider: fakeApollo, apolloProvider: fakeApollo,
...@@ -136,6 +124,9 @@ describe('Snippet Edit app', () => { ...@@ -136,6 +124,9 @@ describe('Snippet Edit app', () => {
ApolloMutation, ApolloMutation,
FormFooterActions, FormFooterActions,
}, },
provide: {
selectedLevel,
},
propsData: { propsData: {
snippetGid: 'gid://gitlab/PersonalSnippet/42', snippetGid: 'gid://gitlab/PersonalSnippet/42',
markdownPreviewPath: 'http://preview.foo.bar', markdownPreviewPath: 'http://preview.foo.bar',
...@@ -273,14 +264,8 @@ describe('Snippet Edit app', () => { ...@@ -273,14 +264,8 @@ describe('Snippet Edit app', () => {
async visibility => { async visibility => {
createComponent({ createComponent({
props: { snippetGid: '' }, props: { snippetGid: '' },
withApollo: true, selectedLevel: visibility,
apolloData: {
selectedLevel: visibility,
},
}); });
jest.runOnlyPendingTimers();
await wrapper.vm.$nextTick();
expect(wrapper.vm.snippet.visibilityLevel).toEqual(visibility); expect(wrapper.vm.snippet.visibilityLevel).toEqual(visibility);
}, },
); );
......
import { GlFormRadio, GlIcon, GlFormRadioGroup, GlLink } from '@gitlab/ui'; import { GlFormRadio, GlIcon, GlFormRadioGroup, GlLink } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils'; import { mount, shallowMount } from '@vue/test-utils';
import SnippetVisibilityEdit from '~/snippets/components/snippet_visibility_edit.vue'; import SnippetVisibilityEdit from '~/snippets/components/snippet_visibility_edit.vue';
import { defaultSnippetVisibilityLevels } from '~/snippets/utils/blob';
import { import {
SNIPPET_VISIBILITY, SNIPPET_VISIBILITY,
SNIPPET_VISIBILITY_PRIVATE, SNIPPET_VISIBILITY_PRIVATE,
...@@ -15,36 +14,25 @@ describe('Snippet Visibility Edit component', () => { ...@@ -15,36 +14,25 @@ describe('Snippet Visibility Edit component', () => {
let wrapper; let wrapper;
const defaultHelpLink = '/foo/bar'; const defaultHelpLink = '/foo/bar';
const defaultVisibilityLevel = 'private'; const defaultVisibilityLevel = 'private';
const defaultVisibility = defaultSnippetVisibilityLevels([0, 10, 20]);
function createComponent({ function createComponent({
propsData = {}, propsData = {},
visibilityLevels = defaultVisibility, visibilityLevels = [0, 10, 20],
multipleLevelsRestricted = false, multipleLevelsRestricted = false,
deep = false, deep = false,
} = {}) { } = {}) {
const method = deep ? mount : shallowMount; const method = deep ? mount : shallowMount;
const $apollo = {
queries: {
defaultVisibility: {
loading: false,
},
},
};
wrapper = method.call(this, SnippetVisibilityEdit, { wrapper = method.call(this, SnippetVisibilityEdit, {
mock: { $apollo },
propsData: { propsData: {
helpLink: defaultHelpLink, helpLink: defaultHelpLink,
isProjectSnippet: false, isProjectSnippet: false,
value: defaultVisibilityLevel, value: defaultVisibilityLevel,
...propsData, ...propsData,
}, },
data() { provide: {
return { visibilityLevels,
visibilityLevels, multipleLevelsRestricted,
multipleLevelsRestricted,
};
}, },
}); });
} }
...@@ -108,7 +96,6 @@ describe('Snippet Visibility Edit component', () => { ...@@ -108,7 +96,6 @@ describe('Snippet Visibility Edit component', () => {
it.each` it.each`
levels | resultOptions levels | resultOptions
${undefined} | ${[]}
${''} | ${[]} ${''} | ${[]}
${[]} | ${[]} ${[]} | ${[]}
${[0]} | ${[RESULTING_OPTIONS[0]]} ${[0]} | ${[RESULTING_OPTIONS[0]]}
...@@ -117,7 +104,7 @@ describe('Snippet Visibility Edit component', () => { ...@@ -117,7 +104,7 @@ describe('Snippet Visibility Edit component', () => {
${[0, 20]} | ${[RESULTING_OPTIONS[0], RESULTING_OPTIONS[20]]} ${[0, 20]} | ${[RESULTING_OPTIONS[0], RESULTING_OPTIONS[20]]}
${[10, 20]} | ${[RESULTING_OPTIONS[10], RESULTING_OPTIONS[20]]} ${[10, 20]} | ${[RESULTING_OPTIONS[10], RESULTING_OPTIONS[20]]}
`('renders correct visibility options for $levels', ({ levels, resultOptions }) => { `('renders correct visibility options for $levels', ({ levels, resultOptions }) => {
createComponent({ visibilityLevels: defaultSnippetVisibilityLevels(levels), deep: true }); createComponent({ visibilityLevels: levels, deep: true });
expect(findRadiosData()).toEqual(resultOptions); expect(findRadiosData()).toEqual(resultOptions);
}); });
...@@ -132,7 +119,7 @@ describe('Snippet Visibility Edit component', () => { ...@@ -132,7 +119,7 @@ describe('Snippet Visibility Edit component', () => {
'renders correct information about restricted visibility levels for $levels', 'renders correct information about restricted visibility levels for $levels',
({ levels, levelsRestricted, resultText }) => { ({ levels, levelsRestricted, resultText }) => {
createComponent({ createComponent({
visibilityLevels: defaultSnippetVisibilityLevels(levels), visibilityLevels: levels,
multipleLevelsRestricted: levelsRestricted, multipleLevelsRestricted: levelsRestricted,
}); });
expect(findRestrictedInfo().text()).toBe(resultText); expect(findRestrictedInfo().text()).toBe(resultText);
......
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