Commit 3aee0a8d authored by Fernando Arias's avatar Fernando Arias

Add unit tests for corpus upload form

* Add unit tests for user interactions
parent ed0ff771
...@@ -170,7 +170,9 @@ export default { ...@@ -170,7 +170,9 @@ export default {
<div v-if="isUploading" data-testid="upload-status" class="gl-mt-2"> <div v-if="isUploading" data-testid="upload-status" class="gl-mt-2">
<gl-loading-icon inline size="sm" /> <gl-loading-icon inline size="sm" />
{{ progressText }} {{ progressText }}
<gl-button size="small" @click="cancelUpload"> {{ __('Cancel') }} </gl-button> <gl-button size="small" data-testid="cancel-upload" @click="cancelUpload">
{{ __('Cancel') }}
</gl-button>
</div> </div>
</gl-form> </gl-form>
</template> </template>
...@@ -18,6 +18,7 @@ exports[`Corpus upload modal corpus modal uploading state does show the upload p ...@@ -18,6 +18,7 @@ exports[`Corpus upload modal corpus modal uploading state does show the upload p
<button <button
class="btn btn-default btn-sm gl-button" class="btn btn-default btn-sm gl-button"
data-testid="cancel-upload"
type="button" type="button"
> >
<!----> <!---->
......
import { createLocalVue, mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import CorpusUploadForm from 'ee/security_configuration/corpus_management/components/corpus_upload_form.vue'; import CorpusUploadForm from 'ee/security_configuration/corpus_management/components/corpus_upload_form.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
const TEST_PROJECT_FULL_PATH = '/namespace/project'; const TEST_PROJECT_FULL_PATH = '/namespace/project';
const localVue = createLocalVue();
localVue.use(VueApollo);
describe('Corpus upload modal', () => { describe('Corpus upload modal', () => {
let wrapper; let wrapper;
...@@ -15,12 +10,12 @@ describe('Corpus upload modal', () => { ...@@ -15,12 +10,12 @@ describe('Corpus upload modal', () => {
const findUploadAttachment = () => wrapper.find('[data-testid="upload-attachment-button"]'); const findUploadAttachment = () => wrapper.find('[data-testid="upload-attachment-button"]');
const findUploadCorpus = () => wrapper.find('[data-testid="upload-corpus"]'); const findUploadCorpus = () => wrapper.find('[data-testid="upload-corpus"]');
const findUploadStatus = () => wrapper.find('[data-testid="upload-status"]'); const findUploadStatus = () => wrapper.find('[data-testid="upload-status"]');
const findFileInput = () => wrapper.find({ ref: 'fileUpload' });
const findCancelButton = () => wrapper.find('[data-testid="cancel-upload"]');
const createComponent = (propsData, options = {}) => { const createComponent = (propsData, options = {}) => {
wrapper = mount(CorpusUploadForm, { wrapper = mount(CorpusUploadForm, {
localVue,
propsData, propsData,
apolloProvider: createMockApollo(),
provide: { provide: {
projectFullPath: TEST_PROJECT_FULL_PATH, projectFullPath: TEST_PROJECT_FULL_PATH,
}, },
...@@ -46,10 +41,6 @@ describe('Corpus upload modal', () => { ...@@ -46,10 +41,6 @@ describe('Corpus upload modal', () => {
const props = { const props = {
states: { states: {
mockedPackages: {
totalSize: 0,
data: [],
},
uploadState: { uploadState: {
isUploading: false, isUploading: false,
progress: 0, progress: 0,
...@@ -75,6 +66,15 @@ describe('Corpus upload modal', () => { ...@@ -75,6 +66,15 @@ describe('Corpus upload modal', () => {
it('does not show the upload progress', () => { it('does not show the upload progress', () => {
expect(findUploadStatus().exists()).toBe(false); expect(findUploadStatus().exists()).toBe(false);
}); });
describe('selecting a file', () => {
it('transitions to selected state', async () => {
jest.spyOn(wrapper.vm, 'onFileUploadChange').mockImplementation(() => {});
await wrapper.vm.$forceUpdate();
findFileInput().trigger('change');
expect(wrapper.vm.onFileUploadChange).toHaveBeenCalled();
});
});
}); });
describe('file selected state', () => { describe('file selected state', () => {
...@@ -87,16 +87,11 @@ describe('Corpus upload modal', () => { ...@@ -87,16 +87,11 @@ describe('Corpus upload modal', () => {
attachmentName, attachmentName,
corpusName, corpusName,
files: [attachmentName], files: [attachmentName],
uploadTimeout: null,
}; };
}; };
const props = { const props = {
states: { states: {
mockedPackages: {
totalSize: 0,
data: [],
},
uploadState: { uploadState: {
isUploading: false, isUploading: false,
progress: 0, progress: 0,
...@@ -122,6 +117,15 @@ describe('Corpus upload modal', () => { ...@@ -122,6 +117,15 @@ describe('Corpus upload modal', () => {
it('does not show the upload progress', () => { it('does not show the upload progress', () => {
expect(findUploadStatus().exists()).toBe(false); expect(findUploadStatus().exists()).toBe(false);
}); });
describe('clicking upload file', () => {
it('begins the file upload', async () => {
jest.spyOn(wrapper.vm, 'beginFileUpload').mockImplementation(() => {});
await wrapper.vm.$forceUpdate();
findUploadCorpus().trigger('click');
expect(wrapper.vm.beginFileUpload).toHaveBeenCalled();
});
});
}); });
describe('uploading state', () => { describe('uploading state', () => {
...@@ -134,16 +138,11 @@ describe('Corpus upload modal', () => { ...@@ -134,16 +138,11 @@ describe('Corpus upload modal', () => {
attachmentName, attachmentName,
corpusName, corpusName,
files: [attachmentName], files: [attachmentName],
uploadTimeout: null,
}; };
}; };
const props = { const props = {
states: { states: {
mockedPackages: {
totalSize: 0,
data: [],
},
uploadState: { uploadState: {
isUploading: true, isUploading: true,
progress: 25, progress: 25,
...@@ -171,6 +170,13 @@ describe('Corpus upload modal', () => { ...@@ -171,6 +170,13 @@ describe('Corpus upload modal', () => {
expect(findUploadStatus().exists()).toBe(true); expect(findUploadStatus().exists()).toBe(true);
expect(findUploadStatus().element).toMatchSnapshot(); expect(findUploadStatus().element).toMatchSnapshot();
}); });
describe('clicking cancel button', () => {
it('emits the reset corpus event', () => {
findCancelButton().trigger('click');
expect(wrapper.emitted().resetCorpus).toBeTruthy();
});
});
}); });
describe('file uploaded state', () => { describe('file uploaded state', () => {
...@@ -183,16 +189,11 @@ describe('Corpus upload modal', () => { ...@@ -183,16 +189,11 @@ describe('Corpus upload modal', () => {
attachmentName, attachmentName,
corpusName, corpusName,
files: [attachmentName], files: [attachmentName],
uploadTimeout: null,
}; };
}; };
const props = { const props = {
states: { states: {
mockedPackages: {
totalSize: 0,
data: [],
},
uploadState: { uploadState: {
isUploading: false, isUploading: false,
progress: 100, progress: 100,
......
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