2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-10-09 05:04:13 -04:00
|
|
|
import AjaxLoadingSpinner from '~/ajax_loading_spinner';
|
2016-10-09 10:17:14 -04:00
|
|
|
|
|
|
|
describe('Ajax Loading Spinner', () => {
|
2019-03-26 12:03:28 -04:00
|
|
|
const fixtureTemplate = 'static/ajax_loading_spinner.html';
|
2016-10-09 10:17:14 -04:00
|
|
|
preloadFixtures(fixtureTemplate);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(fixtureTemplate);
|
2017-10-09 05:04:13 -04:00
|
|
|
AjaxLoadingSpinner.init();
|
2016-10-09 10:17:14 -04:00
|
|
|
});
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
it('change current icon with spinner icon and disable link while waiting ajax response', done => {
|
|
|
|
spyOn($, 'ajax').and.callFake(req => {
|
2016-10-09 10:17:14 -04:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
const ajaxLoadingSpinner = document.querySelector('.js-ajax-loading-spinner');
|
|
|
|
const icon = ajaxLoadingSpinner.querySelector('i');
|
|
|
|
|
|
|
|
req.beforeSend(xhr, { dataType: 'text/html' });
|
|
|
|
|
|
|
|
expect(icon).not.toHaveClass('fa-trash-o');
|
|
|
|
expect(icon).toHaveClass('fa-spinner');
|
|
|
|
expect(icon).toHaveClass('fa-spin');
|
|
|
|
expect(icon.dataset.icon).toEqual('fa-trash-o');
|
|
|
|
expect(ajaxLoadingSpinner.getAttribute('disabled')).toEqual('');
|
|
|
|
|
|
|
|
req.complete({});
|
|
|
|
|
|
|
|
done();
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
return deferred.promise();
|
|
|
|
});
|
|
|
|
document.querySelector('.js-ajax-loading-spinner').click();
|
|
|
|
});
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
it('use original icon again and enabled the link after complete the ajax request', done => {
|
|
|
|
spyOn($, 'ajax').and.callFake(req => {
|
2016-10-09 10:17:14 -04:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
const ajaxLoadingSpinner = document.querySelector('.js-ajax-loading-spinner');
|
|
|
|
|
|
|
|
req.beforeSend(xhr, { dataType: 'text/html' });
|
|
|
|
req.complete({});
|
|
|
|
|
|
|
|
const icon = ajaxLoadingSpinner.querySelector('i');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2016-10-09 10:17:14 -04:00
|
|
|
expect(icon).toHaveClass('fa-trash-o');
|
|
|
|
expect(icon).not.toHaveClass('fa-spinner');
|
|
|
|
expect(icon).not.toHaveClass('fa-spin');
|
|
|
|
expect(ajaxLoadingSpinner.getAttribute('disabled')).toEqual(null);
|
|
|
|
|
|
|
|
done();
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
return deferred.promise();
|
|
|
|
});
|
|
|
|
document.querySelector('.js-ajax-loading-spinner').click();
|
|
|
|
});
|
|
|
|
});
|