Commit f6edec19 authored by Jose Vargas's avatar Jose Vargas

Move Vue toast init code

Also change the test code to emit
a click instead of accesing the component
internals
parent ba0ddd1a
import Vue from 'vue'; import Vue from 'vue';
import { GlToast } from '@gitlab/ui';
import ResetButton from './reset_button.vue'; import ResetButton from './reset_button.vue';
Vue.use(GlToast);
export function pipelineMinutes() { export function pipelineMinutes() {
const el = document.getElementById('pipeline-minutes-vue'); const el = document.getElementById('pipeline-minutes-vue');
......
<script> <script>
import Vue from 'vue'; import { GlButton } from '@gitlab/ui';
import { GlButton, GlToast } from '@gitlab/ui';
import { __ } from '~/locale'; import { __ } from '~/locale';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import statusCodes from '~/lib/utils/http_status'; import statusCodes from '~/lib/utils/http_status';
Vue.use(GlToast);
export default { export default {
components: { components: {
GlButton, GlButton,
...@@ -19,7 +16,7 @@ export default { ...@@ -19,7 +16,7 @@ export default {
}, },
methods: { methods: {
resetPipelineMinutes() { resetPipelineMinutes() {
return axios axios
.post(this.resetMinutesPath) .post(this.resetMinutesPath)
.then(resp => { .then(resp => {
if (resp.status === statusCodes.OK) { if (resp.status === statusCodes.OK) {
......
...@@ -3,9 +3,10 @@ import { GlButton } from '@gitlab/ui'; ...@@ -3,9 +3,10 @@ import { GlButton } from '@gitlab/ui';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import ResetButton from 'ee/pages/admin/users/pipeline_minutes/reset_button.vue'; import ResetButton from 'ee/pages/admin/users/pipeline_minutes/reset_button.vue';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import httpStatusCodes from '~/lib/utils/http_status';
const defaultProps = { resetMinutesPath: '/adming/reset_minutes' }; const defaultProps = { resetMinutesPath: '/adming/reset_minutes' };
const toastMock = { const $toast = {
show: jest.fn(), show: jest.fn(),
}; };
...@@ -19,16 +20,14 @@ describe('Reset pipeline minutes button', () => { ...@@ -19,16 +20,14 @@ describe('Reset pipeline minutes button', () => {
...defaultProps, ...defaultProps,
}, },
mocks: { mocks: {
$toast: toastMock, $toast,
}, },
}); });
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
mock.onPost(defaultProps.resetMinutesPath).reply(200, {});
}); });
afterEach(() => { afterEach(() => {
mock.restore();
wrapper.destroy(); wrapper.destroy();
wrapper = null; wrapper = null;
}); });
...@@ -41,13 +40,56 @@ describe('Reset pipeline minutes button', () => { ...@@ -41,13 +40,56 @@ describe('Reset pipeline minutes button', () => {
expect(button.text()).toBe('Reset pipeline minutes'); expect(button.text()).toBe('Reset pipeline minutes');
}); });
it('should call do a network request when reseting the pipelines', () => { describe('when the api is available', () => {
beforeEach(() => {
mock
.onPost(defaultProps.resetMinutesPath)
.reply(httpStatusCodes.OK, { status: httpStatusCodes.OK });
});
afterEach(() => {
mock.restore();
});
it('should create a network request when the reset button is clicked', () => {
const axiosSpy = jest.spyOn(axios, 'post'); const axiosSpy = jest.spyOn(axios, 'post');
wrapper.vm.resetPipelineMinutes(); const button = findResetButton();
button.vm.$emit('click');
return wrapper.vm.$nextTick().then(() => { return axios.waitForAll().then(() => {
expect(axiosSpy).toHaveBeenCalled(); expect(axiosSpy).toHaveBeenCalled();
expect($toast.show).toHaveBeenCalledWith('User pipeline minutes were successfully reset.');
});
});
});
describe('when the api is not available', () => {
beforeEach(() => {
mock.onPost(defaultProps.resetMinutesPath).reply(httpStatusCodes.SERVICE_UNAVAILABLE, {
status: httpStatusCodes.SERVICE_UNAVAILABLE,
});
});
afterEach(() => {
mock.restore();
});
it('should show a toast error message', () => {
const axiosSpy = jest.spyOn(axios, 'post');
const button = findResetButton();
button.vm.$emit('click');
return axios.waitForAll().then(() => {
expect(axiosSpy).toHaveBeenCalled();
expect($toast.show).toHaveBeenCalledWith(
'There was an error resetting user pipeline minutes.',
{ type: 'error' },
);
});
}); });
}); });
}); });
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