Commit b5b30446 authored by Phil Hughes's avatar Phil Hughes

Converted mini_pipeline_graph_dropdown.js to axios

parent 76b9f1a4
/* eslint-disable no-new */ /* eslint-disable no-new */
import Flash from './flash'; import flash from './flash';
import axios from './lib/utils/axios_utils';
/** /**
* In each pipelines table we have a mini pipeline graph for each pipeline. * In each pipelines table we have a mini pipeline graph for each pipeline.
...@@ -78,27 +79,22 @@ export default class MiniPipelineGraph { ...@@ -78,27 +79,22 @@ export default class MiniPipelineGraph {
const button = e.relatedTarget; const button = e.relatedTarget;
const endpoint = button.dataset.stageEndpoint; const endpoint = button.dataset.stageEndpoint;
return $.ajax({ this.renderBuildsList(button, '');
dataType: 'json', this.toggleLoading(button);
type: 'GET',
url: endpoint, axios.get(endpoint)
beforeSend: () => { .then(({ data }) => {
this.renderBuildsList(button, '');
this.toggleLoading(button);
},
success: (data) => {
this.toggleLoading(button); this.toggleLoading(button);
this.renderBuildsList(button, data.html); this.renderBuildsList(button, data.html);
this.stopDropdownClickPropagation(); this.stopDropdownClickPropagation();
}, })
error: () => { .catch(() => {
this.toggleLoading(button); this.toggleLoading(button);
if ($(button).parent().hasClass('open')) { if ($(button).parent().hasClass('open')) {
$(button).dropdown('toggle'); $(button).dropdown('toggle');
} }
new Flash('An error occurred while fetching the builds.', 'alert'); flash('An error occurred while fetching the builds.', 'alert');
}, });
});
} }
/** /**
......
/* eslint-disable no-new */ /* eslint-disable no-new */
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import MiniPipelineGraph from '~/mini_pipeline_graph_dropdown'; import MiniPipelineGraph from '~/mini_pipeline_graph_dropdown';
import '~/flash'; import timeoutPromise from './helpers/set_timeout_promise_helper';
describe('Mini Pipeline Graph Dropdown', () => { describe('Mini Pipeline Graph Dropdown', () => {
preloadFixtures('static/mini_dropdown_graph.html.raw'); preloadFixtures('static/mini_dropdown_graph.html.raw');
...@@ -27,6 +29,16 @@ describe('Mini Pipeline Graph Dropdown', () => { ...@@ -27,6 +29,16 @@ describe('Mini Pipeline Graph Dropdown', () => {
}); });
describe('When dropdown is clicked', () => { describe('When dropdown is clicked', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
it('should call getBuildsList', () => { it('should call getBuildsList', () => {
const getBuildsListSpy = spyOn( const getBuildsListSpy = spyOn(
MiniPipelineGraph.prototype, MiniPipelineGraph.prototype,
...@@ -41,46 +53,55 @@ describe('Mini Pipeline Graph Dropdown', () => { ...@@ -41,46 +53,55 @@ describe('Mini Pipeline Graph Dropdown', () => {
}); });
it('should make a request to the endpoint provided in the html', () => { it('should make a request to the endpoint provided in the html', () => {
const ajaxSpy = spyOn($, 'ajax').and.callFake(function () {}); const ajaxSpy = spyOn(axios, 'get').and.callThrough();
mock.onGet('foobar').reply(200, {
html: '',
});
new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents(); new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();
document.querySelector('.js-builds-dropdown-button').click(); document.querySelector('.js-builds-dropdown-button').click();
expect(ajaxSpy.calls.allArgs()[0][0].url).toEqual('foobar'); expect(ajaxSpy.calls.allArgs()[0][0]).toEqual('foobar');
}); });
it('should not close when user uses cmd/ctrl + click', () => { it('should not close when user uses cmd/ctrl + click', (done) => {
spyOn($, 'ajax').and.callFake(function (params) { mock.onGet('foobar').reply(200, {
params.success({ html: `<li>
html: `<li> <a class="mini-pipeline-graph-dropdown-item" href="#">
<a class="mini-pipeline-graph-dropdown-item" href="#"> <span class="ci-status-icon ci-status-icon-failed"></span>
<span class="ci-status-icon ci-status-icon-failed"></span> <span class="ci-build-text">build</span>
<span class="ci-build-text">build</span> </a>
</a> <a class="ci-action-icon-wrapper js-ci-action-icon" href="#"></a>
<a class="ci-action-icon-wrapper js-ci-action-icon" href="#"></a> </li>`,
</li>`,
});
}); });
new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents(); new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();
document.querySelector('.js-builds-dropdown-button').click(); document.querySelector('.js-builds-dropdown-button').click();
document.querySelector('a.mini-pipeline-graph-dropdown-item').click(); timeoutPromise()
.then(() => {
expect($('.js-builds-dropdown-list').is(':visible')).toEqual(true); document.querySelector('a.mini-pipeline-graph-dropdown-item').click();
})
.then(timeoutPromise)
.then(() => {
expect($('.js-builds-dropdown-list').is(':visible')).toEqual(true);
})
.then(done)
.catch(done.fail);
}); });
});
it('should close the dropdown when request returns an error', (done) => { it('should close the dropdown when request returns an error', (done) => {
spyOn($, 'ajax').and.callFake(options => options.error()); mock.onGet('foobar').networkError();
new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents(); new MiniPipelineGraph({ container: '.js-builds-dropdown-tests' }).bindEvents();
document.querySelector('.js-builds-dropdown-button').click(); document.querySelector('.js-builds-dropdown-button').click();
setTimeout(() => { setTimeout(() => {
expect($('.js-builds-dropdown-tests .dropdown').hasClass('open')).toEqual(false); expect($('.js-builds-dropdown-tests .dropdown').hasClass('open')).toEqual(false);
done(); done();
}, 0); });
});
}); });
}); });
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