Commit 6e7b56e2 authored by Paul Slaughter's avatar Paul Slaughter

fixup! Add/remove snippet files in the edit view

parent 5c537cdc
...@@ -56,7 +56,6 @@ export default { ...@@ -56,7 +56,6 @@ export default {
isUpdating: false, isUpdating: false,
newSnippet: false, newSnippet: false,
actions: [], actions: [],
isLoaded: false,
}; };
}, },
computed: { computed: {
...@@ -133,8 +132,6 @@ export default { ...@@ -133,8 +132,6 @@ export default {
this.newSnippet = false; this.newSnippet = false;
}, },
onSnippetFetch(snippetRes) { onSnippetFetch(snippetRes) {
this.isLoaded = true;
if (snippetRes.data.snippets.edges.length === 0) { if (snippetRes.data.snippets.edges.length === 0) {
this.onNewSnippetFetched(); this.onNewSnippetFetched();
} else { } else {
...@@ -221,11 +218,7 @@ export default { ...@@ -221,11 +218,7 @@ export default {
:markdown-preview-path="markdownPreviewPath" :markdown-preview-path="markdownPreviewPath"
:markdown-docs-path="markdownDocsPath" :markdown-docs-path="markdownDocsPath"
/> />
<snippet-blob-actions-edit <snippet-blob-actions-edit :init-blobs="blobs" @actions="updateActions" />
:is-ready="isLoaded"
:init-blobs="blobs"
@actions="updateActions"
/>
<snippet-visibility-edit <snippet-visibility-edit
v-model="snippet.visibilityLevel" v-model="snippet.visibilityLevel"
......
...@@ -14,15 +14,12 @@ export default { ...@@ -14,15 +14,12 @@ export default {
}, },
mixins: [glFeatureFlagsMixin()], mixins: [glFeatureFlagsMixin()],
props: { props: {
// TODO: Build reactivity for initBlobs.
// What happens if this changes (i.e. blobs are being loaded through pages)?
initBlobs: { initBlobs: {
type: Array, type: Array,
required: true, required: true,
}, },
isReady: {
type: Boolean,
required: false,
default: true,
},
}, },
data() { data() {
return { return {
...@@ -74,14 +71,6 @@ export default { ...@@ -74,14 +71,6 @@ export default {
}, },
}, },
watch: { watch: {
isReady: {
immediate: true,
handler(val) {
if (val) {
this.setup();
}
},
},
actions: { actions: {
immediate: true, immediate: true,
handler(val) { handler(val) {
...@@ -89,20 +78,20 @@ export default { ...@@ -89,20 +78,20 @@ export default {
}, },
}, },
}, },
methods: { created() {
setup() { const blobs = this.initBlobs.map(decorateBlob);
const blobs = this.initBlobs.map(decorateBlob); const blobsById = blobs.reduce((acc, x) => Object.assign(acc, { [x.id]: x }), {});
const blobsById = blobs.reduce((acc, x) => Object.assign(acc, { [x.id]: x }), {});
this.blobsOrig = blobsById; this.blobsOrig = blobsById;
this.blobs = cloneDeep(blobsById); this.blobs = cloneDeep(blobsById);
this.blobIds = blobs.map(x => x.id); this.blobIds = blobs.map(x => x.id);
// Show 1 empty blob if none exist // Show 1 empty blob if none exist
if (!this.blobIds.length) { if (!this.blobIds.length) {
this.addBlob(); this.addBlob();
} }
}, },
methods: {
updateBlobContent(id, content) { updateBlobContent(id, content) {
const origBlob = this.blobsOrig[id]; const origBlob = this.blobsOrig[id];
const blob = this.blobs[id]; const blob = this.blobs[id];
......
...@@ -18,13 +18,12 @@ export default { ...@@ -18,13 +18,12 @@ export default {
props: { props: {
blob: { blob: {
type: Object, type: Object,
required: false, required: true,
default: null,
}, },
canDelete: { canDelete: {
type: Boolean, type: Boolean,
required: false, required: false,
default: false, default: true,
}, },
showDelete: { showDelete: {
type: Boolean, type: Boolean,
......
...@@ -5,6 +5,7 @@ exports[`Snippet Blob Edit component with loaded blob matches snapshot 1`] = ` ...@@ -5,6 +5,7 @@ exports[`Snippet Blob Edit component with loaded blob matches snapshot 1`] = `
class="file-holder snippet" class="file-holder snippet"
> >
<blob-header-edit-stub <blob-header-edit-stub
candelete="true"
data-qa-selector="file_name_field" data-qa-selector="file_name_field"
id="blob_local_7_file_path" id="blob_local_7_file_path"
value="foo/bar/test.md" value="foo/bar/test.md"
......
...@@ -23,7 +23,6 @@ describe('snippets/components/snippet_blob_actions_edit', () => { ...@@ -23,7 +23,6 @@ describe('snippets/components/snippet_blob_actions_edit', () => {
wrapper = shallowMount(SnippetBlobActionsEdit, { wrapper = shallowMount(SnippetBlobActionsEdit, {
propsData: { propsData: {
initBlobs: TEST_BLOBS, initBlobs: TEST_BLOBS,
isReady: false,
...props, ...props,
}, },
provide: { provide: {
...@@ -76,7 +75,7 @@ describe('snippets/components/snippet_blob_actions_edit', () => { ...@@ -76,7 +75,7 @@ describe('snippets/components/snippet_blob_actions_edit', () => {
${false} | ${'File'} | ${false} | ${false} ${false} | ${'File'} | ${false} | ${false}
`('with feature flag = $featureFlag', ({ featureFlag, label, showDelete, showAdd }) => { `('with feature flag = $featureFlag', ({ featureFlag, label, showDelete, showAdd }) => {
beforeEach(() => { beforeEach(() => {
createComponent({ isReady: true }, featureFlag); createComponent({}, featureFlag);
}); });
it('renders label', () => { it('renders label', () => {
...@@ -100,132 +99,118 @@ describe('snippets/components/snippet_blob_actions_edit', () => { ...@@ -100,132 +99,118 @@ describe('snippets/components/snippet_blob_actions_edit', () => {
createComponent(); createComponent();
}); });
it('has no blobs yet', () => { it('emits no actions', () => {
expect(findBlobsData()).toEqual([]); expect(getLastActions()).toEqual([]);
}); });
describe('when isReady', () => { it('shows blobs', () => {
beforeEach(async () => { expect(findBlobsData()).toEqual(buildBlobsDataExpectation(TEST_BLOBS_UNLOADED));
wrapper.setProps({ isReady: true }); });
await wrapper.vm.$nextTick(); it('shows add button', () => {
}); const button = findAddButton();
it('emits no actions', () => { expect(button.text()).toBe(`Add another file ${TEST_BLOBS.length}/${SNIPPET_MAX_BLOBS}`);
expect(getLastActions()).toEqual([]); expect(button.props('disabled')).toBe(false);
}); });
it('shows blobs', () => { describe('when add is clicked', () => {
expect(findBlobsData()).toEqual(buildBlobsDataExpectation(TEST_BLOBS_UNLOADED)); beforeEach(() => {
findAddButton().vm.$emit('click');
}); });
it('shows add button', () => { it('adds blob with empty content', () => {
const button = findAddButton(); expect(findBlobsData()).toEqual(
buildBlobsDataExpectation([
expect(button.text()).toBe(`Add another file ${TEST_BLOBS.length}/${SNIPPET_MAX_BLOBS}`); ...TEST_BLOBS_UNLOADED,
expect(button.props('disabled')).toBe(false); {
content: '',
isLoaded: true,
path: '',
},
]),
);
}); });
describe('when add is clicked', () => { it('emits action', () => {
beforeEach(() => { expect(getLastActions()).toEqual([
findAddButton().vm.$emit('click'); expect.objectContaining({
}); action: SNIPPET_BLOB_ACTION_CREATE,
}),
it('adds blob with empty content', () => { ]);
expect(findBlobsData()).toEqual(
buildBlobsDataExpectation([
...TEST_BLOBS_UNLOADED,
{
content: '',
isLoaded: true,
path: '',
},
]),
);
});
it('emits action', () => {
expect(getLastActions()).toEqual([
expect.objectContaining({
action: SNIPPET_BLOB_ACTION_CREATE,
}),
]);
});
}); });
});
describe('when blob is deleted', () => { describe('when blob is deleted', () => {
beforeEach(() => { beforeEach(() => {
triggerBlobDelete(1); triggerBlobDelete(1);
}); });
it('removes blob', () => { it('removes blob', () => {
expect(findBlobsData()).toEqual( expect(findBlobsData()).toEqual(buildBlobsDataExpectation(TEST_BLOBS_UNLOADED.slice(0, 1)));
buildBlobsDataExpectation(TEST_BLOBS_UNLOADED.slice(0, 1)), });
);
});
it('emits action', () => { it('emits action', () => {
expect(getLastActions()).toEqual([ expect(getLastActions()).toEqual([
expect.objectContaining({ expect.objectContaining({
...testEntries.deleted.diff, ...testEntries.deleted.diff,
content: '', content: '',
}), }),
]); ]);
});
}); });
});
describe('when blob changes path', () => { describe('when blob changes path', () => {
beforeEach(() => { beforeEach(() => {
triggerBlobUpdate(0, { path: 'new/path' }); triggerBlobUpdate(0, { path: 'new/path' });
}); });
it('renames blob', () => { it('renames blob', () => {
expect(findBlobsData()[0]).toMatchObject({ expect(findBlobsData()[0]).toMatchObject({
blob: { blob: {
path: 'new/path', path: 'new/path',
}, },
});
}); });
});
it('emits action', () => { it('emits action', () => {
expect(getLastActions()).toMatchObject([ expect(getLastActions()).toMatchObject([
{ {
action: SNIPPET_BLOB_ACTION_MOVE, action: SNIPPET_BLOB_ACTION_MOVE,
filePath: 'new/path', filePath: 'new/path',
previousPath: testEntries.updated.diff.filePath, previousPath: testEntries.updated.diff.filePath,
}, },
]); ]);
});
}); });
});
describe('when blob emits new content', () => { describe('when blob emits new content', () => {
const { content } = testEntries.updated.diff; const { content } = testEntries.updated.diff;
const originalContent = `${content}\noriginal content\n`; const originalContent = `${content}\noriginal content\n`;
beforeEach(() => { beforeEach(() => {
triggerBlobUpdate(0, { content: originalContent }); triggerBlobUpdate(0, { content: originalContent });
}); });
it('loads new content', () => { it('loads new content', () => {
expect(findBlobsData()[0]).toMatchObject({ expect(findBlobsData()[0]).toMatchObject({
blob: { blob: {
content: originalContent, content: originalContent,
isLoaded: true, isLoaded: true,
}, },
});
}); });
});
it('does not emit an action', () => { it('does not emit an action', () => {
expect(getLastActions()).toEqual([]); expect(getLastActions()).toEqual([]);
}); });
it('emits an action when content changes again', async () => { it('emits an action when content changes again', async () => {
triggerBlobUpdate(0, { content }); triggerBlobUpdate(0, { content });
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
expect(getLastActions()).toEqual([testEntries.updated.diff]); expect(getLastActions()).toEqual([testEntries.updated.diff]);
});
}); });
}); });
}); });
......
...@@ -141,7 +141,7 @@ describe('Snippet Blob Edit component', () => { ...@@ -141,7 +141,7 @@ describe('Snippet Blob Edit component', () => {
}); });
it('shows blob header', () => { it('shows blob header', () => {
const { canDelete = false, showDelete = false } = props; const { canDelete = true, showDelete = false } = props;
expect(findHeader().props()).toMatchObject({ expect(findHeader().props()).toMatchObject({
canDelete, canDelete,
......
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