ajax_loading_spinner_spec.js 1.9 KB
Newer Older
1
import $ from 'jquery';
2
import AjaxLoadingSpinner from '~/ajax_loading_spinner';
3 4 5 6 7 8 9

describe('Ajax Loading Spinner', () => {
  const fixtureTemplate = 'static/ajax_loading_spinner.html.raw';
  preloadFixtures(fixtureTemplate);

  beforeEach(() => {
    loadFixtures(fixtureTemplate);
10
    AjaxLoadingSpinner.init();
11 12 13
  });

  it('change current icon with spinner icon and disable link while waiting ajax response', (done) => {
14
    spyOn($, 'ajax').and.callFake((req) => {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      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();
  });

  it('use original icon again and enabled the link after complete the ajax request', (done) => {
37
    spyOn($, 'ajax').and.callFake((req) => {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
      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');
      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();
  });
});