Commit 156d5fe9 authored by pburdette's avatar pburdette

Refactor unit test

Remove axios and check
if mutations are called. Also
catch errors as data in component.
parent 471ad1ac
...@@ -129,12 +129,20 @@ export default { ...@@ -129,12 +129,20 @@ export default {
this.isCanceling = true; this.isCanceling = true;
try { try {
await this.$apollo.mutate({ const {
data: {
pipelineCancel: { errors },
},
} = await this.$apollo.mutate({
mutation: cancelPipelineMutation, mutation: cancelPipelineMutation,
variables: { id: this.pipeline.id }, variables: { id: this.pipeline.id },
}); });
if (errors.length > 0) {
this.reportFailure(POST_FAILURE);
} else {
this.$apollo.queries.pipeline.refetch(); this.$apollo.queries.pipeline.refetch();
}
} catch { } catch {
this.reportFailure(POST_FAILURE); this.reportFailure(POST_FAILURE);
} }
...@@ -143,12 +151,20 @@ export default { ...@@ -143,12 +151,20 @@ export default {
this.isRetrying = true; this.isRetrying = true;
try { try {
await this.$apollo.mutate({ const {
data: {
pipelineRetry: { errors },
},
} = await this.$apollo.mutate({
mutation: retryPipelineMutation, mutation: retryPipelineMutation,
variables: { id: this.pipeline.id }, variables: { id: this.pipeline.id },
}); });
if (errors.length > 0) {
this.reportFailure(POST_FAILURE);
} else {
this.$apollo.queries.pipeline.refetch(); this.$apollo.queries.pipeline.refetch();
}
} catch { } catch {
this.reportFailure(POST_FAILURE); this.reportFailure(POST_FAILURE);
} }
...@@ -158,14 +174,23 @@ export default { ...@@ -158,14 +174,23 @@ export default {
this.$apollo.queries.pipeline.stopPolling(); this.$apollo.queries.pipeline.stopPolling();
try { try {
await this.$apollo.mutate({ const {
data: {
pipelineDestroy: { errors },
},
} = await this.$apollo.mutate({
mutation: deletePipelineMutation, mutation: deletePipelineMutation,
variables: { variables: {
id: this.pipeline.id, id: this.pipeline.id,
}, },
}); });
if (errors.length > 0) {
this.reportFailure(DELETE_FAILURE);
this.isDeleting = false;
} else {
redirectTo(setUrlFragment(this.paths.pipelinesPath, 'delete_success')); redirectTo(setUrlFragment(this.paths.pipelinesPath, 'delete_success'));
}
} catch { } catch {
this.$apollo.queries.pipeline.startPolling(POLL_INTERVAL); this.$apollo.queries.pipeline.startPolling(POLL_INTERVAL);
this.reportFailure(DELETE_FAILURE); this.reportFailure(DELETE_FAILURE);
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { GlModal, GlLoadingIcon } from '@gitlab/ui'; import { GlModal, GlLoadingIcon } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter';
import { import {
mockCancelledPipelineHeader, mockCancelledPipelineHeader,
mockFailedPipelineHeader, mockFailedPipelineHeader,
mockRunningPipelineHeader, mockRunningPipelineHeader,
mockSuccessfulPipelineHeader, mockSuccessfulPipelineHeader,
} from './mock_data'; } from './mock_data';
import axios from '~/lib/utils/axios_utils';
import HeaderComponent from '~/pipelines/components/header_component.vue'; import HeaderComponent from '~/pipelines/components/header_component.vue';
import deletePipelineMutation from '~/pipelines/graphql/mutations/delete_pipeline.mutation.graphql';
import retryPipelineMutation from '~/pipelines/graphql/mutations/retry_pipeline.mutation.graphql';
import cancelPipelineMutation from '~/pipelines/graphql/mutations/cancel_pipeline.mutation.graphql';
describe('Pipeline details header', () => { describe('Pipeline details header', () => {
let wrapper; let wrapper;
let glModalDirective; let glModalDirective;
let mockAxios;
const findDeleteModal = () => wrapper.find(GlModal); const findDeleteModal = () => wrapper.find(GlModal);
const findRetryButton = () => wrapper.find('[data-testid="retryPipeline"]'); const findRetryButton = () => wrapper.find('[data-testid="retryPipeline"]');
...@@ -25,9 +25,7 @@ describe('Pipeline details header', () => { ...@@ -25,9 +25,7 @@ describe('Pipeline details header', () => {
pipelineId: 14, pipelineId: 14,
pipelineIid: 1, pipelineIid: 1,
paths: { paths: {
retry: '/retry', pipelinesPath: '/namespace/my-project/-/pipelines',
cancel: '/cancel',
delete: '/delete',
fullProject: '/namespace/my-project', fullProject: '/namespace/my-project',
}, },
}; };
...@@ -43,6 +41,7 @@ describe('Pipeline details header', () => { ...@@ -43,6 +41,7 @@ describe('Pipeline details header', () => {
startPolling: jest.fn(), startPolling: jest.fn(),
}, },
}, },
mutate: jest.fn(),
}; };
return shallowMount(HeaderComponent, { return shallowMount(HeaderComponent, {
...@@ -65,16 +64,9 @@ describe('Pipeline details header', () => { ...@@ -65,16 +64,9 @@ describe('Pipeline details header', () => {
}); });
}; };
beforeEach(() => {
mockAxios = new MockAdapter(axios);
mockAxios.onGet('*').replyOnce(200);
});
afterEach(() => { afterEach(() => {
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
mockAxios.restore();
}); });
describe('initial loading', () => { describe('initial loading', () => {
...@@ -111,13 +103,13 @@ describe('Pipeline details header', () => { ...@@ -111,13 +103,13 @@ describe('Pipeline details header', () => {
wrapper = createComponent(mockCancelledPipelineHeader); wrapper = createComponent(mockCancelledPipelineHeader);
}); });
it('should call axios with the right path when retry button is clicked', async () => { it('should call retryPipeline Mutation with pipeline id', () => {
jest.spyOn(axios, 'post');
findRetryButton().vm.$emit('click'); findRetryButton().vm.$emit('click');
await wrapper.vm.$nextTick(); expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
mutation: retryPipelineMutation,
expect(axios.post).toHaveBeenCalledWith(defaultProvideOptions.paths.retry); variables: { id: mockCancelledPipelineHeader.id },
});
}); });
}); });
...@@ -126,13 +118,13 @@ describe('Pipeline details header', () => { ...@@ -126,13 +118,13 @@ describe('Pipeline details header', () => {
wrapper = createComponent(mockRunningPipelineHeader); wrapper = createComponent(mockRunningPipelineHeader);
}); });
it('should call axios with the right path when cancel button is clicked', async () => { it('should call cancelPipeline Mutation with pipeline id', () => {
jest.spyOn(axios, 'post');
findCancelButton().vm.$emit('click'); findCancelButton().vm.$emit('click');
await wrapper.vm.$nextTick(); expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
mutation: cancelPipelineMutation,
expect(axios.post).toHaveBeenCalledWith(defaultProvideOptions.paths.cancel); variables: { id: mockRunningPipelineHeader.id },
});
}); });
}); });
...@@ -141,24 +133,21 @@ describe('Pipeline details header', () => { ...@@ -141,24 +133,21 @@ describe('Pipeline details header', () => {
wrapper = createComponent(mockFailedPipelineHeader); wrapper = createComponent(mockFailedPipelineHeader);
}); });
it('displays delete modal when clicking on delete and does not call the delete action', async () => { it('displays delete modal when clicking on delete and does not call the delete action', () => {
jest.spyOn(axios, 'delete');
findDeleteButton().vm.$emit('click'); findDeleteButton().vm.$emit('click');
await wrapper.vm.$nextTick();
expect(findDeleteModal().props('modalId')).toBe(wrapper.vm.$options.DELETE_MODAL_ID); expect(findDeleteModal().props('modalId')).toBe(wrapper.vm.$options.DELETE_MODAL_ID);
expect(glModalDirective).toHaveBeenCalledWith(wrapper.vm.$options.DELETE_MODAL_ID); expect(glModalDirective).toHaveBeenCalledWith(wrapper.vm.$options.DELETE_MODAL_ID);
expect(axios.delete).not.toHaveBeenCalled(); expect(wrapper.vm.$apollo.mutate).not.toHaveBeenCalled();
}); });
it('should call delete path when modal is submitted', async () => { it('should call deletePipeline Mutation with pipeline id when modal is submitted', () => {
jest.spyOn(axios, 'delete');
findDeleteModal().vm.$emit('ok'); findDeleteModal().vm.$emit('ok');
await wrapper.vm.$nextTick(); expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
mutation: deletePipelineMutation,
expect(axios.delete).toHaveBeenCalledWith(defaultProvideOptions.paths.delete); variables: { id: mockFailedPipelineHeader.id },
});
}); });
}); });
}); });
......
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