Commit 481e3b00 authored by Tom Quirk's avatar Tom Quirk Committed by Kushal Pandya

Add design_input.vue component

Intended to be used for any design upload.

Only currently used in button.vue,
but will eventually be used for dropzone

CHanges behaviour slightly, to get upload
files from the @change event, rather than
the element.files itself
parent c9f50b1d
<script> <script>
import { GlButton, GlLoadingIcon, GlTooltipDirective } from '@gitlab/ui'; import { GlButton, GlLoadingIcon, GlTooltipDirective } from '@gitlab/ui';
import DesignInput from './design_input.vue';
export default { export default {
components: { components: {
GlButton, GlButton,
GlLoadingIcon, GlLoadingIcon,
DesignInput,
}, },
directives: { directives: {
GlTooltip: GlTooltipDirective, GlTooltip: GlTooltipDirective,
...@@ -17,10 +19,10 @@ export default { ...@@ -17,10 +19,10 @@ export default {
}, },
methods: { methods: {
openFileUpload() { openFileUpload() {
this.$refs.fileUpload.click(); this.$refs.fileUpload.$el.click();
}, },
onFileUploadChange() { onFileUploadChange(e) {
this.$emit('upload', this.$refs.fileUpload.files); this.$emit('upload', e.target.files);
}, },
}, },
}; };
...@@ -42,14 +44,7 @@ export default { ...@@ -42,14 +44,7 @@ export default {
{{ s__('DesignManagement|Add designs') }} {{ s__('DesignManagement|Add designs') }}
<gl-loading-icon v-if="isSaving" inline class="ml-1" /> <gl-loading-icon v-if="isSaving" inline class="ml-1" />
</gl-button> </gl-button>
<input
ref="fileUpload" <design-input ref="fileUpload" @change="onFileUploadChange" />
type="file"
name="design_file"
accept="image/*"
class="hide"
multiple
@change="onFileUploadChange"
/>
</div> </div>
</template> </template>
<script>
import VALID_DESIGN_FILE_MIMETYPE from '../../constants';
export default {
VALID_DESIGN_FILE_MIMETYPE,
};
</script>
<template>
<input
type="file"
name="design_file"
:accept="$options.VALID_DESIGN_FILE_MIMETYPE"
class="hide"
multiple
@change="$emit('change', $event)"
/>
</template>
// WARNING: replace this with something
// more sensical as per https://gitlab.com/gitlab-org/gitlab/issues/118611
const VALID_DESIGN_FILE_MIMETYPE = 'image/*';
export { VALID_DESIGN_FILE_MIMETYPE as default };
...@@ -15,13 +15,7 @@ exports[`Design management upload button component renders inverted upload desig ...@@ -15,13 +15,7 @@ exports[`Design management upload button component renders inverted upload desig
<!----> <!---->
</gl-button-stub> </gl-button-stub>
<input <design-input-stub />
accept="image/*"
class="hide"
multiple="multiple"
name="design_file"
type="file"
/>
</div> </div>
`; `;
...@@ -45,13 +39,7 @@ exports[`Design management upload button component renders loading icon 1`] = ` ...@@ -45,13 +39,7 @@ exports[`Design management upload button component renders loading icon 1`] = `
/> />
</gl-button-stub> </gl-button-stub>
<input <design-input-stub />
accept="image/*"
class="hide"
multiple="multiple"
name="design_file"
type="file"
/>
</div> </div>
`; `;
...@@ -68,12 +56,6 @@ exports[`Design management upload button component renders upload design button ...@@ -68,12 +56,6 @@ exports[`Design management upload button component renders upload design button
<!----> <!---->
</gl-button-stub> </gl-button-stub>
<input <design-input-stub />
accept="image/*"
class="hide"
multiple="multiple"
name="design_file"
type="file"
/>
</div> </div>
`; `;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Design management upload button component renders design input 1`] = `
<input
accept="image/*"
class="hide"
multiple="multiple"
name="design_file"
type="file"
/>
`;
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import UploadButton from 'ee/design_management/components/upload/button.vue'; import UploadButton from 'ee/design_management/components/upload/button.vue';
import DesignInput from 'ee/design_management/components/upload/design_input.vue';
describe('Design management upload button component', () => { describe('Design management upload button component', () => {
let wrapper; let wrapper;
...@@ -39,11 +40,7 @@ describe('Design management upload button component', () => { ...@@ -39,11 +40,7 @@ describe('Design management upload button component', () => {
it('emits upload event', () => { it('emits upload event', () => {
createComponent(); createComponent();
jest wrapper.vm.onFileUploadChange({ target: { files: 'test' } });
.spyOn(wrapper.find({ ref: 'fileUpload' }).element, 'files', 'get')
.mockReturnValue('test');
wrapper.vm.onFileUploadChange('test');
expect(wrapper.emitted().upload[0]).toEqual(['test']); expect(wrapper.emitted().upload[0]).toEqual(['test']);
}); });
...@@ -53,7 +50,7 @@ describe('Design management upload button component', () => { ...@@ -53,7 +50,7 @@ describe('Design management upload button component', () => {
it('triggers click on input', () => { it('triggers click on input', () => {
createComponent(); createComponent();
const clickSpy = jest.spyOn(wrapper.find({ ref: 'fileUpload' }).element, 'click'); const clickSpy = jest.spyOn(wrapper.find(DesignInput).element, 'click');
wrapper.vm.openFileUpload(); wrapper.vm.openFileUpload();
......
import { mount } from '@vue/test-utils';
import DesignInput from 'ee/design_management/components/upload/design_input.vue';
describe('Design management upload button component', () => {
let wrapper;
function createComponent() {
wrapper = mount(DesignInput);
}
afterEach(() => {
wrapper.destroy();
});
it('renders design input', () => {
createComponent();
expect(wrapper.element).toMatchSnapshot();
});
});
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